2 * Image Library -- Computation of image signatures
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
12 #include "sherlock/sherlock.h"
14 #include "lib/fastbuf.h"
15 #include "images/images.h"
16 #include "images/color.h"
17 #include "images/signature.h"
20 static double image_sig_inertia_scale[3] = { 3, 1, 0.3 };
23 bget_image_signature(struct fastbuf *fb, struct image_signature *sig)
25 breadb(fb, &sig->vec, sizeof(sig->vec));
27 breadb(fb, sig->reg, sig->len * sizeof(*sig->reg));
31 bput_image_signature(struct fastbuf *fb, struct image_signature *sig)
33 bwrite(fb, &sig->vec, sizeof(sig->vec));
35 bwrite(fb, sig->reg, sig->len * sizeof(*sig->reg));
39 u32 l, u, v; /* average Luv coefficients */
40 u32 lh, hl, hh; /* energies in Daubechies wavelet bands */
41 u32 x, y; /* block position */
48 u32 sum_l, sum_u, sum_v;
49 u32 sum_lh, sum_hl, sum_hh;
64 compute_k_means(struct block *blocks, uns blocks_count, struct region *regions, uns regions_count)
66 ASSERT(regions_count <= blocks_count);
67 struct block *mean[IMAGE_REG_MAX], *b, *blocks_end = blocks + blocks_count;
68 struct region *r, *regions_end = regions + regions_count;
70 /* Select means_count random blocks as initial regions pivots */
71 if (regions_count <= blocks_count - regions_count)
73 for (b = blocks; b != blocks_end; b++)
75 for (uns i = 0; i < regions_count; )
77 uns j = random_max(blocks_count);
80 b->next = mean[i++] = b;
86 for (uns i = regions_count; i; j--)
87 if (random_max(j) <= i)
88 mean[--i] = blocks + j - 1;
91 for (uns i = 0; i < regions_count; i++, r++)
102 /* Convergation cycle */
103 for (uns conv_i = 4; ; conv_i--)
105 for (r = regions; r != regions_end; r++)
107 r->sum_l = r->sum_u = r->sum_v = r->sum_lh = r->sum_hl = r->sum_hh = r->count = 0;
111 /* Find nearest regions and accumulate averages */
112 for (b = blocks; b != blocks_end; b++)
115 struct region *best_r = NULL;
116 for (r = regions; r != regions_end; r++)
131 best_r->sum_l += b->l;
132 best_r->sum_u += b->u;
133 best_r->sum_v += b->v;
134 best_r->sum_lh += b->lh;
135 best_r->sum_hl += b->hl;
136 best_r->sum_hh += b->hh;
138 b->next = best_r->blocks;
142 /* Compute new averages */
143 for (r = regions; r != regions_end; r++)
146 r->l = r->sum_l / r->count;
147 r->u = r->sum_u / r->count;
148 r->v = r->sum_v / r->count;
149 r->lh = r->sum_lh / r->count;
150 r->hl = r->sum_hl / r->count;
151 r->hh = r->sum_hh / r->count;
155 break; // FIXME: convergation criteria
158 /* Remove empty regions */
159 struct region *r2 = regions;
160 for (r = regions; r != regions_end; r++)
167 compute_image_signature(struct image_thread *thread, struct image_signature *sig, struct image *image)
169 uns cols = image->cols;
170 uns rows = image->rows;
172 /* FIXME: deal with smaller images */
173 if (cols < 4 || cols < 4)
175 image_thread_err_format(thread, IMAGE_ERR_INVALID_DIMENSIONS, "Image too small... %ux%u", cols, rows);
181 DBG("Computing signature for image %dx%d... %dx%d blocks", cols, rows, w, h);
182 uns blocks_count = w * h;
183 struct block *blocks = xmalloc(blocks_count * sizeof(struct block)), *block = blocks; /* FIXME: use mempool */
185 /* Every block of 4x4 pixels (FIXME: deal with smaller blocks near the edges) */
186 byte *p = image->pixels;
187 for (uns block_y = 0; block_y < h; block_y++, p += 3 * ((cols & 3) + cols * 3))
188 for (uns block_x = 0; block_x < w; block_x++, p -= 3 * (4 * cols - 4), block++)
193 int t[16], s[16], *tp = t;
195 /* Convert pixels to Luv color space and compute average coefficients
197 * - could be MUCH faster with precomputed tables and integer arithmetic...
198 * I will propably use interpolation in 3-dim array */
202 for (uns y = 0; y < 4; y++, p += 3 * (cols - 4))
203 for (uns x = 0; x < 4; x++, p += 3)
206 srgb_to_luv_pixel(luv, p);
207 l_sum += *tp++ = luv[0];
212 block->l = (l_sum >> 4);
213 block->u = (u_sum >> 4);
214 block->v = (v_sum >> 4);
216 /* Apply Daubechies wavelet transformation
218 * - MMX/SSE instructions or tables could be faster
219 * - maybe it would be better to compute Luv and wavelet separately because of processor cache or MMX/SSE
220 * - eliminate slow square roots
221 * - what about Haar transformation? */
223 #define DAUB_0 31651 /* (1 + sqrt 3) / (4 * sqrt 2) */
224 #define DAUB_1 54822 /* (3 + sqrt 3) / (4 * sqrt 2) */
225 #define DAUB_2 14689 /* (3 - sqrt 3) / (4 * sqrt 2) */
226 #define DAUB_3 -8481 /* (1 - sqrt 3) / (4 * sqrt 2) */
228 /* ... to the rows */
230 for (i = 0; i < 16; i += 4)
232 s[i + 0] = (DAUB_0 * t[i + 2] + DAUB_1 * t[i + 3] + DAUB_2 * t[i + 0] + DAUB_3 * t[i + 1]) / 0x10000;
233 s[i + 1] = (DAUB_0 * t[i + 0] + DAUB_1 * t[i + 1] + DAUB_2 * t[i + 2] + DAUB_3 * t[i + 3]) / 0x10000;
234 s[i + 2] = (DAUB_3 * t[i + 2] - DAUB_2 * t[i + 3] + DAUB_1 * t[i + 0] - DAUB_0 * t[i + 1]) / 0x10000;
235 s[i + 3] = (DAUB_3 * t[i + 0] - DAUB_2 * t[i + 1] + DAUB_1 * t[i + 2] - DAUB_0 * t[i + 3]) / 0x10000;
238 /* ... and to the columns... skip LL band */
239 for (i = 0; i < 2; i++)
241 t[i + 8] = (DAUB_3 * s[i + 8] - DAUB_2 * s[i +12] + DAUB_1 * s[i + 0] - DAUB_0 * s[i + 4]) / 0x1000;
242 t[i +12] = (DAUB_3 * s[i + 0] - DAUB_2 * s[i + 4] + DAUB_1 * s[i + 8] - DAUB_0 * s[i +12]) / 0x1000;
246 t[i + 0] = (DAUB_0 * s[i + 8] + DAUB_1 * s[i +12] + DAUB_2 * s[i + 0] + DAUB_3 * s[i + 4]) / 0x1000;
247 t[i + 4] = (DAUB_0 * s[i + 0] + DAUB_1 * s[i + 4] + DAUB_2 * s[i + 8] + DAUB_3 * s[i +12]) / 0x1000;
248 t[i + 8] = (DAUB_3 * s[i + 8] - DAUB_2 * s[i +12] + DAUB_1 * s[i + 0] - DAUB_0 * s[i + 4]) / 0x1000;
249 t[i +12] = (DAUB_3 * s[i + 0] - DAUB_2 * s[i + 4] + DAUB_1 * s[i + 8] - DAUB_0 * s[i +12]) / 0x1000;
252 /* Extract energies in LH, HL and HH bands */
253 block->lh = CLAMP((int)(sqrt(t[8] * t[8] + t[9] * t[9] + t[12] * t[12] + t[13] * t[13]) / 16), 0, 255);
254 block->hl = CLAMP((int)(sqrt(t[2] * t[2] + t[3] * t[3] + t[6] * t[6] + t[7] * t[7]) / 16), 0, 255);
255 block->hh = CLAMP((int)(sqrt(t[10] * t[10] + t[11] * t[11] + t[14] * t[14] + t[15] * t[15]) / 16), 0, 255);
258 /* FIXME: simple average is for testing pusposes only */
265 for (uns i = 0; i < blocks_count; i++)
267 l_sum += blocks[i].l;
268 u_sum += blocks[i].u;
269 v_sum += blocks[i].v;
270 lh_sum += blocks[i].lh;
271 hl_sum += blocks[i].hl;
272 hh_sum += blocks[i].hh;
275 sig->vec.f[0] = l_sum / blocks_count;
276 sig->vec.f[1] = u_sum / blocks_count;
277 sig->vec.f[2] = v_sum / blocks_count;
278 sig->vec.f[3] = lh_sum / blocks_count;
279 sig->vec.f[4] = hl_sum / blocks_count;
280 sig->vec.f[5] = hh_sum / blocks_count;
282 /* Quantize blocks to image regions */
283 struct region regions[IMAGE_REG_MAX];
284 sig->len = compute_k_means(blocks, blocks_count, regions, MIN(blocks_count, IMAGE_REG_MAX));
286 /* For each region */
288 uns w_border = (MIN(w, h) + 3) / 4;
289 uns w_mul = 127 * 256 / w_border;
290 for (uns i = 0; i < sig->len; i++)
292 struct region *r = regions + i;
293 DBG("Processing region %u: count=%u", i, r->count);
296 /* Copy texture properties */
297 sig->reg[i].f[0] = r->l;
298 sig->reg[i].f[1] = r->u;
299 sig->reg[i].f[2] = r->v;
300 sig->reg[i].f[3] = r->lh;
301 sig->reg[i].f[4] = r->hl;
302 sig->reg[i].f[5] = r->hh;
304 /* Compute coordinates centroid and region weight */
305 u64 x_avg = 0, y_avg = 0, w_sum = 0;
306 for (struct block *b = r->blocks; b; b = b->next)
312 d = MIN(d, w - b->x - 1);
313 d = MIN(d, h - b->y - 1);
317 w_sum += 128 + (d - w_border) * w_mul / 256;
323 DBG(" centroid=(%u %u)", (uns)x_avg, (uns)y_avg);
325 /* Compute normalized inertia */
326 u64 sum1 = 0, sum2 = 0, sum3 = 0;
327 for (struct block *b = r->blocks; b; b = b->next)
329 uns inc2 = dist(x_avg, b->x) + dist(y_avg, b->y);
330 uns inc1 = sqrt(inc2);
335 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);
336 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);
337 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);
341 /* Compute average differences */
344 for (uns i = 0; i < sig->len; i++)
345 for (uns j = i + 1; j < sig->len; j++)
348 for (uns k = 0; k < IMAGE_REG_F; k++)
349 d += dist(sig->reg[i].f[k], sig->reg[j].f[k]);
352 for (uns k = 0; k < IMAGE_REG_H; k++)
353 d += dist(sig->reg[i].h[k], sig->reg[j].h[k]);
359 DBG("Average regions difs: df=%u dh=%u", sig->df, sig->dh);
361 /* Compute normalized weights */
362 uns wa = 128, wb = 128;
363 for (uns i = sig->len; --i > 0; )
365 struct region *r = regions + i;
366 wa -= sig->reg[i].wa = CLAMP(r->count * 128 / blocks_count, 1, (int)(wa - i));
367 wb -= sig->reg[i].wb = CLAMP(r->w_sum * 128 / w_total, 1, (int)(wa - i));
372 /* Dump regions features */
374 for (uns i = 0; i < sig->len; i++)
376 byte buf[IMAGE_REGION_DUMP_MAX];
377 image_region_dump(buf, sig->reg + i);
378 DBG("region %u: features=%s", i, buf);