int FHGraphSegmentation( const ImgBgr& img, float sigma, float c, int min_size, ImgInt *out_labels, ImgBgr *out_pseudocolors) { int width = img.Width(); int height = img.Height(); int x, y; ImgFloat R(width, height),G(width, height),B(width, height); iExtractRGBColorSpace(img, &B, &G, &R); ImgFloat smooth_R(width, height), smooth_G(width, height), smooth_B(width, height); out_labels->Reset(width, height); out_pseudocolors->Reset(width, height); iSmooth(B, sigma, &smooth_B); iSmooth(G, sigma, &smooth_G); iSmooth(R, sigma, &smooth_R); std::vector<Edge> edges; int num_edges; iBuildGraph(smooth_R, smooth_G, smooth_B, &edges, &num_edges); Universe u(width*height); iSegment_graph(width*height, num_edges, edges, c, &u); int i; for (i = 0; i < num_edges; 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); } } int num_ccs = u.num_sets(); std::vector<Bgr> colors; for (i = 0; i < width*height; i++) { Bgr color; random_rgb(&color); colors.push_back(color); } for (y = 0; y < height; y++) { for ( x = 0; x < width; x++) { int comp = u.find(y * width + x); (*out_labels)(x, y) = comp; (*out_pseudocolors)(x, y) = colors[comp]; } } return num_ccs; }
/* * Segment an image * * Returns a color image representing the segmentation. * * im: image to segment. * sigma: to smooth the image. * c: constant for treshold function. * min_size: minimum component size (enforced by post-processing stage). * num_ccs: number of connected components in the segmentation. */ 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(); image<float> *r = new image<float>(width, height); image<float> *g = new image<float>(width, height); image<float> *b = new image<float>(width, height); // smooth each color channel 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 = smooth(r, sigma); image<float> *smooth_g = smooth(g, sigma); image<float> *smooth_b = smooth(b, sigma); delete r; delete g; delete b; // build graph edge *edges = new edge[width*height*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; // segment universe *u = segment_graph(width*height, num, edges, c); // post process small components 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); // pick random colors for each component rgb *colors = new rgb[width*height]; for (int i = 0; i < width*height; i++) colors[i] = random_rgb(); 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; }