segment-image-labelOutput.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef SEGMENT_IMAGE_LABELOUTPUT
  2. #define SEGMENT_IMAGE_LABELOUTPUT
  3. #include <cstdlib>
  4. #include "./image.h"
  5. #include "./misc.h"
  6. #include "./filter.h"
  7. #include "./segment-graph.h"
  8. #include "./segment-image.h"
  9. /*
  10. * Segment an image
  11. *
  12. * Returns a int image giving the index of the corresponding segment for every pixel
  13. *
  14. * im: image to segment.
  15. * sigma: to smooth the image.
  16. * c: constant for treshold function.
  17. * min_size: minimum component size (enforced by post-processing stage).
  18. * num_ccs: number of connected components in the segmentation.
  19. */
  20. image<int> *segment_image_labelOutput(image<rgb> *im, float sigma, float c, int min_size,
  21. int *num_ccs) {
  22. int width = im->width();
  23. int height = im->height();
  24. image<float> *r = new image<float>(width, height);
  25. image<float> *g = new image<float>(width, height);
  26. image<float> *b = new image<float>(width, height);
  27. // smooth each color channel
  28. for (int y = 0; y < height; y++) {
  29. for (int x = 0; x < width; x++) {
  30. imRef(r, x, y) = imRef(im, x, y).r;
  31. imRef(g, x, y) = imRef(im, x, y).g;
  32. imRef(b, x, y) = imRef(im, x, y).b;
  33. }
  34. }
  35. image<float> *smooth_r = smooth(r, sigma);
  36. image<float> *smooth_g = smooth(g, sigma);
  37. image<float> *smooth_b = smooth(b, sigma);
  38. delete r;
  39. delete g;
  40. delete b;
  41. // build graph
  42. edge *edges = new edge[width*height*4];
  43. int num = 0;
  44. for (int y = 0; y < height; y++) {
  45. for (int x = 0; x < width; x++) {
  46. if (x < width-1) {
  47. edges[num].a = y * width + x;
  48. edges[num].b = y * width + (x+1);
  49. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y);
  50. num++;
  51. }
  52. if (y < height-1) {
  53. edges[num].a = y * width + x;
  54. edges[num].b = (y+1) * width + x;
  55. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1);
  56. num++;
  57. }
  58. if ((x < width-1) && (y < height-1)) {
  59. edges[num].a = y * width + x;
  60. edges[num].b = (y+1) * width + (x+1);
  61. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1);
  62. num++;
  63. }
  64. if ((x < width-1) && (y > 0)) {
  65. edges[num].a = y * width + x;
  66. edges[num].b = (y-1) * width + (x+1);
  67. edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1);
  68. num++;
  69. }
  70. }
  71. }
  72. delete smooth_r;
  73. delete smooth_g;
  74. delete smooth_b;
  75. // segment
  76. universe *u = segment_graph(width*height, num, edges, c);
  77. // post process small components
  78. for (int i = 0; i < num; i++) {
  79. int a = u->find(edges[i].a);
  80. int b = u->find(edges[i].b);
  81. if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size)))
  82. u->join(a, b);
  83. }
  84. delete [] edges;
  85. *num_ccs = u->num_sets();
  86. image<int> *output = new image<int>(width, height);
  87. for (int y = 0; y < height; y++) {
  88. for (int x = 0; x < width; x++) {
  89. int comp = u->find(y * width + x);
  90. imRef(output, x, y) = comp;
  91. }
  92. }
  93. delete u;
  94. return output;
  95. }
  96. #endif