test_color_labeling.cpp
  1| #include <iostream>
  2| #include "camellia.h"
  3| 
  4| void cpp_example_color_labeling()
  5| {
  6|     CamImage source,YUV;
  7|     CamRLEImage encoded;
  8|     CamBlobs blobs;
  9|     CamTable clusters;
 10| 
 11|     // Load picture alfa156.bmp
 12|     source.load_bmp("resources/alfa156.bmp");
 13|     source.to_yuv(YUV);
 14| 
 15|     // Set the color clusters
 16|     const int limits[3*6]={
 17|     //  Ymin Ymax Umin Umax Vmin Vmax
 18|         0,   60,  0,   255, 0,   255, // Black
 19|         230, 255, 0,   255, 0,   255, // White
 20|         0,   255, 0,   255, 140, 255  // Red
 21|     };
 22|     clusters.set(limits,18);
 23|     const int cluster_colors[3]={CAM_RGB(0,0,0),CAM_RGB(255,255,255),CAM_RGB(255,0,0)};
 24|     
 25|     // Threshold and encode
 26|     encoded.encode_color(YUV,clusters);
 27|     std::cout<<"Number of runs : "<<encoded.nbRuns<<std::endl;
 28|     
 29|     // Labeling
 30|     encoded.labeling(blobs);
 31|     std::cout<<blobs.nbBlobs<<" blobs detected"<<std::endl;
 32| 
 33|     // Print and draw the results 
 34|     for (int i=0;i<blobs.nbBlobs;i++) {
 35|         std::cout<<"Blob #"<<i<<" : Val="<<blobs.blobInfo[i].value<< \
 36|             " ("<<blobs.blobInfo[i].left<<","<<blobs.blobInfo[i].top<<","<< \
 37|             blobs.blobInfo[i].width<<","<<blobs.blobInfo[i].height<<") Surface="<<blobs.blobInfo[i].surface<< \
 38|             std::endl;
 39|         source.draw_rectangle(blobs.blobInfo[i].left, blobs.blobInfo[i].top,
 40|             blobs.blobInfo[i].left+blobs.blobInfo[i].width-1,
 41|             blobs.blobInfo[i].top+blobs.blobInfo[i].height-1,
 42|             cluster_colors[blobs.blobInfo[i].value-1]);
 43|     }
 44| 
 45|     // Save the result
 46|     source.save_bmp("output/alfa156_color_labeling.bmp");
 47| }