segment-image.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright (C) 2006 Pedro Felzenszwalb
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #ifndef SEGMENT_IMAGE
  16. #define SEGMENT_IMAGE
  17. #include <cstdlib>
  18. #include "./image.h"
  19. #include "./misc.h"
  20. #include "./filter.h"
  21. #include "./segment-graph.h"
  22. // random color
  23. rgb random_rgb(){
  24. rgb c;
  25. double r;
  26. c.r = (uchar)random();
  27. c.g = (uchar)random();
  28. c.b = (uchar)random();
  29. return c;
  30. }
  31. // dissimilarity measure between pixels
  32. static inline float diff( image<float> *r,
  33. image<float> *g,
  34. image<float> *b,
  35. int x1,
  36. int y1,
  37. int x2,
  38. int y2
  39. )
  40. {
  41. return sqrt(square(imRef(r, x1, y1)-imRef(r, x2, y2)) +
  42. square(imRef(g, x1, y1)-imRef(g, x2, y2)) +
  43. square(imRef(b, x1, y1)-imRef(b, x2, y2))
  44. );
  45. }
  46. /**
  47. * @brief Segment an image, returning a color image representing the segmentation
  48. * @author Pedro Felzenszwalb, Alexander Freytag
  49. * @date 27-03-2014 ( dd-mm-yyyy, last updated)
  50. *
  51. * @param[in] im: image to segment
  52. * @param[in] d_sigma: to smooth the image
  53. * @param[in] c: constant for treshold function
  54. * @param[in] i_minSize: minimum component size (enforced by post-processing stage)
  55. * @param[in] num_ccs: number of connected components in the segmentation
  56. *
  57. * @param[out] output rgb-image (ushort per channel) visualizing the resulting segments
  58. */
  59. image<rgb> *segment_image( image<rgb> *im,
  60. const float d_sigma,
  61. const float c,
  62. const int i_minSize,
  63. int *num_ccs
  64. )
  65. {
  66. int width = im->width();
  67. int height = im->height();
  68. image<float> *r = new image<float>(width, height);
  69. image<float> *g = new image<float>(width, height);
  70. image<float> *b = new image<float>(width, height);
  71. // smooth each color channel
  72. for (int y = 0; y < height; y++)
  73. {
  74. for (int x = 0; x < width; x++)
  75. {
  76. imRef(r, x, y) = imRef(im, x, y).r;
  77. imRef(g, x, y) = imRef(im, x, y).g;
  78. imRef(b, x, y) = imRef(im, x, y).b;
  79. }
  80. }
  81. image<float> *smooth_r = smooth(r, d_sigma);
  82. image<float> *smooth_g = smooth(g, d_sigma);
  83. image<float> *smooth_b = smooth(b, d_sigma);
  84. delete r;
  85. delete g;
  86. delete b;
  87. // build graph
  88. edge *edges = new edge[width*height*4];
  89. int num = 0;
  90. for (int y = 0; y < height; y++)
  91. {
  92. for (int x = 0; x < width; x++)
  93. {
  94. if (x < width-1)
  95. {
  96. edges[num].a = y * width + x;
  97. edges[num].b = y * width + (x+1);
  98. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y);
  99. num++;
  100. }
  101. if (y < height-1)
  102. {
  103. edges[num].a = y * width + x;
  104. edges[num].b = (y+1) * width + x;
  105. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1);
  106. num++;
  107. }
  108. if ((x < width-1) && (y < height-1))
  109. {
  110. edges[num].a = y * width + x;
  111. edges[num].b = (y+1) * width + (x+1);
  112. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1);
  113. num++;
  114. }
  115. if ((x < width-1) && (y > 0))
  116. {
  117. edges[num].a = y * width + x;
  118. edges[num].b = (y-1) * width + (x+1);
  119. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1);
  120. num++;
  121. }
  122. }
  123. }
  124. delete smooth_r;
  125. delete smooth_g;
  126. delete smooth_b;
  127. // segment
  128. universe *u = segment_graph(width*height, num, edges, c);
  129. // post process small components
  130. for (int i = 0; i < num; i++)
  131. {
  132. int a = u->find(edges[i].a);
  133. int b = u->find(edges[i].b);
  134. if ((a != b) && ((u->size(a) < i_minSize) || (u->size(b) < i_minSize)))
  135. u->join(a, b);
  136. }
  137. delete [] edges;
  138. *num_ccs = u->num_sets();
  139. image<rgb> *output = new image<rgb>(width, height);
  140. // pick random colors for each component
  141. rgb *colors = new rgb[width*height];
  142. for (int i = 0; i < width*height; i++)
  143. {
  144. colors[i] = random_rgb();
  145. }
  146. for (int y = 0; y < height; y++)
  147. {
  148. for (int x = 0; x < width; x++)
  149. {
  150. int comp = u->find(y * width + x);
  151. imRef(output, x, y) = colors[comp];
  152. }
  153. }
  154. delete [] colors;
  155. delete u;
  156. return output;
  157. }
  158. #endif