segment-image.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #ifdef NICE_USELIB_OPENMP
  18. #include <omp.h>
  19. #endif
  20. #include <cstdlib>
  21. #include <segmentation/felzenszwalb/image.h>
  22. #include <segmentation/felzenszwalb/misc.h>
  23. #include <segmentation/felzenszwalb/filter.h>
  24. #include "segmentation/felzenszwalb/segment-graph.h"
  25. namespace felzenszwalb {
  26. // random color
  27. rgb random_rgb() {
  28. rgb c;
  29. double r;
  30. c.r = (uchar)random();
  31. c.g = (uchar)random();
  32. c.b = (uchar)random();
  33. return c;
  34. }
  35. // dissimilarity measure between pixels
  36. static inline float diff(image<float> *r, image<float> *g, image<float> *b,
  37. int x1, int y1, int x2, int y2) {
  38. return sqrt(square(imRef(r, x1, y1) - imRef(r, x2, y2)) +
  39. square(imRef(g, x1, y1) - imRef(g, x2, y2)) +
  40. square(imRef(b, x1, y1) - imRef(b, x2, y2)));
  41. }
  42. /*
  43. * Segment an image
  44. *
  45. * Returns a color image representing the segmentation.
  46. *
  47. * im: image to segment.
  48. * sigma: to smooth the image.
  49. * c: constant for treshold function.
  50. * min_size: minimum component size (enforced by post-processing stage).
  51. * num_ccs: number of connected components in the segmentation.
  52. */
  53. image<rgb> *segment_image(image<rgb> *im, float sigma, float c, int min_size,
  54. int *num_ccs) {
  55. //clog << "\nsegment_image: Sprung in die Methode" << endl;
  56. int width = im->width();
  57. int height = im->height();
  58. int size = width * height;
  59. image<float> *r = new image<float>(width, height);
  60. image<float> *g = new image<float>(width, height);
  61. image<float> *b = new image<float>(width, height);
  62. // smooth each color channel
  63. #pragma omp parallel for
  64. for (int y = 0; y < height; y++) {
  65. for (int x = 0; x < width; x++) {
  66. imRef(r, x, y) = imRef(im, x, y).r;
  67. imRef(g, x, y) = imRef(im, x, y).g;
  68. imRef(b, x, y) = imRef(im, x, y).b;
  69. }
  70. }
  71. image<float> *smooth_r = smooth(r, sigma);
  72. image<float> *smooth_g = smooth(g, sigma);
  73. image<float> *smooth_b = smooth(b, sigma);
  74. delete r;
  75. delete g;
  76. delete b;
  77. // build graph
  78. //clog << "bulid graph: ... ";
  79. edge *edges = new edge[size*4];
  80. int num = 0;
  81. for (int y = 0; y < height; y++) {
  82. for (int x = 0; x < width; x++) {
  83. if (x < width - 1) {
  84. edges[num].a = y * width + x;
  85. edges[num].b = y * width + (x + 1);
  86. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x + 1, y);
  87. num++;
  88. }
  89. if (y < height - 1) {
  90. edges[num].a = y * width + x;
  91. edges[num].b = (y + 1) * width + x;
  92. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y + 1);
  93. num++;
  94. }
  95. if ((x < width - 1) && (y < height - 1)) {
  96. edges[num].a = y * width + x;
  97. edges[num].b = (y + 1) * width + (x + 1);
  98. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x + 1, y + 1);
  99. num++;
  100. }
  101. if ((x < width - 1) && (y > 0)) {
  102. edges[num].a = y * width + x;
  103. edges[num].b = (y - 1) * width + (x + 1);
  104. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x + 1, y - 1);
  105. num++;
  106. }
  107. }
  108. }
  109. // clog << "fertig" << endl;
  110. delete smooth_r;
  111. delete smooth_g;
  112. delete smooth_b;
  113. // segment
  114. //clog << "segment_graph: ... ";
  115. universe *u = segment_graph(size, num, edges, c);
  116. //clog << "fertig" << endl;
  117. // post process small components
  118. #pragma omp parallel for
  119. for (int i = 0; i < num; i++) {
  120. int a = u->find(edges[i].a);
  121. int b = u->find(edges[i].b);
  122. if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size)))
  123. u->join(a, b);
  124. }
  125. delete [] edges;
  126. *num_ccs = u->num_sets();
  127. image<rgb> *output = new image<rgb>(width, height);
  128. // pick random colors for each component
  129. //clog << "pick random colors for each component: ... ";
  130. rgb *colors = new rgb[size];
  131. for (int i = 0; i < size; i++)
  132. colors[i] = random_rgb();
  133. // clog << "/*fertig*/" << endl;
  134. #pragma omp parallel for
  135. for (int y = 0; y < height; y++) {
  136. for (int x = 0; x < width; x++) {
  137. int comp = u->find(y * width + x);
  138. imRef(output, x, y) = colors[comp];
  139. }
  140. }
  141. delete [] colors;
  142. delete u;
  143. //clog << "segment_image: Sprung aus der Methode" << endl;
  144. return output;
  145. }
  146. }//namespace
  147. #endif