123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #ifndef SEGMENT_IMAGE
- #define SEGMENT_IMAGE
- #ifdef NICE_USELIB_OPENMP
- #include <omp.h>
- #endif
- #include <cstdlib>
- #include <segmentation/felzenszwalb/image.h>
- #include <segmentation/felzenszwalb/misc.h>
- #include <segmentation/felzenszwalb/filter.h>
- #include "segmentation/felzenszwalb/segment-graph.h"
- namespace felzenszwalb {
- static rgb random_rgb() {
- rgb c;
- double r;
- c.r = (uchar)random();
- c.g = (uchar)random();
- c.b = (uchar)random();
- return c;
- }
- static inline float diff(image<float> *r, image<float> *g, image<float> *b,
- int x1, int y1, int x2, int y2) {
- return sqrt(square(imRef(r, x1, y1) - imRef(r, x2, y2)) +
- square(imRef(g, x1, y1) - imRef(g, x2, y2)) +
- square(imRef(b, x1, y1) - imRef(b, x2, y2)));
- }
- static image<rgb> *segment_image(image<rgb> *im, float sigma, float c, int min_size,
- int *num_ccs) {
-
- int width = im->width();
- int height = im->height();
- int size = width * height;
- image<float> *r = new image<float>(width, height);
- image<float> *g = new image<float>(width, height);
- image<float> *b = new image<float>(width, height);
-
- #pragma omp parallel for
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- imRef(r, x, y) = imRef(im, x, y).r;
- imRef(g, x, y) = imRef(im, x, y).g;
- imRef(b, x, y) = imRef(im, x, y).b;
- }
- }
- image<float> *smooth_r = smooth2(r, sigma);
- image<float> *smooth_g = smooth2(g, sigma);
- image<float> *smooth_b = smooth2(b, sigma);
- delete r;
- delete g;
- delete b;
-
-
- edge *edges = new edge[size*4];
- int num = 0;
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- if (x < width - 1) {
- edges[num].a = y * width + x;
- edges[num].b = y * width + (x + 1);
- edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x + 1, y);
- num++;
- }
- if (y < height - 1) {
- edges[num].a = y * width + x;
- edges[num].b = (y + 1) * width + x;
- edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y + 1);
- num++;
- }
- if ((x < width - 1) && (y < height - 1)) {
- edges[num].a = y * width + x;
- edges[num].b = (y + 1) * width + (x + 1);
- edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x + 1, y + 1);
- num++;
- }
- if ((x < width - 1) && (y > 0)) {
- edges[num].a = y * width + x;
- edges[num].b = (y - 1) * width + (x + 1);
- edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x + 1, y - 1);
- num++;
- }
- }
- }
- delete smooth_r;
- delete smooth_g;
- delete smooth_b;
-
-
- universe *u = segment_graph(size, num, edges, c);
-
-
- #pragma omp parallel for
- for (int i = 0; i < num; i++) {
- int a = u->find(edges[i].a);
- int b = u->find(edges[i].b);
- if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size)))
- u->join(a, b);
- }
- delete [] edges;
- *num_ccs = u->num_sets();
- image<rgb> *output = new image<rgb>(width, height);
-
-
- rgb *colors = new rgb[size];
- for (int i = 0; i < size; i++)
- colors[i] = random_rgb();
- #pragma omp parallel for
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int comp = u->find(y * width + x);
- imRef(output, x, y) = colors[comp];
- }
- }
- delete [] colors;
- delete u;
-
- return output;
- }
- }
- #endif
|