]> mj.ucw.cz Git - libucw.git/blob - images/sig-init.c
experiment - store relative centroid position for each image region
[libucw.git] / images / sig-init.c
1 /*
2  *      Image Library -- Computation of image signatures
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include "sherlock/sherlock.h"
13 #include "lib/fastbuf.h"
14 #include "lib/conf.h"
15 #include "lib/math.h"
16 #include "images/math.h"
17 #include "images/images.h"
18 #include "images/color.h"
19 #include "images/signature.h"
20
21 #include <alloca.h>
22
23 int
24 image_sig_init(struct image_thread *thread, struct image_sig_data *data, struct image *image)
25 {
26   ASSERT((image->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB);
27   data->image = image;
28   data->flags = 0;
29   data->cols = (image->cols + 3) >> 2;
30   data->rows = (image->rows + 3) >> 2;
31   data->full_cols = image->cols >> 2;
32   data->full_rows = image->rows >> 2;
33   data->blocks_count = data->cols * data->rows;
34   if (data->blocks_count >= 0x10000)
35     {
36       image_thread_err(thread, IMAGE_ERR_INVALID_DIMENSIONS, "Image too large for implemented signature algorithm.");
37       return 0;
38     }
39   data->blocks = xmalloc(data->blocks_count * sizeof(struct image_sig_block));
40   data->area = image->cols * image->rows;
41   DBG("Computing signature for image of %ux%u pixels (%ux%u blocks)",
42       image->cols, image->rows, data->cols, data->rows);
43   return 1;
44 }
45
46 void
47 image_sig_preprocess(struct image_sig_data *data)
48 {
49   struct image *image = data->image;
50   struct image_sig_block *block = data->blocks;
51   uns sum[IMAGE_VEC_F];
52   bzero(sum, sizeof(sum));
53
54   /* Every block of 4x4 pixels */
55   byte *row_start = image->pixels;
56   for (uns block_y = 0; block_y < data->rows; block_y++, row_start += image->row_size * 4)
57     {
58       byte *p = row_start;
59       for (uns block_x = 0; block_x < data->cols; block_x++, p += 12, block++)
60         {
61           int t[16], s[16], *tp = t;
62           block->x = block_x;
63           block->y = block_y;
64
65           /* Convert pixels to Luv color space and compute average coefficients */
66           uns l_sum = 0, u_sum = 0, v_sum = 0;
67           byte *p2 = p;
68           if (block_x < data->full_cols && block_y < data->full_rows)
69             {
70               for (uns y = 0; y < 4; y++, p2 += image->row_size - 12)
71                 for (uns x = 0; x < 4; x++, p2 += 3)
72                   {
73                     byte luv[3];
74                     srgb_to_luv_pixel(luv, p2);
75                     l_sum += *tp++ = luv[0] / 4;
76                     u_sum += luv[1];
77                     v_sum += luv[2];
78                   }
79               block->area = 16;
80               sum[0] += l_sum;
81               sum[1] += u_sum;
82               sum[2] += v_sum;
83               block->v[0] = (l_sum >> 4);
84               block->v[1] = (u_sum >> 4);
85               block->v[2] = (v_sum >> 4);
86             }
87           /* Incomplete square near the edge */
88           else
89             {
90               uns x, y;
91               uns square_cols = (block_x < data->full_cols) ? 4 : image->cols & 3;
92               uns square_rows = (block_y < data->full_rows) ? 4 : image->rows & 3;
93               for (y = 0; y < square_rows; y++, p2 += image->row_size)
94                 {
95                   byte *p3 = p2;
96                   for (x = 0; x < square_cols; x++, p3 += 3)
97                     {
98                       byte luv[3];
99                       srgb_to_luv_pixel(luv, p3);
100                       l_sum += *tp++ = luv[0] / 4;
101                       u_sum += luv[1];
102                       v_sum += luv[2];
103                     }
104                   for (; x < 4; x++)
105                     {
106                       *tp = tp[-square_cols];
107                       tp++;
108                     }
109                 }
110               for (; y < 4; y++)
111                 for (x = 0; x < 4; x++)
112                   {
113                     *tp = tp[-square_rows * 4];
114                     tp++;
115                   }
116               block->area = square_cols * square_rows;
117               uns inv = 0x10000 / block->area;
118               sum[0] += l_sum;
119               sum[1] += u_sum;
120               sum[2] += v_sum;
121               block->v[0] = (l_sum * inv) >> 16;
122               block->v[1] = (u_sum * inv) >> 16;
123               block->v[2] = (v_sum * inv) >> 16;
124             }
125
126           /* Apply Daubechies wavelet transformation */
127
128 #         define DAUB_0 31651   /* (1 + sqrt 3) / (4 * sqrt 2) * 0x10000 */
129 #         define DAUB_1 54822   /* (3 + sqrt 3) / (4 * sqrt 2) * 0x10000 */
130 #         define DAUB_2 14689   /* (3 - sqrt 3) / (4 * sqrt 2) * 0x10000 */
131 #         define DAUB_3 -8481   /* (1 - sqrt 3) / (4 * sqrt 2) * 0x10000 */
132
133           /* ... to the rows */
134           uns i;
135           for (i = 0; i < 16; i += 4)
136             {
137               s[i + 0] = (DAUB_0 * t[i + 2] + DAUB_1 * t[i + 3] + DAUB_2 * t[i + 0] + DAUB_3 * t[i + 1]) / 0x10000;
138               s[i + 1] = (DAUB_0 * t[i + 0] + DAUB_1 * t[i + 1] + DAUB_2 * t[i + 2] + DAUB_3 * t[i + 3]) / 0x10000;
139               s[i + 2] = (DAUB_3 * t[i + 2] - DAUB_2 * t[i + 3] + DAUB_1 * t[i + 0] - DAUB_0 * t[i + 1]) / 0x10000;
140               s[i + 3] = (DAUB_3 * t[i + 0] - DAUB_2 * t[i + 1] + DAUB_1 * t[i + 2] - DAUB_0 * t[i + 3]) / 0x10000;
141             }
142
143           /* ... and to the columns... skip LL band */
144           for (i = 0; i < 2; i++)
145             {
146               t[i + 8] = (DAUB_3 * s[i + 8] - DAUB_2 * s[i +12] + DAUB_1 * s[i + 0] - DAUB_0 * s[i + 4]) / 0x10000;
147               t[i +12] = (DAUB_3 * s[i + 0] - DAUB_2 * s[i + 4] + DAUB_1 * s[i + 8] - DAUB_0 * s[i +12]) / 0x10000;
148             }
149           for (; i < 4; i++)
150             {
151               t[i + 0] = (DAUB_0 * s[i + 8] + DAUB_1 * s[i +12] + DAUB_2 * s[i + 0] + DAUB_3 * s[i + 4]) / 0x10000;
152               t[i + 4] = (DAUB_0 * s[i + 0] + DAUB_1 * s[i + 4] + DAUB_2 * s[i + 8] + DAUB_3 * s[i +12]) / 0x10000;
153               t[i + 8] = (DAUB_3 * s[i + 8] - DAUB_2 * s[i +12] + DAUB_1 * s[i + 0] - DAUB_0 * s[i + 4]) / 0x10000;
154               t[i +12] = (DAUB_3 * s[i + 0] - DAUB_2 * s[i + 4] + DAUB_1 * s[i + 8] - DAUB_0 * s[i +12]) / 0x10000;
155             }
156
157           /* Extract energies in LH, HL and HH bands */
158           block->v[3] = fast_sqrt_u32(isqr(t[8]) + isqr(t[9]) + isqr(t[12]) + isqr(t[13]));
159           block->v[4] = fast_sqrt_u32(isqr(t[2]) + isqr(t[3]) + isqr(t[6]) + isqr(t[7]));
160           block->v[5] = fast_sqrt_u32(isqr(t[10]) + isqr(t[11]) + isqr(t[14]) + isqr(t[15]));
161           sum[3] += block->v[3] * block->area;
162           sum[4] += block->v[4] * block->area;
163           sum[5] += block->v[5] * block->area;
164         }
165     }
166
167   /* Compute featrures average */
168   uns inv = 0xffffffffU / data->area;
169   for (uns i = 0; i < IMAGE_VEC_F; i++)
170     data->f[i] = ((u64)sum[i] * inv) >> 32;
171
172   if (image->cols < image_sig_min_width || image->rows < image_sig_min_height)
173     {
174       data->valid = 0;
175       data->regions_count = 0;
176     }
177   else
178     data->valid = 1;
179 }
180
181 void
182 image_sig_finish(struct image_sig_data *data, struct image_signature *sig)
183 {
184   for (uns i = 0; i < IMAGE_VEC_F; i++)
185     sig->vec.f[i] = data->f[i];
186   sig->len = data->regions_count;
187   sig->flags = data->flags;
188   if (!sig->len)
189     return;
190   
191   /* For each region */
192   u64 w_total = 0;
193   uns w_border = MIN(data->cols, data->rows) * image_sig_border_size;
194   int w_mul = w_border ? image_sig_border_bonus * 256 / (int)w_border : 0;
195   for (uns i = 0; i < sig->len; i++)
196     {
197       struct image_sig_region *r = data->regions + i;
198       DBG("Processing region %u: count=%u", i, r->count);
199       ASSERT(r->count);
200
201       /* Copy texture properties */
202       sig->reg[i].f[0] = r->a[0];
203       sig->reg[i].f[1] = r->a[1];
204       sig->reg[i].f[2] = r->a[2];
205       sig->reg[i].f[3] = r->a[3];
206       sig->reg[i].f[4] = r->a[4];
207       sig->reg[i].f[5] = r->a[5];
208
209       /* Compute coordinates centroid and region weight */
210       u64 x_sum = 0, y_sum = 0, w_sum = 0;
211       for (struct image_sig_block *b = r->blocks; b; b = b->next)
212         {
213           x_sum += b->x;
214           y_sum += b->y;
215           uns d = b->x;
216           d = MIN(d, b->y);
217           d = MIN(d, data->cols - b->x - 1);
218           d = MIN(d, data->rows - b->y - 1);
219           if (d >= w_border)
220             w_sum += 128;
221           else
222             w_sum += 128 + (int)(w_border - d) * w_mul / 256;
223         }
224       w_total += w_sum;
225       r->w_sum = w_sum;
226       uns x_avg = x_sum / r->count;
227       uns y_avg = y_sum / r->count;
228       DBG("  centroid=(%u %u)", x_avg, y_avg);
229
230       /* Compute normalized inertia */
231       u64 sum1 = 0, sum2 = 0, sum3 = 0;
232       for (struct image_sig_block *b = r->blocks; b; b = b->next)
233         {
234           uns inc2 = isqr(x_avg - b->x) + isqr(y_avg - b->y);
235           uns inc1 = fast_sqrt_u32(inc2);
236           sum1 += inc1;
237           sum2 += inc2;
238           sum3 += inc1 * inc2;
239         }
240       sig->reg[i].h[0] = CLAMP(image_sig_inertia_scale[0] * sum1 * ((3 * M_PI * M_PI) / 2) * pow(r->count, -1.5), 0, 255);
241       sig->reg[i].h[1] = CLAMP(image_sig_inertia_scale[1] * sum2 * ((4 * M_PI * M_PI * M_PI) / 2) / ((u64)r->count * r->count), 0, 255);
242       sig->reg[i].h[2] = CLAMP(image_sig_inertia_scale[2] * sum3 * ((5 * M_PI * M_PI * M_PI * M_PI) / 2) * pow(r->count, -2.5), 0, 255);
243       sig->reg[i].h[3] = (uns)x_avg * 127 / data->cols;
244       sig->reg[i].h[4] = (uns)y_avg * 127 / data->rows;
245     }
246
247   /* Compute average differences */
248   u64 df = 0, dh = 0;
249
250   if (sig->len < 2)
251     {
252       sig->df = 1;
253       sig->dh = 1;
254     }
255   else
256     {
257       uns cnt = 0;
258       for (uns i = 0; i < sig->len; i++)
259         for (uns j = i + 1; j < sig->len; j++)
260           {
261             uns d = 0;
262             for (uns k = 0; k < IMAGE_REG_F; k++)
263               d += image_sig_cmp_features_weights[k] * isqr(sig->reg[i].f[k] - sig->reg[j].f[k]);
264             df += fast_sqrt_u32(d);
265             d = 0;
266             for (uns k = 0; k < IMAGE_REG_H; k++)
267               d += image_sig_cmp_features_weights[k + IMAGE_REG_F] * isqr(sig->reg[i].h[k] - sig->reg[j].h[k]);
268             dh += fast_sqrt_u32(d);
269             cnt++;
270           }
271       sig->df = CLAMP(df / cnt, 1, 0xffff);
272       sig->dh = CLAMP(dh / cnt, 1, 0xffff);
273     }
274   DBG("Average regions difs: df=%u dh=%u", sig->df, sig->dh);
275
276   /* Compute normalized weights */
277   uns wa = 128, wb = 128;
278   for (uns i = sig->len; --i > 0; )
279     {
280       struct image_sig_region *r = data->regions + i;
281       wa -= sig->reg[i].wa = CLAMP(r->count * 128 / data->blocks_count, 1, (int)(wa - i));
282       wb -= sig->reg[i].wb = CLAMP(r->w_sum * 128 / w_total, 1, (int)(wb - i));
283     }
284   sig->reg[0].wa = wa;
285   sig->reg[0].wb = wb;
286
287   /* Dump regions features */
288 #ifdef LOCAL_DEBUG
289   for (uns i = 0; i < sig->len; i++)
290     {
291       byte buf[IMAGE_REGION_DUMP_MAX];
292       image_region_dump(buf, sig->reg + i);
293       DBG("region %u: features=%s", i, buf);
294     }
295 #endif
296 }
297
298 void
299 image_sig_cleanup(struct image_sig_data *data)
300 {
301   xfree(data->blocks);
302 }
303
304 int
305 compute_image_signature(struct image_thread *thread, struct image_signature *sig, struct image *image)
306 {
307   struct image_sig_data data;
308   if (!image_sig_init(thread, &data, image))
309     return 0;
310   image_sig_preprocess(&data);
311   if (data.valid)
312     {
313       image_sig_segmentation(&data);
314       image_sig_detect_textured(&data);
315     }
316   image_sig_finish(&data, sig);
317   image_sig_cleanup(&data);
318   return 1;
319 }