[37] | 1 | /* |
---|
| 2 | Copyright (C) 2006 Pedro Felzenszwalb |
---|
| 3 | |
---|
| 4 | This program is free software; you can redistribute it and/or modify |
---|
| 5 | it under the terms of the GNU General Public License as published by |
---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | (at your option) any later version. |
---|
| 8 | |
---|
| 9 | This program is distributed in the hope that it will be useful, |
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | GNU General Public License for more details. |
---|
| 13 | |
---|
| 14 | You should have received a copy of the GNU General Public License |
---|
| 15 | along with this program; if not, write to the Free Software |
---|
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef SEGMENT_IMAGE |
---|
| 20 | #define SEGMENT_IMAGE |
---|
| 21 | |
---|
| 22 | #include <stdio.h> |
---|
| 23 | #include <cstdlib> |
---|
| 24 | #include <cstring> |
---|
| 25 | #include "image.h" |
---|
| 26 | #include "misc.h" |
---|
| 27 | #include "pnmfile.h" |
---|
| 28 | #include "filter.h" |
---|
| 29 | #include "segment-graph.h" |
---|
| 30 | |
---|
| 31 | // random color |
---|
| 32 | rgb random_rgb(){ |
---|
| 33 | rgb c; |
---|
| 34 | double r; |
---|
| 35 | |
---|
| 36 | c.r = (uchar)random(); |
---|
| 37 | c.g = (uchar)random(); |
---|
| 38 | c.b = (uchar)random(); |
---|
| 39 | |
---|
| 40 | return c; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | // dissimilarity measure between pixels |
---|
| 44 | static inline float diff(image<float> *r, image<float> *g, image<float> *b, |
---|
| 45 | int x1, int y1, int x2, int y2) { |
---|
| 46 | return sqrt(square(imRef(r, x1, y1)-imRef(r, x2, y2)) + |
---|
| 47 | square(imRef(g, x1, y1)-imRef(g, x2, y2)) + |
---|
| 48 | square(imRef(b, x1, y1)-imRef(b, x2, y2))); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /* |
---|
| 52 | * Segment an image |
---|
| 53 | * |
---|
| 54 | * Returns a color image representing the segmentation. |
---|
| 55 | * |
---|
| 56 | * im: image to segment. |
---|
| 57 | * sigma: to smooth the image. |
---|
| 58 | * c: constant for treshold function. |
---|
| 59 | * min_size: minimum component size (enforced by post-processing stage). |
---|
| 60 | * num_ccs: number of connected components in the segmentation. |
---|
| 61 | */ |
---|
| 62 | /*image<rgb> *segment_image(image<rgb> *im, int SegOut[], int height, int width, float sigma, float c, int min_size, |
---|
| 63 | int *num_ccs) {*/ |
---|
| 64 | /*void segment_image(image<rgb> *im, double SegOut[], int height, int width, float sigma, float c, int min_size |
---|
| 65 | ) {*/ |
---|
| 66 | /*void segment_image( image<unsigned char> *r, image<unsigned char> *g, image<unsigned char> *b, double SegOut[], int height, int width, float sigma, float c, int min_size |
---|
| 67 | ) {*/ |
---|
| 68 | image<rgb> * segment_image( unsigned char *Imptr, double SegOut[], int height, int width, float sigma, float c, int min_size, double *PpmOption |
---|
| 69 | ) { |
---|
| 70 | /*image<rgb> *segment_image( image<float> *r, image<float> *g, image<float> *b, int height, int width, float sigma, float c, int min_size,*/ |
---|
| 71 | /* int width = im->width(); |
---|
| 72 | int height = im->height();*/ |
---|
| 73 | |
---|
| 74 | image<float> *r = new image<float>(width, height); |
---|
| 75 | image<float> *g = new image<float>(width, height); |
---|
| 76 | image<float> *b = new image<float>(width, height); |
---|
| 77 | |
---|
| 78 | // smooth each color channel |
---|
| 79 | int Area = width* height; |
---|
| 80 | for (int x = 0; x < width; x++) { |
---|
| 81 | int HightTimesX = height*x; |
---|
| 82 | for (int y = 0; y < height; y++) { |
---|
| 83 | int temp = HightTimesX+y; |
---|
| 84 | imRef(r, x, y) = Imptr[temp]; |
---|
| 85 | temp += Area; |
---|
| 86 | imRef(g, x, y) = Imptr[temp]; |
---|
| 87 | temp += Area; |
---|
| 88 | imRef(b, x, y) = Imptr[temp]; |
---|
| 89 | |
---|
| 90 | /* imRef(g, x, y) = Imptr[temp + (Area * 1 -1)];; |
---|
| 91 | imRef(b, x, y) = Imptr[temp + (Area * 2 -1)];;*/ |
---|
| 92 | /* imRef(r, x, y) = imRef(im, x, y).r; |
---|
| 93 | imRef(g, x, y) = imRef(im, x, y).g; |
---|
| 94 | imRef(b, x, y) = imRef(im, x, y).b;*/ |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | image<float> *smooth_r = smooth(r, sigma); |
---|
| 98 | image<float> *smooth_g = smooth(g, sigma); |
---|
| 99 | image<float> *smooth_b = smooth(b, sigma); |
---|
| 100 | delete r; |
---|
| 101 | delete g; |
---|
| 102 | delete b; |
---|
| 103 | |
---|
| 104 | // build graph |
---|
| 105 | edge *edges = new edge[width*height*4]; |
---|
| 106 | int num = 0; |
---|
| 107 | for (int y = 0; y < height; y++) { |
---|
| 108 | int YtimesWidth = y * width; |
---|
| 109 | for (int x = 0; x < width; x++) { |
---|
| 110 | if (x < width-1) { |
---|
| 111 | edges[num].a = YtimesWidth + x; |
---|
| 112 | edges[num].b = YtimesWidth + (x+1); |
---|
| 113 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y); |
---|
| 114 | num++; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if (y < height-1) { |
---|
| 118 | edges[num].a = YtimesWidth + x; |
---|
| 119 | edges[num].b = YtimesWidth + width + x; |
---|
| 120 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1); |
---|
| 121 | num++; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if ((x < width-1) && (y < height-1)) { |
---|
| 125 | edges[num].a = YtimesWidth + x; |
---|
| 126 | edges[num].b = YtimesWidth + width + (x+1); |
---|
| 127 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1); |
---|
| 128 | num++; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if ((x < width-1) && (y > 0)) { |
---|
| 132 | edges[num].a = YtimesWidth + x; |
---|
| 133 | edges[num].b = YtimesWidth - width + (x+1); |
---|
| 134 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1); |
---|
| 135 | num++; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | delete smooth_r; |
---|
| 140 | delete smooth_g; |
---|
| 141 | delete smooth_b; |
---|
| 142 | |
---|
| 143 | // segment |
---|
| 144 | universe *u = segment_graph(width*height, num, edges, c); |
---|
| 145 | |
---|
| 146 | // post process small components |
---|
| 147 | for (int i = 0; i < num; i++) { |
---|
| 148 | int a = u->find(edges[i].a); |
---|
| 149 | int b = u->find(edges[i].b); |
---|
| 150 | if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size))) |
---|
| 151 | u->join(a, b); |
---|
| 152 | } |
---|
| 153 | delete [] edges; |
---|
| 154 | |
---|
| 155 | image<rgb> *output = new image<rgb>(width, height); |
---|
| 156 | rgb *colors = new rgb[width*height]; |
---|
| 157 | /* image<int> *output = new image<int>(width, height);*/ |
---|
| 158 | |
---|
| 159 | // pick random colors for each component |
---|
| 160 | if (*PpmOption){ |
---|
| 161 | for (int i = 0; i < width*height; i++) |
---|
| 162 | colors[i] = random_rgb(); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | for (int y = 0; y < height-1; y++) { |
---|
| 167 | int YtimesWidth = y * width; |
---|
| 168 | int temp = y; |
---|
| 169 | for (int x = 0; x < width-1; x++) { |
---|
| 170 | int comp = u->find(YtimesWidth + x); |
---|
| 171 | if (*PpmOption){ |
---|
| 172 | imRef(output, x, y) = colors[comp]; |
---|
| 173 | } |
---|
| 174 | SegOut[ temp] = (double) comp; |
---|
| 175 | temp += height; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | delete u; |
---|
| 180 | |
---|
| 181 | return output; |
---|
| 182 | /* return;*/ |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | #endif |
---|