segment-image-labelOutput.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef SEGMENT_IMAGE_LABELOUTPUT
  2. #define SEGMENT_IMAGE_LABELOUTPUT
  3. #include <cstdlib>
  4. #include <map>
  5. #include <limits>
  6. #include "./image.h"
  7. #include "./misc.h"
  8. #include "./filter.h"
  9. #include "./segment-graph.h"
  10. #include "./segment-image.h"
  11. #include <mex.h>
  12. /*
  13. * Segment an image
  14. *
  15. * Returns a int image giving the index of the corresponding segment for every pixel
  16. *
  17. * im: image to segment.
  18. * sigma: to smooth the image.
  19. * c: constant for treshold function.
  20. * min_size: minimum component size (enforced by post-processing stage).
  21. * num_ccs: number of connected components in the segmentation.
  22. */
  23. image<int> *segment_image_labelOutput(image<rgb> *im, float sigma, float c, int min_size,
  24. int *num_ccs) {
  25. int width = im->width();
  26. int height = im->height();
  27. image<float> *r = new image<float>(width, height);
  28. image<float> *g = new image<float>(width, height);
  29. image<float> *b = new image<float>(width, height);
  30. // smooth each color channel
  31. for (int y = 0; y < height; y++) {
  32. for (int x = 0; x < width; x++) {
  33. imRef(r, x, y) = imRef(im, x, y).r;
  34. imRef(g, x, y) = imRef(im, x, y).g;
  35. imRef(b, x, y) = imRef(im, x, y).b;
  36. }
  37. }
  38. image<float> *smooth_r = smooth(r, sigma);
  39. image<float> *smooth_g = smooth(g, sigma);
  40. image<float> *smooth_b = smooth(b, sigma);
  41. delete r;
  42. delete g;
  43. delete b;
  44. // build graph
  45. edge *edges = new edge[width*height*4];
  46. int num = 0;
  47. for (int y = 0; y < height; y++) {
  48. for (int x = 0; x < width; x++) {
  49. if (x < width-1) {
  50. // if ( num == 0)
  51. // {
  52. // mexPrintf("r: %f , b: %f g: %f\n", imRef(smooth_r, x, y), imRef(smooth_g, x, y), imRef(smooth_b, x, y));
  53. // }
  54. edges[num].a = y * width + x;
  55. edges[num].b = y * width + (x+1);
  56. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y);
  57. num++;
  58. }
  59. if (y < height-1) {
  60. edges[num].a = y * width + x;
  61. edges[num].b = (y+1) * width + x;
  62. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1);
  63. num++;
  64. }
  65. if ((x < width-1) && (y < height-1)) {
  66. edges[num].a = y * width + x;
  67. edges[num].b = (y+1) * width + (x+1);
  68. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1);
  69. num++;
  70. }
  71. if ((x < width-1) && (y > 0)) {
  72. edges[num].a = y * width + x;
  73. edges[num].b = (y-1) * width + (x+1);
  74. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1);
  75. num++;
  76. }
  77. }
  78. }
  79. delete smooth_r;
  80. delete smooth_g;
  81. delete smooth_b;
  82. // for (int i = 0; i < 5; i++) {
  83. // int a = edges[i].a;
  84. // int b = edges[i].b;
  85. // mexPrintf("components %d: %d <-> %d\n",i, a, b);
  86. // }
  87. // so far, everything is always reproducable
  88. // segment
  89. universe *u = segment_graph(width*height, num, edges, c);
  90. // // post process small components
  91. // mexPrintf("components before PP: %d\n",num);
  92. // for (int i = 0; i < 10; i++) {
  93. // int a = u->find(edges[i].a);
  94. // int b = u->find(edges[i].b);
  95. // mexPrintf("components %d: %d <-> %d\n",i, a, b);
  96. // }
  97. for (int i = 0; i < num; i++) {
  98. int a = u->find(edges[i].a);
  99. int b = u->find(edges[i].b);
  100. if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size)))
  101. u->join(a, b);
  102. }
  103. delete [] edges;
  104. //NOTE since u->num_sets() is an int, we are not able to handle more than
  105. // max_int segments ( and therefore also not imgs with more pixels)
  106. *num_ccs = u->num_sets();
  107. // mexPrintf("components after PP: %d\n",*num_ccs);
  108. image<int> *output = new image<int>(width, height);
  109. //how many different regions do we finally have, and which of them are the corresponding indices?
  110. std::map<int,int> regionLabels;
  111. int idx ( 0 );
  112. for (int y = 0; y < height; y++)
  113. {
  114. for (int x = 0; x < width; x++)
  115. {
  116. int comp = u->find(y * width + x);
  117. if ( regionLabels.find( comp ) == regionLabels.end() )
  118. {
  119. regionLabels.insert( std::pair<int,int>(comp,idx) );
  120. idx++;
  121. }
  122. }
  123. }
  124. // insert region indices into img, map indices from current ones to consecutive ones
  125. for (int y = 0; y < height; y++)
  126. {
  127. for (int x = 0; x < width; x++)
  128. {
  129. int comp = u->find(y * width + x);
  130. imRef(output, x, y) = regionLabels.find(comp)->second;
  131. }
  132. }
  133. delete u;
  134. return output;
  135. }
  136. #endif