segment-image.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, image<float> *g, image<float> *b,
  33. int x1, int y1, int x2, int y2) {
  34. return sqrt(square(imRef(r, x1, y1)-imRef(r, x2, y2)) +
  35. square(imRef(g, x1, y1)-imRef(g, x2, y2)) +
  36. square(imRef(b, x1, y1)-imRef(b, x2, y2)));
  37. }
  38. /*
  39. * Segment an image
  40. *
  41. * Returns a color image representing the segmentation.
  42. *
  43. * im: image to segment.
  44. * sigma: to smooth the image.
  45. * c: constant for treshold function.
  46. * min_size: minimum component size (enforced by post-processing stage).
  47. * num_ccs: number of connected components in the segmentation.
  48. */
  49. image<rgb> *segment_image(image<rgb> *im, float sigma, float c, int min_size,
  50. int *num_ccs) {
  51. int width = im->width();
  52. int height = im->height();
  53. image<float> *r = new image<float>(width, height);
  54. image<float> *g = new image<float>(width, height);
  55. image<float> *b = new image<float>(width, height);
  56. // smooth each color channel
  57. for (int y = 0; y < height; y++) {
  58. for (int x = 0; x < width; x++) {
  59. imRef(r, x, y) = imRef(im, x, y).r;
  60. imRef(g, x, y) = imRef(im, x, y).g;
  61. imRef(b, x, y) = imRef(im, x, y).b;
  62. }
  63. }
  64. image<float> *smooth_r = smooth(r, sigma);
  65. image<float> *smooth_g = smooth(g, sigma);
  66. image<float> *smooth_b = smooth(b, sigma);
  67. delete r;
  68. delete g;
  69. delete b;
  70. // build graph
  71. edge *edges = new edge[width*height*4];
  72. int num = 0;
  73. for (int y = 0; y < height; y++) {
  74. for (int x = 0; x < width; x++) {
  75. if (x < width-1) {
  76. edges[num].a = y * width + x;
  77. edges[num].b = y * width + (x+1);
  78. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y);
  79. num++;
  80. }
  81. if (y < height-1) {
  82. edges[num].a = y * width + x;
  83. edges[num].b = (y+1) * width + x;
  84. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1);
  85. num++;
  86. }
  87. if ((x < width-1) && (y < height-1)) {
  88. edges[num].a = y * width + x;
  89. edges[num].b = (y+1) * width + (x+1);
  90. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1);
  91. num++;
  92. }
  93. if ((x < width-1) && (y > 0)) {
  94. edges[num].a = y * width + x;
  95. edges[num].b = (y-1) * width + (x+1);
  96. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1);
  97. num++;
  98. }
  99. }
  100. }
  101. delete smooth_r;
  102. delete smooth_g;
  103. delete smooth_b;
  104. // segment
  105. universe *u = segment_graph(width*height, num, edges, c);
  106. // post process small components
  107. for (int i = 0; i < num; i++) {
  108. int a = u->find(edges[i].a);
  109. int b = u->find(edges[i].b);
  110. if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size)))
  111. u->join(a, b);
  112. }
  113. delete [] edges;
  114. *num_ccs = u->num_sets();
  115. image<rgb> *output = new image<rgb>(width, height);
  116. // pick random colors for each component
  117. rgb *colors = new rgb[width*height];
  118. for (int i = 0; i < width*height; i++)
  119. colors[i] = random_rgb();
  120. for (int y = 0; y < height; y++) {
  121. for (int x = 0; x < width; x++) {
  122. int comp = u->find(y * width + x);
  123. imRef(output, x, y) = colors[comp];
  124. }
  125. }
  126. delete [] colors;
  127. delete u;
  128. return output;
  129. }
  130. #endif