ColorSegmenter.C
00001
00002
00003 #include "BeoSub/ColorSegmenter.H"
00004
00005
00006 ColorSegmenter::ColorSegmenter(){
00007
00008
00009 debugmode = false;
00010 setup = false;
00011 color.resize(4,0.0F);
00012 std.resize(4,0.0F);
00013 norm.resize(4,0.0F);
00014 adapt.resize(4,0.0F);
00015 lowerBound.resize(4,0.0F);
00016 upperBound.resize(4,0.0F);
00017 candidate_color = "none";
00018 Hz = 0.0F;
00019 fps = 0.0F;
00020 res = 0.0F;
00021
00022 int wi = 320/4;
00023 int hi = 240/4;
00024
00025
00026 width = 320;
00027 height = 240;
00028 segmenter = new segmentImageTrackMC<float,unsigned int, 4> (width*height);
00029
00030 segmenter->SITsetFrame(&wi,&hi);
00031
00032 segmenter->SITsetCircleColor(0,255,0);
00033 segmenter->SITsetBoxColor(255,255,0,0,255,255);
00034 segmenter->SITsetUseSmoothing(false,10);
00035
00036
00037 segmenter->SITtoggleCandidateBandPass(false);
00038 segmenter->SITtoggleColorAdaptation(false);
00039 }
00040
00041 ColorSegmenter::~ColorSegmenter(){
00042 }
00043
00044
00045 void ColorSegmenter::setupColorSegmenter(const char* inputColor, bool debug){
00046 debugmode = debug;
00047
00048
00049
00050
00051
00052
00053 setupOrange();
00054
00055
00056
00057
00058
00059
00060 segmenter->SITsetTrackColor(&color,&std,&norm,&adapt,&upperBound,&lowerBound);
00061
00062
00063 wini.reset(new XWindow(Dims(width, height), 0, 0, "test-input window"));
00064 wini->setPosition(0, 0);
00065 wino.reset(new XWindow(Dims(width, height), 0, 0, "test-output window"));
00066 wino->setPosition(width+10, 0);
00067
00068
00069
00070 setup = true;
00071 }
00072
00073 void ColorSegmenter::setupOrange() {
00074
00075 colorConf.openFile("colortrack.conf", true);
00076
00077 PixRGB<float> P1(colorConf.getItemValueF("ORANGE_R"),
00078 colorConf.getItemValueF("ORANGE_G"),
00079 colorConf.getItemValueF("ORANGE_B"));
00080
00081 PixH2SV1<float> ORANGE(P1);
00082
00083 color[0] = ORANGE.H1(); color[1] = ORANGE.H2(); color[2] = ORANGE.S(); color[3] = ORANGE.V();
00084
00085
00086 std[0] = colorConf.getItemValueF("ORNG_std0");
00087 std[1] = colorConf.getItemValueF("ORNG_std1");
00088 std[2] = colorConf.getItemValueF("ORNG_std2");
00089 std[3] = colorConf.getItemValueF("ORNG_std3");
00090
00091
00092 norm[0] = colorConf.getItemValueF("ORNG_norm0");
00093 norm[1] = colorConf.getItemValueF("ORNG_norm1");
00094 norm[2] = colorConf.getItemValueF("ORNG_norm2");
00095 norm[3] = colorConf.getItemValueF("ORNG_norm3");
00096
00097
00098 adapt[0] = colorConf.getItemValueF("ORNG_adapt0");
00099 adapt[1] = colorConf.getItemValueF("ORNG_adapt1");
00100 adapt[2] = colorConf.getItemValueF("ORNG_adapt2");
00101 adapt[3] = colorConf.getItemValueF("ORNG_adapt3");
00102
00103
00104 upperBound[0] = color[0] + colorConf.getItemValueF("ORNG_up0");
00105 upperBound[1] = color[1] + colorConf.getItemValueF("ORNG_up1");
00106 upperBound[2] = color[2] + colorConf.getItemValueF("ORNG_up2");
00107 upperBound[3] = color[3] + colorConf.getItemValueF("ORNG_up3");
00108
00109
00110 lowerBound[0] = color[0] - colorConf.getItemValueF("ORNG_lb0");
00111 lowerBound[1] = color[1] - colorConf.getItemValueF("ORNG_lb1");
00112 lowerBound[2] = color[2] - colorConf.getItemValueF("ORNG_lb2");
00113 lowerBound[3] = color[3] - colorConf.getItemValueF("ORNG_lb3");
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 float ColorSegmenter::isolateOrange(Image< PixRGB<byte> > &inputImage, Image<byte> &outputImage)
00251 {
00252 Image< PixRGB<byte> > tempImage(inputImage);
00253 Image<PixH2SV2<float> > h2svImage(tempImage);
00254
00255 readConfig colorConf;
00256 colorConf.openFile("colortrack.conf", false);
00257
00258 int orangeCount = 0;
00259
00260 Image<PixRGB<byte> >::iterator iptr = inputImage.beginw();
00261 Image<byte>::iterator optr = outputImage.beginw();
00262 Image<PixRGB<byte> >::iterator stop = inputImage.endw();
00263
00264 float tR = colorConf.getItemValueF("ORANGE_stdR");
00265 float tG = colorConf.getItemValueF("ORANGE_stdG");
00266 float tB = colorConf.getItemValueF("ORANGE_stdB");
00267
00268 float R = colorConf.getItemValueF("ORANGE_R");
00269 float G = colorConf.getItemValueF("ORANGE_G");
00270 float B = colorConf.getItemValueF("ORANGE_B");
00271
00272
00273
00274 float totalHue = 0.0;
00275 int numTotal = 0;
00276
00277
00278 float pure_orange_hue = 60*(((G/255)-(B/255))/((R/255)-(B/255)))+0;
00279 float orange_hue = 60*(((tB/255)-(tR/255))/((tG/255) - (tR/255)))+120;
00280
00281 float orange_sat = ((200.0/255.0)-(70/255.0))/(2.0-(270.0/255.0));
00282
00283 std::cout<<"orange hue is: "<<orange_hue<<std::endl;
00284 std::cout<<"orange saturation(purity) is: "<<orange_sat<<std::endl;
00285 std::cout<<"orange HSV saturation is: "<<(1.0-70.0/200.0)<<std::endl;
00286
00287
00288 while (iptr != stop)
00289 {
00290 float hue = 0.0;
00291 float s = 0.0;
00292 float avgR = (*iptr).red();
00293 float avgG = (*iptr).green();
00294 float avgB = (*iptr).blue();
00295 float r = avgR/255;
00296 float g = avgG/255;
00297 float b = avgB/255;
00298
00299
00300
00301
00302
00303 float max = 0;
00304 float min = 1;
00305
00306 if(r > max) { max = r;}
00307 if(g > max) { max = g;}
00308 if(b > max) { max = b;}
00309
00310 if(r < min){min = r;}
00311 if(g < min){min = g;}
00312 if(b < min){min = b;}
00313
00314
00315 if(max == min) {hue = 0.0;}
00316 else if(max == r && g >= b) {hue = 60.0*((g-b)/(max - min)) + 0.0;}
00317 else if(max == r && g < b) {hue = 60.0*((g-b)/(max - min)) + 360.0;}
00318 else if(max == g) {hue = 60.0*((b-r)/(max-min))+120.0;}
00319 else if(max == b) {hue = 60.0*((r-g)/(max-min))+240.0;}
00320
00321
00322
00323 totalHue += hue;
00324 numTotal++;
00325
00326
00327 if(max){s = max;}
00328 if(max != 0){s = 1 - min/max;}
00329
00330 if(hue == orange_hue)
00331 {
00332
00333
00334 }
00335 if(hue == pure_orange_hue)
00336 {
00337
00338
00339 }
00340
00341
00342
00343
00344
00345
00346 if(!(120<hue && hue<146) &&
00347 s != 1)
00348 {
00349
00350 (*optr) = (byte)255;
00351 orangeCount++;
00352 }
00353
00354
00355
00356
00357
00358
00359 if (avgR > R - tR && avgR < R + tR &&
00360 avgG > G - tG && avgG < G + tG &&
00361 avgB > B - tB && avgB < B + tB )
00362 {
00363 (*optr) = (byte)255;
00364 orangeCount++;
00365 }
00366
00367
00368
00369
00370
00371
00372 iptr++; optr++;
00373
00374 }
00375
00376
00377
00378 Image<PixRGB<byte> > Aux;
00379 Aux.resize(100,450,true);
00380
00381
00382
00383
00384 segmenter->SITtrackImageAny(h2svImage,&inputImage,&Aux,true);
00385
00386
00387 Image<byte> temp = quickInterpolate(segmenter->SITreturnCandidateImage(),4);
00388
00389
00390 wino->drawImage(temp);
00391
00392 std::cout<<"average hue was "<<totalHue/numTotal<<std::endl;
00393 return float(orangeCount)/float( (inputImage.getHeight() * inputImage.getWidth()));
00394 }
00395
00396
00397
00398
00399