00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "Image/ColorOps.H"
00040 #include "Image/FilterOps.H"
00041 #include "Image/Image.H"
00042 #include "Image/ImageSet.H"
00043 #include "Image/ImageSetOps.H"
00044 #include "Image/MathOps.H"
00045 #include "Image/Pixels.H"
00046 #include "Image/ShapeOps.H"
00047 #include "Image/Transforms.H"
00048 #include "Image/fancynorm.H"
00049 #include "Landmark/Tree.H"
00050 #include "Landmark/density.H"
00051 #include "Raster/Raster.H"
00052 #include "Util/Timer.H"
00053 #include "Util/log.H"
00054
00055 #include <cstdio>
00056 #include <iostream>
00057 #include <map>
00058 #include <vector>
00059
00060 int countClusters(Image<float> input, float thresh)
00061 {
00062
00063 int w = input.getWidth();
00064 int h = input.getHeight();
00065
00066 int count = 0;
00067 for(int i = 0; i < w; i++)
00068 for(int j = 0; j < h; j++)
00069 {
00070 if(input.getVal(i,j) > thresh)
00071 {
00072
00073 Image<float> output;
00074 flood(input, output, Point2D<int>(i,j), thresh, 255.0f);
00075 input -= output;
00076 inplaceClamp(input, 0.0f, 255.0f);
00077 count++;
00078 }
00079 }
00080 return count;
00081 }
00082
00083
00084
00085 int main (int argc, char **argv)
00086 {
00087 if (argc != 3)
00088 {
00089 std::cerr << "usage: test-segmentLandmark <option> <weight>\n\n \
00090 where <option> is f (flat) or h (heirarchy)\n \
00091 where <weight> is y (weight maps) or n (use all maps)\n";
00092 exit(2);
00093 }
00094 std::map<int, Object*> list;
00095
00096
00097 Image<byte> img = density("feature1.input", list);
00098 int global_edge[list.size()][list.size()];
00099 for(uint i = 0; i < list.size(); i++)
00100 for(uint j = 0; j < list.size(); j++)
00101 global_edge[i][j] = 0;
00102
00103 int edge[list.size()][list.size()][42];
00104 for(uint i = 0; i < list.size(); i++)
00105 for(uint j = 0; j < list.size(); j++)
00106 for(uint k = 0; k < 42; k++)
00107 edge[i][j][k] = 0;
00108
00109
00110
00111 for(int features = 1; features < 42; features++)
00112 {
00113 char filename[256];
00114 sprintf(filename, "feature%d.input", features);
00115 list.clear();
00116 Image<byte> img = density(filename, list);
00117 Raster::WriteGray(img, sformat("density_%d.pgm",features));
00118
00119 Image<float> input = (Image<float>)(img);
00120 input = lowPass9(input);
00121 PixRGB<float> color(1, 0, 0);
00122 Raster::WriteRGB( stain(input, color),"input.ppm");
00123
00124
00125 int targets = countClusters(input, 0.0f);
00126 LDEBUG("# of targets = %d", targets);
00127
00128
00129 const int w = input.getWidth();
00130 const int h = input.getHeight();
00131 float min, max;
00132 getMinMax(input, min, max);
00133
00134
00135 bool flat = false, heirarchy = false;
00136 if(strcmp(argv[1],"f") == 0)
00137 {
00138 flat = true;
00139 std::cout<<"just get the members of each cluster"<<std::endl;
00140 }
00141 else if(strcmp(argv[1], "h") == 0)
00142 {
00143 heirarchy = true;
00144 std::cout<<"get the complete heirarchy"<<std::endl;
00145 }
00146 else
00147 std::cout<<"unrecognized option!";
00148
00149
00150
00151
00152 std::multimap<float, Tree*, std::greater<float> > forest;
00153
00154
00155
00156
00157
00158
00159 std::multimap<float, Point2D<int>, std::greater<float> > maxima;
00160 int lm_num = 0;
00161
00162
00163 float thresh = (min + (max - min) / 10.0);
00164
00165 for (int index = 1; index < w - 1; index ++)
00166 {
00167 float val = input.getVal(index, 0);
00168 if (val >= thresh &&
00169 val >= input.getVal(index - 1 , 0) &&
00170 val >= input.getVal(index + 1, 0))
00171 {
00172
00173 Point2D<int> p(index, 0);
00174 maxima.insert(std::pair<float, Point2D<int> >(val, p));
00175 lm_num ++;
00176 }
00177 }
00178
00179 LDEBUG(" # of local maxima = %"ZU"", maxima.size());
00180
00181 if(maxima.size() == 0)
00182 {
00183 LERROR(" there r no maxima in this image!");
00184 break;
00185 }
00186
00187 std::multimap<float, Point2D<int>, std::greater<float> >::iterator
00188 min_peak = maxima.end();
00189 min_peak --;
00190 maxima.insert(std::pair<float, Point2D<int> >(min_peak->first / 2,
00191 Point2D<int>(0, 0)));
00192
00193
00194
00195
00196
00197
00198 std::multimap<float, Point2D<int>, std::greater<float> >::iterator itr;
00199
00200 int iter = 0;
00201 Image< PixRGB<float> > cat;
00202 for( itr = maxima.begin(); itr != maxima.end(); iter++)
00203 {
00204 float thresh;
00205
00206 if(heirarchy)
00207 {
00208 thresh = itr->first;
00209 }
00210
00211 else if(flat)
00212 {
00213
00214 std::multimap<float, Point2D<int>, std::greater<float> >::iterator
00215 last = maxima.end();
00216 last --;
00217 while( itr != last)
00218 {
00219 Tree* new_peak = new Tree(itr->second, itr->first);
00220 forest.insert(std::pair<float, Tree*>(itr->first, new_peak));
00221 itr++;
00222 }
00223
00224 thresh = last->first;
00225 }
00226
00227 else break;
00228
00229 Image<float> output = highThresh( input, thresh, 255.0f);
00230
00231
00232
00233 while (itr != maxima.end() && itr->first == thresh )
00234 {
00235
00236 Tree* new_peak = new Tree(itr->second, itr->first);
00237 forest.insert(std::pair<float, Tree*>(thresh, new_peak));
00238 itr++;
00239
00240 }
00241
00242
00243
00244 cat.clear(PixRGB<float>(0.0f));
00245 int count = 0;
00246
00247
00248
00249
00250 std::multimap<float, Tree*, std::greater<float> >::iterator tree,
00251 curr_peak;
00252 for(tree = forest.begin(); tree != forest.end(); )
00253 {
00254 Tree* current = tree->second;
00255
00256 Image<float> output;
00257 Point2D<int> seed = current->node->loc;
00258 LDEBUG("current peak ======== (%d, %d)",
00259 seed.i, seed.j);
00260 flood(input, output, seed, thresh, -255.0f);
00261
00262
00263 std::vector<Tree* > to_merge;
00264 std::multimap<float, Tree*, std::greater<float> >::iterator
00265 member = forest.begin();
00266 while(member != forest.end())
00267 {
00268 for(member = forest.begin(); member != forest.end();
00269 member++)
00270 {
00271 if(output.getVal(member->second->node->loc) == -255.0f &&
00272 member->second->node->loc != seed)
00273 {
00274
00275 to_merge.push_back(member->second);
00276
00277
00278
00279 LDEBUG(" --------- (%d, %d)",
00280 member->second->node->loc.i,
00281 member->second->node->loc.j);
00282
00283
00284
00285 forest.erase(member);
00286 break;
00287 }
00288 }
00289 }
00290
00291 if(to_merge.size() > 0)
00292 {
00293
00294 to_merge.push_back(current);
00295
00296 Tree* new_tree = new Tree(seed, current->node->height);
00297 new_tree->mergeTrees(to_merge);
00298
00299
00300 float height = tree->first;
00301 forest.erase(tree);
00302
00303
00304 current = new_tree;
00305 tree = forest.insert(std::pair<float, Tree*>
00306 (height, current));
00307 tree++;
00308 }
00309
00310 else
00311 {
00312
00313 to_merge.push_back(current);
00314 tree++;
00315 }
00316
00317 std::vector<Object*> siblings;
00318
00319 for(uint l = 0; l < list.size(); l++)
00320 {
00321 if(output.getVal((int)list[l]->mu1, 0) == -255.0f)
00322 {
00323 siblings.push_back(list[l]);
00324
00325 }
00326
00327 }
00328
00329
00330 int size = siblings.size();
00331 for(int k = 0; k < size; k++)
00332 for(int l = 0; l < size; l++)
00333 {
00334 edge[siblings[k]->tag][siblings[l]->tag][features] += 1;
00335 global_edge[siblings[k]->tag][siblings[l]->tag] += 1;
00336
00337
00338
00339
00340 }
00341
00342 for(int k = 0; k < w; k ++)
00343 for(int l = 0; l < h; l ++)
00344 if(output.getVal(k,l) == -255.0f)
00345 {
00346 output.setVal(k, l, input.getVal(k, l));
00347 }
00348
00349
00350
00351
00352 Image<PixRGB<float> > cluster;
00353 if(to_merge.size() > 0)
00354 {
00355 count++;
00356
00357 float red = 0.0f, green = 0.0f, blue = 0.0f;
00358
00359 if(count % 3 == 1)
00360 {
00361 red = 1.0f ;
00362 green = 0.0f;
00363 blue = 0.0f;
00364
00365 }
00366 else if(count % 3 == 2)
00367 {
00368 green = 1.0f ;
00369 blue = 0.0f;
00370 red = 0.0f;
00371
00372 }
00373 else if(count % 3 == 0)
00374 {
00375 blue = 1.0f ;
00376 green = 0.0f;
00377 red = 0.0f;
00378
00379 }
00380
00381 PixRGB<float> color(red, green, blue);
00382
00383 cluster = stain(output, color);
00384
00385 if(cluster.initialized())
00386 Raster::WriteRGB(cluster,sformat("clusters_%d.ppm", count-1));
00387 else
00388 LDEBUG("cluster not initialized!");
00389
00390 if(!cat.initialized())
00391 cat = cluster;
00392 else
00393 cat += cluster;
00394 }
00395
00396
00397
00398 }
00399
00400 if(cat.initialized())
00401 Raster::WriteRGB((Image< PixRGB<byte> >)cat,
00402 sformat("iter_%d_%d.ppm",
00403 iter,
00404 features));
00405 else
00406 LDEBUG("iter not initialized!");
00407
00408
00409 }
00410
00411
00412 std::multimap<float, Tree*, std::greater<float> >::iterator tree;
00413 Image<PixRGB<byte> > plot = (Image< PixRGB<byte> >)cat;
00414 for(tree = forest.begin(); tree != forest.end(); tree++)
00415 tree->second->traverse(input, plot);
00416
00417 Raster::WriteRGB(plot,"plot.ppm");
00418
00419
00420
00421
00422 std::map<int, Object*>::iterator tr, jtr, start, stop;
00423 std::map<int, Object*> bak = list;
00424 for(tr = bak.begin(); tr != bak.end(); tr++)
00425 {
00426 start = tr;
00427 stop = bak.end();
00428 LINFO("%s", tr->second->name.c_str());
00429 for(jtr = tr ; jtr != stop; jtr ++)
00430 {
00431 if(edge[tr->second->tag][jtr->second->tag][features] == 1
00432 && tr != jtr)
00433 {
00434 LINFO("%s", jtr->second->name.c_str());
00435 bak.erase(jtr);
00436 }
00437 }
00438 LINFO("---------------------------");
00439 }
00440
00441 }
00442 if(strcmp(argv[2], "n") == 0)
00443 {
00444
00445 std::map<int, Object*>::iterator tr, jtr, start, stop;
00446 for(int conf = 42; conf > 0; conf -- )
00447 {
00448
00449 LDEBUG("CONFIDENCE LEVEL = %d", conf);
00450 std::map<int, Object*> bak = list;
00451 for(tr = bak.begin(); tr != bak.end(); tr++)
00452 {
00453 start = tr;
00454 stop = bak.end();
00455 LINFO("%s", tr->second->name.c_str());
00456 for(jtr = tr ; jtr != stop; jtr ++)
00457 {
00458 if(global_edge[tr->second->tag][jtr->second->tag] >= conf
00459 && tr != jtr)
00460 {
00461 LINFO("%s", jtr->second->name.c_str());
00462 bak.erase(jtr);
00463 }
00464 }
00465 LINFO("---------------------------");
00466 }
00467 LINFO("################################################");
00468 }
00469 }
00470 else if(strcmp(argv[2], "y") == 0)
00471 {
00472
00473 for(uint i = 0; i < list.size(); i++)
00474 for(uint j = 0; j < list.size(); j++)
00475 {
00476 global_edge[i][j] = 0;
00477 }
00478
00479 for(uint i = 0; i < list.size(); i++)
00480 for(uint j = 0; j < list.size(); j++)
00481 {
00482
00483 for(uint k = 0; k < 12; k++)
00484 global_edge[i][j] += edge[i][j][k];
00485 global_edge[i][j] /= 2;
00486
00487
00488 for(uint k = 12; k < 18; k++)
00489 global_edge[i][j] += edge[i][j][k];
00490
00491
00492 for(uint k = 18; k < 42; k++)
00493 global_edge[i][j] += edge[i][j][k];
00494 global_edge[i][j] /= 4;
00495 }
00496
00497
00498 std::map<int, Object*>::iterator tr, jtr, start, stop;
00499 for(int conf = 18; conf > 0; conf -- )
00500 {
00501
00502 LDEBUG("CONFIDENCE LEVEL = %d", conf);
00503 std::map<int, Object*> bak = list;
00504 for(tr = bak.begin(); tr != bak.end(); tr++)
00505 {
00506 start = tr;
00507 stop = bak.end();
00508 LINFO("%s", tr->second->name.c_str());
00509 for(jtr = tr ; jtr != stop; jtr ++)
00510 {
00511 if(global_edge[tr->second->tag][jtr->second->tag] >= conf
00512 && tr != jtr)
00513 {
00514 LINFO("%s", jtr->second->name.c_str());
00515 bak.erase(jtr);
00516 }
00517 }
00518 LINFO("---------------------------");
00519 }
00520 LINFO("################################################");
00521 }
00522 }
00523
00524 }
00525
00526
00527
00528
00529
00530
00531