]> mj.ucw.cz Git - libucw.git/blob - images/sig-init.c
- experimenting with segmentation parameters
[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/math.h"
14 #include "lib/fastbuf.h"
15 #include "lib/conf.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->cols = (image->cols + 3) >> 2;
29   data->rows = (image->rows + 3) >> 2;
30   data->full_cols = image->cols >> 2;
31   data->full_rows = image->rows >> 2;
32   data->blocks_count = data->cols * data->rows;
33   if (data->blocks_count >= 0x10000)
34     {
35       image_thread_err(thread, IMAGE_ERR_INVALID_DIMENSIONS, "Image too large for implemented signature algorithm.");
36       return 0;
37     }
38   data->blocks = xmalloc(data->blocks_count * sizeof(struct image_sig_block));
39   data->area = image->cols * image->rows;
40   DBG("Computing signature for image of %ux%u pixels (%ux%u blocks)",
41       image->cols, image->rows, data->cols, data->rows);
42   return 1;
43 }
44
45 void
46 image_sig_preprocess(struct image_sig_data *data)
47 {
48   struct image *image = data->image;
49   struct image_sig_block *block = data->blocks;
50   uns sum[IMAGE_VEC_F];
51   bzero(sum, sizeof(sum));
52
53   /* Every block of 4x4 pixels */
54   byte *row_start = image->pixels;
55   for (uns block_y = 0; block_y < data->rows; block_y++, row_start += image->row_size * 4)
56     {
57       byte *p = row_start;
58       for (uns block_x = 0; block_x < data->cols; block_x++, p += 12, block++)
59         {
60           int t[16], s[16], *tp = t;
61           block->x = block_x;
62           block->y = block_y;
63
64           /* Convert pixels to Luv color space and compute average coefficients */
65           uns l_sum = 0, u_sum = 0, v_sum = 0;
66           byte *p2 = p;
67           if (block_x < data->full_cols && block_y < data->full_rows)
68             {
69               for (uns y = 0; y < 4; y++, p2 += image->row_size - 12)
70                 for (uns x = 0; x < 4; x++, p2 += 3)
71                   {
72                     byte luv[3];
73                     srgb_to_luv_pixel(luv, p2);
74                     l_sum += *tp++ = luv[0] / 4;
75                     u_sum += luv[1];
76                     v_sum += luv[2];
77                   }
78               block->area = 16;
79               sum[0] += l_sum;
80               sum[1] += u_sum;
81               sum[2] += v_sum;
82               block->v[0] = (l_sum >> 4);
83               block->v[1] = (u_sum >> 4);
84               block->v[2] = (v_sum >> 4);
85             }
86           /* Incomplete square near the edge */
87           else
88             {
89               uns x, y;
90               uns square_cols = (block_x < data->full_cols) ? 4 : image->cols & 3;
91               uns square_rows = (block_y < data->full_rows) ? 4 : image->rows & 3;
92               for (y = 0; y < square_rows; y++, p2 += image->row_size)
93                 {
94                   byte *p3 = p2;
95                   for (x = 0; x < square_cols; x++, p3 += 3)
96                     {
97                       byte luv[3];
98                       srgb_to_luv_pixel(luv, p3);
99                       l_sum += *tp++ = luv[0] / 4;
100                       u_sum += luv[1];
101                       v_sum += luv[2];
102                     }
103                   for (; x < 4; x++)
104                     {
105                       *tp = tp[-square_cols];
106                       tp++;
107                     }
108                 }
109               for (; y < 4; y++)
110                 for (x = 0; x < 4; x++)
111                   {
112                     *tp = tp[-square_rows * 4];
113                     tp++;
114                   }
115               block->area = square_cols * square_rows;
116               uns inv = 0x10000 / block->area;
117               sum[0] += l_sum;
118               sum[1] += u_sum;
119               sum[2] += v_sum;
120               block->v[0] = (l_sum * inv) >> 16;
121               block->v[1] = (u_sum * inv) >> 16;
122               block->v[2] = (v_sum * inv) >> 16;
123             }
124
125           /* Apply Daubechies wavelet transformation */
126
127 #         define DAUB_0 31651   /* (1 + sqrt 3) / (4 * sqrt 2) * 0x10000 */
128 #         define DAUB_1 54822   /* (3 + sqrt 3) / (4 * sqrt 2) * 0x10000 */
129 #         define DAUB_2 14689   /* (3 - sqrt 3) / (4 * sqrt 2) * 0x10000 */
130 #         define DAUB_3 -8481   /* (1 - sqrt 3) / (4 * sqrt 2) * 0x10000 */
131
132           /* ... to the rows */
133           uns i;
134           for (i = 0; i < 16; i += 4)
135             {
136               s[i + 0] = (DAUB_0 * t[i + 2] + DAUB_1 * t[i + 3] + DAUB_2 * t[i + 0] + DAUB_3 * t[i + 1]) / 0x10000;
137               s[i + 1] = (DAUB_0 * t[i + 0] + DAUB_1 * t[i + 1] + DAUB_2 * t[i + 2] + DAUB_3 * t[i + 3]) / 0x10000;
138               s[i + 2] = (DAUB_3 * t[i + 2] - DAUB_2 * t[i + 3] + DAUB_1 * t[i + 0] - DAUB_0 * t[i + 1]) / 0x10000;
139               s[i + 3] = (DAUB_3 * t[i + 0] - DAUB_2 * t[i + 1] + DAUB_1 * t[i + 2] - DAUB_0 * t[i + 3]) / 0x10000;
140             }
141
142           /* ... and to the columns... skip LL band */
143           for (i = 0; i < 2; i++)
144             {
145               t[i + 8] = (DAUB_3 * s[i + 8] - DAUB_2 * s[i +12] + DAUB_1 * s[i + 0] - DAUB_0 * s[i + 4]) / 0x10000;
146               t[i +12] = (DAUB_3 * s[i + 0] - DAUB_2 * s[i + 4] + DAUB_1 * s[i + 8] - DAUB_0 * s[i +12]) / 0x10000;
147             }
148           for (; i < 4; i++)
149             {
150               t[i + 0] = (DAUB_0 * s[i + 8] + DAUB_1 * s[i +12] + DAUB_2 * s[i + 0] + DAUB_3 * s[i + 4]) / 0x10000;
151               t[i + 4] = (DAUB_0 * s[i + 0] + DAUB_1 * s[i + 4] + DAUB_2 * s[i + 8] + DAUB_3 * s[i +12]) / 0x10000;
152               t[i + 8] = (DAUB_3 * s[i + 8] - DAUB_2 * s[i +12] + DAUB_1 * s[i + 0] - DAUB_0 * s[i + 4]) / 0x10000;
153               t[i +12] = (DAUB_3 * s[i + 0] - DAUB_2 * s[i + 4] + DAUB_1 * s[i + 8] - DAUB_0 * s[i +12]) / 0x10000;
154             }
155
156           /* Extract energies in LH, HL and HH bands */
157           block->v[3] = fast_sqrt_u16(isqr(t[8]) + isqr(t[9]) + isqr(t[12]) + isqr(t[13]));
158           block->v[4] = fast_sqrt_u16(isqr(t[2]) + isqr(t[3]) + isqr(t[6]) + isqr(t[7]));
159           block->v[5] = fast_sqrt_u16(isqr(t[10]) + isqr(t[11]) + isqr(t[14]) + isqr(t[15]));
160           sum[3] += block->v[3] * block->area;
161           sum[4] += block->v[4] * block->area;
162           sum[5] += block->v[5] * block->area;
163         }
164     }
165
166   /* Compute featrures average */
167   uns inv = 0xffffffffU / data->area;
168   for (uns i = 0; i < IMAGE_VEC_F; i++)
169     data->f[i] = ((u64)sum[i] * inv) >> 32;
170
171   if (image->cols < image_sig_min_width || image->rows < image_sig_min_height)
172     {
173       data->valid = 0;
174       data->regions_count = 0;
175     }
176   else
177     data->valid = 1;
178 }
179
180 static double image_sig_inertia_scale[3] = { 3, 1, 0.3 };
181
182 void
183 image_sig_finish(struct image_sig_data *data, struct image_signature *sig)
184 {
185   for (uns i = 0; i < IMAGE_VEC_F; i++)
186     sig->vec.f[i] = data->f[i];
187   sig->len = data->regions_count;
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) + 3) / 4;
194   uns w_mul = 127 * 256 / w_border;
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_avg = 0, y_avg = 0, w_sum = 0;
211       for (struct image_sig_block *b = r->blocks; b; b = b->next)
212         {
213           x_avg += b->x;
214           y_avg += 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 + (d - w_border) * w_mul / 256;
223         }
224       w_total += w_sum;
225       r->w_sum = w_sum;
226       x_avg /= r->count;
227       y_avg /= r->count;
228       DBG("  centroid=(%u %u)", (uns)x_avg, (uns)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 = sqrt(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, 65535);
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, 65535);
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, 65535);
243
244     }
245
246   /* Compute average differences */
247   u64 df = 0, dh = 0;
248
249   if (sig->len < 2)
250     {
251       sig->df = 1;
252       sig->dh = 1;
253     }
254   else
255     {
256       uns cnt = 0;
257       for (uns i = 0; i < sig->len; i++)
258         for (uns j = i + 1; j < sig->len; j++)
259           {
260             uns d = 0;
261             for (uns k = 0; k < IMAGE_REG_F; k++)
262               d += isqr(sig->reg[i].f[k] - sig->reg[j].f[k]);
263             df += sqrt(d);
264             d = 0;
265             for (uns k = 0; k < IMAGE_REG_H; k++)
266               d += isqr(sig->reg[i].h[k] - sig->reg[j].h[k]);
267             dh += sqrt(d);
268             cnt++;
269           }
270       sig->df = CLAMP(df / cnt, 1, 255);
271       sig->dh = CLAMP(dh / cnt, 1, 65535);
272     }
273   DBG("Average regions difs: df=%u dh=%u", sig->df, sig->dh);
274
275   /* Compute normalized weights */
276   uns wa = 128, wb = 128;
277   for (uns i = sig->len; --i > 0; )
278     {
279       struct image_sig_region *r = data->regions + i;
280       wa -= sig->reg[i].wa = CLAMP(r->count * 128 / data->blocks_count, 1, (int)(wa - i));
281       wb -= sig->reg[i].wb = CLAMP(r->w_sum * 128 / w_total, 1, (int)(wa - i));
282     }
283   sig->reg[0].wa = wa;
284   sig->reg[0].wb = wb;
285
286   /* Dump regions features */
287 #ifdef LOCAL_DEBUG
288   for (uns i = 0; i < sig->len; i++)
289     {
290       byte buf[IMAGE_REGION_DUMP_MAX];
291       image_region_dump(buf, sig->reg + i);
292       DBG("region %u: features=%s", i, buf);
293     }
294 #endif
295 }
296
297 void
298 image_sig_cleanup(struct image_sig_data *data)
299 {
300   xfree(data->blocks);
301 }
302
303 int
304 compute_image_signature(struct image_thread *thread, struct image_signature *sig, struct image *image)
305 {
306   struct image_sig_data data;
307   if (!image_sig_init(thread, &data, image))
308     return 0;
309   image_sig_preprocess(&data);
310   if (data.valid)
311     image_sig_segmentation(&data);
312   image_sig_finish(&data, sig);
313   image_sig_cleanup(&data);
314   return 1;
315 }