]> mj.ucw.cz Git - libucw.git/blob - images/sig-init.c
Merged with slices commit.
[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->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 static double image_sig_inertia_scale[3] = { 3, 1, 0.3 };
182
183 void
184 image_sig_finish(struct image_sig_data *data, struct image_signature *sig)
185 {
186   for (uns i = 0; i < IMAGE_VEC_F; i++)
187     sig->vec.f[i] = data->f[i];
188   sig->len = data->regions_count;
189   sig->flags = data->flags;
190   if (!sig->len)
191     return;
192   
193   /* For each region */
194   u64 w_total = 0;
195   uns w_border = (MIN(data->cols, data->rows) + 3) / 4;
196   uns w_mul = 127 * 256 / w_border;
197   for (uns i = 0; i < sig->len; i++)
198     {
199       struct image_sig_region *r = data->regions + i;
200       DBG("Processing region %u: count=%u", i, r->count);
201       ASSERT(r->count);
202
203       /* Copy texture properties */
204       sig->reg[i].f[0] = r->a[0];
205       sig->reg[i].f[1] = r->a[1];
206       sig->reg[i].f[2] = r->a[2];
207       sig->reg[i].f[3] = r->a[3];
208       sig->reg[i].f[4] = r->a[4];
209       sig->reg[i].f[5] = r->a[5];
210
211       /* Compute coordinates centroid and region weight */
212       u64 x_avg = 0, y_avg = 0, w_sum = 0;
213       for (struct image_sig_block *b = r->blocks; b; b = b->next)
214         {
215           x_avg += b->x;
216           y_avg += b->y;
217           uns d = b->x;
218           d = MIN(d, b->y);
219           d = MIN(d, data->cols - b->x - 1);
220           d = MIN(d, data->rows - b->y - 1);
221           if (d >= w_border)
222             w_sum += 128;
223           else
224             w_sum += 128 + (d - w_border) * w_mul / 256;
225         }
226       w_total += w_sum;
227       r->w_sum = w_sum;
228       x_avg /= r->count;
229       y_avg /= r->count;
230       DBG("  centroid=(%u %u)", (uns)x_avg, (uns)y_avg);
231
232       /* Compute normalized inertia */
233       u64 sum1 = 0, sum2 = 0, sum3 = 0;
234       for (struct image_sig_block *b = r->blocks; b; b = b->next)
235         {
236           uns inc2 = isqr(x_avg - b->x) + isqr(y_avg - b->y);
237           uns inc1 = sqrt(inc2);
238           sum1 += inc1;
239           sum2 += inc2;
240           sum3 += inc1 * inc2;
241         }
242       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);
243       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);
244       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);
245
246     }
247
248   /* Compute average differences */
249   u64 df = 0, dh = 0;
250
251   if (sig->len < 2)
252     {
253       sig->df = 1;
254       sig->dh = 1;
255     }
256   else
257     {
258       uns cnt = 0;
259       for (uns i = 0; i < sig->len; i++)
260         for (uns j = i + 1; j < sig->len; j++)
261           {
262             uns d = 0;
263             for (uns k = 0; k < IMAGE_REG_F; k++)
264               d += isqr(sig->reg[i].f[k] - sig->reg[j].f[k]);
265             df += sqrt(d);
266             d = 0;
267             for (uns k = 0; k < IMAGE_REG_H; k++)
268               d += isqr(sig->reg[i].h[k] - sig->reg[j].h[k]);
269             dh += sqrt(d);
270             cnt++;
271           }
272       sig->df = CLAMP(df / cnt, 1, 255);
273       sig->dh = CLAMP(dh / cnt, 1, 65535);
274     }
275   DBG("Average regions difs: df=%u dh=%u", sig->df, sig->dh);
276
277   /* Compute normalized weights */
278   uns wa = 128, wb = 128;
279   for (uns i = sig->len; --i > 0; )
280     {
281       struct image_sig_region *r = data->regions + i;
282       wa -= sig->reg[i].wa = CLAMP(r->count * 128 / data->blocks_count, 1, (int)(wa - i));
283       wb -= sig->reg[i].wb = CLAMP(r->w_sum * 128 / w_total, 1, (int)(wa - i));
284     }
285   sig->reg[0].wa = wa;
286   sig->reg[0].wb = wb;
287
288   /* Dump regions features */
289 #ifdef LOCAL_DEBUG
290   for (uns i = 0; i < sig->len; i++)
291     {
292       byte buf[IMAGE_REGION_DUMP_MAX];
293       image_region_dump(buf, sig->reg + i);
294       DBG("region %u: features=%s", i, buf);
295     }
296 #endif
297 }
298
299 void
300 image_sig_cleanup(struct image_sig_data *data)
301 {
302   xfree(data->blocks);
303 }
304
305 int
306 compute_image_signature(struct image_thread *thread, struct image_signature *sig, struct image *image)
307 {
308   struct image_sig_data data;
309   if (!image_sig_init(thread, &data, image))
310     return 0;
311   image_sig_preprocess(&data);
312   if (data.valid)
313     {
314       image_sig_segmentation(&data);
315       image_sig_detect_textured(&data);
316     }
317   image_sig_finish(&data, sig);
318   image_sig_cleanup(&data);
319   return 1;
320 }