Debian Codegraph @

src/filter/cluster/cluster.c

raw symbols SCIP
1 /* cluster.c
2 * Copyright (C) 2008 binarymillenium
3 * This file is a Frei0r plugin.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <stdlib.h>
21 #include <assert.h>
22 #include <math.h>
23
24 #include <stdio.h>
25
26 #include "frei0r.h"
27 #include "frei0r/math.h"
28
29 #define MAXNUM 40
30
31 struct cluster_center
32 {
33 int x;
34 int y;
35
36
37 unsigned char r;
38 unsigned char g;
39 unsigned char b;
40
41
42 /// aggregate color and positions
43 float aggr_r;
44 float aggr_g;
45 float aggr_b;
46 float aggr_x;
47 float aggr_y;
48
49 /// number of pixels in the cluster
50 float numpix;
51 };
52
53 typedef struct cluster_instance
54 {
55 unsigned int width;
56 unsigned int height;
57
58 /// number of clusters, must be smaller than maxnum
59 unsigned int num;
60 float dist_weight;
61 //float color_weight;
62
63 struct cluster_center clusters[MAXNUM];
64 } cluster_instance_t;
65
66
67 int f0r_init()
68 {
69 return 1;
70 }
71
72 void f0r_deinit()
73 { /* no initialization required */ }
74
75 void f0r_get_plugin_info(f0r_plugin_info_t* inverterInfo)
76 {
77 inverterInfo->name = "K-Means Clustering";
78 inverterInfo->author = "binarymillenium";
79 inverterInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER;
80 inverterInfo->color_model = F0R_COLOR_MODEL_RGBA8888;
81 inverterInfo->frei0r_version = FREI0R_MAJOR_VERSION;
82 inverterInfo->major_version = 0;
83 inverterInfo->minor_version = 1;
84 inverterInfo->num_params = 2;
85 inverterInfo->explanation = "Clusters of a source image by color and spatial distance";
86 }
87
88 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
89 {
90 switch(param_index)
91 {
92 case 0:
93 info->name = "Num";
94 info->type = F0R_PARAM_DOUBLE;
95 info->explanation = "The number of clusters";
96 break;
97 case 1:
98 info->name = "Dist weight";
99 info->type = F0R_PARAM_DOUBLE;
100 info->explanation = "The weight on distance";
101 break;
102 #if 0
103 case 2:
104 info->name = "Color weight";
105 info->type = F0R_PARAM_DOUBLE;
106 info->explanation = "The weight on color";
107 break;
108 #endif
109 }
110 }
111
112 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
113 {
114 cluster_instance_t* inst = (cluster_instance_t*)calloc(1, sizeof(*inst));
115
116 inst->width = width; inst->height = height;
117
118 inst->num = MAXNUM/2;
119 inst->dist_weight = 0.5;
120 //inst->color_weight = 1.0;
121
122 int k;
123 for (k = 0; k < MAXNUM; k++) {
124 struct cluster_center* cc = &inst->clusters[k];
125
126 int x = rand()%inst->width;
127 int y = rand()%inst->height;
128
129 cc->x = x;
130 cc->y = y;
131
132 /*
133 const unsigned char* src2 = (unsigned char*)(&inframe[x+inst->width*y]);
134
135 inst->clusters[k].r = src2[0];
136 inst->clusters[k].g = src2[1];
137 inst->clusters[k].b = src2[2];
138 */
139 inst->clusters[k].r = rand()%255;
140 inst->clusters[k].g = rand()%255;
141 inst->clusters[k].b = rand()%255;
142
143 cc->numpix = 0;
144 cc->aggr_x = 0;
145 cc->aggr_y = 0;
146 cc->aggr_r = 0;
147 cc->aggr_g = 0;
148 cc->aggr_b = 0;
149 }
150
151 return (f0r_instance_t)inst;
152 }
153
154 void f0r_destruct(f0r_instance_t instance)
155 {
156 free(instance);
157 }
158
159 void f0r_set_param_value(f0r_instance_t instance,
160 f0r_param_t param, int param_index)
161 {
162 assert(instance);
163 cluster_instance_t* inst = (cluster_instance_t*)instance;
164
165 switch(param_index)
166 {
167 unsigned int val;
168 float fval;
169 case 0:
170 /* val is 0-1.0 */
171 fval = ((*((double*)param) ));
172
173 val = (int) (fval*MAXNUM);
174
175 if (val > MAXNUM) val = MAXNUM;
176 if (val < 0) val = 0;
177
178 if (val != inst->num)
179 {
180 inst->num = val;
181 }
182 break;
183
184 case 1:
185 /* val is 0-1.0 */
186 //fval = 2.0 * ((*((double*)param) ) - 0.5);
187 fval = ((*((double*)param) ) );
188
189 if (fval != inst->dist_weight)
190 {
191 inst->dist_weight = fval;
192 }
193 break;
194
195 #if 0
196 case 2:
197 /* val is 0-1.0 */
198 //fval = 2.0 * ((*((double*)param) ) - 0.5);
199 fval = ((*((double*)param) ) );
200
201 if (fval != inst->color_weight)
202 {
203 inst->color_weight = fval;
204 }
205 break;
206 #endif
207 }
208 }
209
210 void f0r_get_param_value(f0r_instance_t instance,
211 f0r_param_t param, int param_index)
212 {
213 assert(instance);
214 cluster_instance_t* inst = (cluster_instance_t*)instance;
215
216 switch(param_index)
217 {
218 case 0:
219 *((double*)param) = (double) ( (inst->num) )/MAXNUM;
220 break;
221 case 1:
222 *((double*)param) = (double) ( (inst->dist_weight));
223 break;
224 }
225 }
226
227 float find_dist(int r1, int g1, int b1, int x1, int y1,
228 int r2, int g2, int b2, int x2, int y2,
229 float max_space_dist, float dist_weight) //, float color_weight)
230 {
231 /// make this a define?
232 float max_color_dist = sqrtf(255*255*3);
233
234 int dr = r1-r2;
235 int dg = g1-g2;
236 int db = b1-b2;
237
238 float color_dist = sqrtf(dr*dr + dg*dg + db*db)/max_color_dist;
239
240 int dx = x1-x2;
241 int dy = y1-y2;
242 float space_dist = sqrtf(dx*dx + dy*dy)/max_space_dist;
243
244 /// add parameter weighting later
245 //return sqrtf(color_weight*color_dist*color_dist + dist_weight*space_dist*space_dist);
246 return sqrtf((1.0-dist_weight)*color_dist*color_dist + dist_weight*space_dist*space_dist);
247 }
248
249 void f0r_update(f0r_instance_t instance, double time,
250 const uint32_t* inframe, uint32_t* outframe)
251 {
252 assert(instance);
253 cluster_instance_t* inst = (cluster_instance_t*)instance;
254
255 unsigned int x,y,k;
256
257 float max_space_dist = sqrtf(inst->width*inst->width + inst->height*inst->height);
258
259 /*
260 if (inst->has_initted) {
261 inst->has_initted = true;
262 }
263 */
264
265 for (y=0; y < inst->height; ++y) {
266 for (x=0; x < inst->width; ++x) {
267
268 const unsigned char* src2 = (unsigned char*)( &inframe[x+inst->width*y]);
269 unsigned char* dst2 = (unsigned char*)(&outframe[x+inst->width*y]);
270
271 float dist = max_space_dist;
272 int dist_ind = 0;
273
274 for (k = 0; k < inst->num; k++) {
275 struct cluster_center *cc = &inst->clusters[k];
276
277 float kdist = find_dist(src2[0], src2[1], src2[2], x,y,
278 cc->r, cc->g, cc->b, cc->x, cc->y,
279 max_space_dist, inst->dist_weight); //, inst->color_weight);
280
281 if (kdist < dist) {
282 dist = kdist;
283 dist_ind = k;
284 }
285 }
286
287 struct cluster_center* cc = &inst->clusters[dist_ind];
288 cc->aggr_x += x;
289 cc->aggr_y += y;
290 cc->aggr_r += src2[0];
291 cc->aggr_g += src2[1];
292 cc->aggr_b += src2[2];
293 cc->numpix += 1.0;
294
295 dst2[0] = cc->r;
296 dst2[1] = cc->g;
297 dst2[2] = cc->b;
298 dst2[3] = src2[3];
299 }
300 }
301
302 /// update cluster_centers
303 for (k = 0; k < inst->num; k++) {
304
305 struct cluster_center* cc = &inst->clusters[k];
306
307 if (cc->numpix > 0) {
308 cc->x = (int) (cc->aggr_x/cc->numpix);
309 cc->y = (int) (cc->aggr_y/cc->numpix);
310 cc->r = (unsigned char) (cc->aggr_r/cc->numpix);
311 cc->g = (unsigned char) (cc->aggr_g/cc->numpix);
312 cc->b = (unsigned char) (cc->aggr_b/cc->numpix);
313
314 //printf("%d, %d %d %d\t", k, (unsigned int)cc->r, (unsigned int)cc->g, (unsigned int)cc->b);
315 //printf("%g, %g %g %g\n", cc->numpix, cc->aggr_r, cc->aggr_g, cc->aggr_b);
316 }
317
318 cc->numpix = 0;
319 cc->aggr_x = 0;
320 cc->aggr_y = 0;
321 cc->aggr_r = 0;
322 cc->aggr_g = 0;
323 cc->aggr_b = 0;
324 }
325 //printf("\n");
326 }
327