segment-image-labelOutput.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * @brief Segment an image, returning an ushort-image giving the index of the corresponding segment for every pixel
  14. * @author Alexander Freytag
  15. * @date 27-03-2014 ( dd-mm-yyyy, last updated)
  16. *
  17. * @param[in] im: image to segment
  18. * @param[in] d_sigma: to smooth the image
  19. * @param[in] c: constant for treshold function
  20. * @param[in] i_minSize: minimum component size (enforced by post-processing stage)
  21. * @param[in] num_ccs: number of connected components in the segmentation
  22. *
  23. * @param[out] output ushort-image giving the index of the corresponding segment for every pixel
  24. */
  25. image<unsigned short> *segment_image_labelOutput( image<rgb> *im,
  26. const float d_sigma,
  27. const float c,
  28. const int i_minSize,
  29. int *num_ccs
  30. )
  31. {
  32. int width = im->width();
  33. int height = im->height();
  34. image<float> *r = new image<float>(width, height);
  35. image<float> *g = new image<float>(width, height);
  36. image<float> *b = new image<float>(width, height);
  37. // pre-processing: smooth each color channel
  38. for (int y = 0; y < height; y++)
  39. {
  40. for (int x = 0; x < width; x++)
  41. {
  42. imRef(r, x, y) = imRef(im, x, y).r;
  43. imRef(g, x, y) = imRef(im, x, y).g;
  44. imRef(b, x, y) = imRef(im, x, y).b;
  45. }
  46. }
  47. image<float> *smooth_r = smooth(r, d_sigma);
  48. image<float> *smooth_g = smooth(g, d_sigma);
  49. image<float> *smooth_b = smooth(b, d_sigma);
  50. delete r;
  51. delete g;
  52. delete b;
  53. // build graph
  54. edge *edges = new edge[width*height*4];
  55. int num ( 0 );
  56. for (int y = 0; y < height; y++)
  57. {
  58. for (int x = 0; x < width; x++)
  59. {
  60. if (x < width-1)
  61. {
  62. edges[num].a = y * width + x;
  63. edges[num].b = y * width + (x+1);
  64. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y);
  65. num++;
  66. }
  67. if (y < height-1)
  68. {
  69. edges[num].a = y * width + x;
  70. edges[num].b = (y+1) * width + x;
  71. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1);
  72. num++;
  73. }
  74. if ( (x < width-1) && (y < height-1) )
  75. {
  76. edges[num].a = y * width + x;
  77. edges[num].b = (y+1) * width + (x+1);
  78. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1);
  79. num++;
  80. }
  81. if ( (x < width-1) && (y > 0) )
  82. {
  83. edges[num].a = y * width + x;
  84. edges[num].b = (y-1) * width + (x+1);
  85. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1);
  86. num++;
  87. }
  88. }
  89. }
  90. delete smooth_r;
  91. delete smooth_g;
  92. delete smooth_b;
  93. // segment
  94. universe *u = segment_graph(width*height, num, edges, c);
  95. // post process small components
  96. for (int i = 0; i < num; i++)
  97. {
  98. int a = u->find(edges[i].a);
  99. int b = u->find(edges[i].b);
  100. if ( (a != b) && ((u->size(a) < i_minSize) || (u->size(b) < i_minSize)) )
  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. image<unsigned short> *output = new image<unsigned short>(width, height);
  108. //how many different regions do we finally have, and which of them are the corresponding indices?
  109. std::map<int,unsigned short> regionLabels;
  110. unsigned short idx ( 0 );
  111. for (int y = 0; y < height; y++)
  112. {
  113. for (int x = 0; x < width; x++)
  114. {
  115. int comp = u->find(y * width + x);
  116. if ( regionLabels.find( comp ) == regionLabels.end() )
  117. {
  118. regionLabels.insert( std::pair<int,unsigned short>(comp,idx) );
  119. idx++;
  120. }
  121. }
  122. }
  123. // insert region indices into img, map indices from current ones to consecutive ones
  124. for (int y = 0; y < height; y++)
  125. {
  126. for (int x = 0; x < width; x++)
  127. {
  128. int comp = u->find(y * width + x);
  129. imRef(output, x, y) = regionLabels.find(comp)->second;
  130. }
  131. }
  132. delete u;
  133. return output;
  134. }
  135. #endif