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"
16 #include "images/math.h"
17 #include "images/images.h"
18 #include "images/color.h"
19 #include "images/signature.h"
24 image_sig_init(struct image_thread *thread, struct image_sig_data *data, struct image *image)
26 ASSERT((image->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB);
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)
36 image_thread_err(thread, IMAGE_ERR_INVALID_DIMENSIONS, "Image too large for implemented signature algorithm.");
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);
47 image_sig_preprocess(struct image_sig_data *data)
49 struct image *image = data->image;
50 struct image_sig_block *block = data->blocks;
52 bzero(sum, sizeof(sum));
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)
59 for (uns block_x = 0; block_x < data->cols; block_x++, p += 12, block++)
61 int t[16], s[16], *tp = t;
65 /* Convert pixels to Luv color space and compute average coefficients */
66 uns l_sum = 0, u_sum = 0, v_sum = 0;
68 if (block_x < data->full_cols && block_y < data->full_rows)
70 for (uns y = 0; y < 4; y++, p2 += image->row_size - 12)
71 for (uns x = 0; x < 4; x++, p2 += 3)
74 srgb_to_luv_pixel(luv, p2);
75 l_sum += *tp++ = luv[0] / 4;
83 block->v[0] = (l_sum >> 4);
84 block->v[1] = (u_sum >> 4);
85 block->v[2] = (v_sum >> 4);
87 /* Incomplete square near the edge */
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)
96 for (x = 0; x < square_cols; x++, p3 += 3)
99 srgb_to_luv_pixel(luv, p3);
100 l_sum += *tp++ = luv[0] / 4;
106 *tp = tp[-square_cols];
111 for (x = 0; x < 4; x++)
113 *tp = tp[-square_rows * 4];
116 block->area = square_cols * square_rows;
117 uns inv = 0x10000 / block->area;
121 block->v[0] = (l_sum * inv) >> 16;
122 block->v[1] = (u_sum * inv) >> 16;
123 block->v[2] = (v_sum * inv) >> 16;
126 /* Apply Daubechies wavelet transformation */
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 */
133 /* ... to the rows */
135 for (i = 0; i < 16; i += 4)
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;
143 /* ... and to the columns... skip LL band */
144 for (i = 0; i < 2; i++)
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;
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;
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;
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;
172 if (image->cols < image_sig_min_width || image->rows < image_sig_min_height)
175 data->regions_count = 0;
181 static double image_sig_inertia_scale[3] = { 3, 1, 0.3 };
184 image_sig_finish(struct image_sig_data *data, struct image_signature *sig)
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;
193 /* For each region */
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++)
199 struct image_sig_region *r = data->regions + i;
200 DBG("Processing region %u: count=%u", i, r->count);
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];
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)
219 d = MIN(d, data->cols - b->x - 1);
220 d = MIN(d, data->rows - b->y - 1);
224 w_sum += 128 + (d - w_border) * w_mul / 256;
230 DBG(" centroid=(%u %u)", (uns)x_avg, (uns)y_avg);
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)
236 uns inc2 = isqr(x_avg - b->x) + isqr(y_avg - b->y);
237 uns inc1 = sqrt(inc2);
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);
248 /* Compute average differences */
259 for (uns i = 0; i < sig->len; i++)
260 for (uns j = i + 1; j < sig->len; j++)
263 for (uns k = 0; k < IMAGE_REG_F; k++)
264 d += isqr(sig->reg[i].f[k] - sig->reg[j].f[k]);
267 for (uns k = 0; k < IMAGE_REG_H; k++)
268 d += isqr(sig->reg[i].h[k] - sig->reg[j].h[k]);
272 sig->df = CLAMP(df / cnt, 1, 255);
273 sig->dh = CLAMP(dh / cnt, 1, 65535);
275 DBG("Average regions difs: df=%u dh=%u", sig->df, sig->dh);
277 /* Compute normalized weights */
278 uns wa = 128, wb = 128;
279 for (uns i = sig->len; --i > 0; )
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)(wb - i));
288 /* Dump regions features */
290 for (uns i = 0; i < sig->len; i++)
292 byte buf[IMAGE_REGION_DUMP_MAX];
293 image_region_dump(buf, sig->reg + i);
294 DBG("region %u: features=%s", i, buf);
300 image_sig_cleanup(struct image_sig_data *data)
306 compute_image_signature(struct image_thread *thread, struct image_signature *sig, struct image *image)
308 struct image_sig_data data;
309 if (!image_sig_init(thread, &data, image))
311 image_sig_preprocess(&data);
314 image_sig_segmentation(&data);
315 image_sig_detect_textured(&data);
317 image_sig_finish(&data, sig);
318 image_sig_cleanup(&data);