1 #ifdef CONFIG_UCW_CLEAN_ABI
2 #define image_signatures_dist ucw_image_signatures_dist
3 #define image_signatures_dist_explain ucw_image_signatures_dist_explain
7 # define MSG(x...) do{ line += sprintf(line, x); }while(0)
8 # define LINE do{ line = buf; msg(line, param); }while(0)
10 static void explain_signature(struct image_signature *sig, void (*msg)(byte *text, void *param), void *param)
12 byte buf[1024], *line = buf;
13 MSG("signature: flags=0x%x df=%u dh=%u f=(%u", sig->flags, sig->df, sig->dh, sig->vec.f[0]);
14 for (uint i = 1; i < IMAGE_VEC_F; i++)
15 MSG(" %u", sig->vec.f[i]);
18 for (uint j = 0; j < sig->len; j++)
20 struct image_region *reg = sig->reg + j;
21 MSG("region %u: wa=%u wb=%u f=(%u", j, reg->wa, reg->wb, reg->f[0]);
22 for (uint i = 1; i < IMAGE_VEC_F; i++)
23 MSG(" %u", reg->f[i]);
24 MSG(") h=(%u", reg->h[0]);
25 for (uint i = 1; i < IMAGE_REG_H; i++)
26 MSG(" %u", reg->h[i]);
33 # define MSG(x...) do{}while(0)
34 # define LINE do{}while(0)
37 #define MSGL(x...) do{ MSG(x); LINE; }while(0)
40 static uint image_signatures_dist_integrated(struct image_signature *sig1, struct image_signature *sig2)
42 static uint image_signatures_dist_integrated_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
45 uint dist[IMAGE_REG_MAX * IMAGE_REG_MAX], p[IMAGE_REG_MAX], q[IMAGE_REG_MAX];
46 uint n, i, j, k, l, s, d;
47 struct image_region *reg1, *reg2;
49 byte buf[1024], *line = buf;
50 MSGL("Integrated matching");
51 explain_signature(sig1, msg, param);
52 explain_signature(sig2, msg, param);
55 /* FIXME: do not mux textured and non-textured images (should be split in clusters tree) */
56 if ((sig1->flags ^ sig2->flags) & IMAGE_SIG_TEXTURED)
58 MSGL("Textured vs non-textured");
62 /* Compute distance matrix */
64 MSGL("Distance matrix:");
65 /* ... for non-textured images */
66 if (!((sig1->flags | sig2->flags) & IMAGE_SIG_TEXTURED))
67 for (j = 0, reg2 = sig2->reg; j < sig2->len; j++, reg2++)
68 for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
70 uint dt = 0, ds = 0, dp = 0, d;
71 for (uint i = 0; i < IMAGE_VEC_F; i++)
72 dt += image_sig_cmp_features_weights[i] * isqr((int)reg1->f[i] - (int)reg2->f[i]);
73 for (uint i = 0; i < 3; i++)
74 ds += image_sig_cmp_features_weights[IMAGE_VEC_F + i] * isqr((int)reg1->h[i] - (int)reg2->h[i]);
75 for (uint i = 3; i < 5; i++)
76 dp += image_sig_cmp_features_weights[IMAGE_VEC_F + i] * isqr((int)reg1->h[i] - (int)reg2->h[i]);
79 if (sig1->cols > sig1->rows)
82 y1 = ((int)reg1->h[4] - 64) * (int)sig1->rows / (int)sig1->cols + 64;
87 x1 = ((int)reg1->h[3] - 64) * (int)sig1->cols / (int)sig1->rows + 64;
89 if (sig2->cols > sig2->rows)
92 y2 = ((int)reg2->h[4] - 64) * (int)sig2->rows / (int)sig2->cols + 64;
97 x2 = ((int)reg2->h[3] - 64) * (int)sig2->cols / (int)sig2->rows + 64;
99 MSGL("%d %d %d %d", x1, y1, x2, y2);
100 dp = image_sig_cmp_features_weights[IMAGE_VEC_F + 3] * isqr(x1 - x2) +
101 image_sig_cmp_features_weights[IMAGE_VEC_F + 4] * isqr(y1 - y2);
104 d = dt * (4 + MIN(8, (ds >> 12))) * (4 + MIN(8, (dp >> 10))) + (ds >> 11) + (dp >> 10);
105 MSG("[%u, %u] d=%u=(%u * %u * %u + %u + %u) dt=%u ds=%u dp=%u df=(%d", i, j, d,
106 dt, 4 + MIN(8, (ds >> 12)), 4 + MIN(8, dp >> 10), ds >> 11, dp >> 10, dt, ds, dp, (int)reg1->f[0] - (int)reg2->f[0]);
129 dist[n++] = (d << 8) + i + (j << 4);
130 MSG("[%u, %u] d=%u dt=%u ds=%u dp=%u df=(%d", i, j, d, dt, ds, dp, (int)reg1->f[0] - (int)reg2->f[0]);
132 for (uint i = 1; i < IMAGE_VEC_F; i++)
133 MSG(" %d", (int)reg1->f[i] - (int)reg2->f[i]);
134 MSG(") dh=(%d", (int)reg1->h[0] - (int)reg2->h[0]);
135 for (uint i = 1; i < IMAGE_REG_H; i++)
136 MSG(" %d", (int)reg1->h[i] - (int)reg2->h[i]);
140 /* ... for textured images (ignore shape properties) */
142 for (j = 0, reg2 = sig2->reg; j < sig2->len; j++, reg2++)
143 for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
146 for (uint i = 0; i < IMAGE_VEC_F; i++)
147 dt += image_sig_cmp_features_weights[i] * isqr((int)reg1->f[i] - (int)reg2->f[i]);
148 dist[n++] = (dt << 12) + i + (j << 4);
150 MSG("[%u, %u] dt=%u df=(%d", i, j, dt, (int)reg1->f[0] - (int)reg2->f[0]);
151 for (uint i = 1; i < IMAGE_VEC_F; i++)
152 MSG(" %d", (int)reg1->f[i] - (int)reg2->f[i]);
157 /* One or both signatures have no regions */
161 /* Get percentages */
162 for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
164 for (i = 0, reg2 = sig2->reg; i < sig2->len; i++, reg2++)
167 /* Sort entries in distance matrix */
168 image_signatures_dist_integrated_sort(dist, n);
170 /* Compute significance matrix and resulting distance */
172 MSGL("Significance matrix:");
173 for (k = 0, l = 128; l; k++)
176 j = (dist[k] >> 4) & 15;
193 reg1 = sig1->reg + i;
194 reg2 = sig2->reg + j;
195 MSG("[%u, %u] s=%u d=%u df=(%d", i, j, s, d, (int)reg1->f[0] - (int)reg2->f[0]);
196 for (uint i = 1; i < IMAGE_VEC_F; i++)
197 MSG(" %d", (int)reg1->f[i] - (int)reg2->f[i]);
198 if (!((sig1->flags | sig2->flags) & IMAGE_SIG_TEXTURED))
200 MSG(") dh=(%d", (int)reg1->h[0] - (int)reg2->h[0]);
201 for (uint i = 1; i < IMAGE_REG_H; i++)
202 MSG(" %d", (int)reg1->h[i] - (int)reg2->h[i]);
210 uint a = sig1->cols * sig2->rows;
211 uint b = sig1->rows * sig2->cols;
212 if (a < 2 * b && b < 2 * a)
214 else if (a < 4 * b && b < 4 * a)
219 a = sig1->cols * sig1->rows;
220 b = sig2->cols * sig2->rows;
222 if ((a < 1000 && b > 5000) || (b < 1000 && a > 5000))
224 else if ((a < 5000 && b > 20000) || (b < 5000 && a > 20000))
231 static uint image_signatures_dist_fuzzy(struct image_signature *sig1, struct image_signature *sig2)
233 static uint image_signatures_dist_fuzzy_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
237 byte buf[1024], *line = buf;
238 MSGL("Fuzzy matching");
239 explain_signature(sig1, msg, param);
240 explain_signature(sig2, msg, param);
243 /* FIXME: do not mux textured and non-textured images (should be split in clusters tree) */
244 if ((sig1->flags ^ sig2->flags) & IMAGE_SIG_TEXTURED)
246 MSGL("Textured vs non-textured");
250 uint cnt1 = sig1->len;
251 uint cnt2 = sig2->len;
252 struct image_region *reg1 = sig1->reg;
253 struct image_region *reg2 = sig2->reg;
254 uint mf[IMAGE_REG_MAX][IMAGE_REG_MAX], mh[IMAGE_REG_MAX][IMAGE_REG_MAX];
255 uint lf[IMAGE_REG_MAX * 2], lh[IMAGE_REG_MAX * 2];
256 uint df = sig1->df + sig2->df, dh = sig1->dh + sig2->dh;
258 /* Compute distance matrix */
259 for (uint i = 0; i < cnt1; i++)
260 for (uint j = 0; j < cnt2; j++)
263 for (uint k = 0; k < IMAGE_VEC_F; k++)
265 int dif = reg1[i].f[k] - reg2[j].f[k];
266 d += image_sig_cmp_features_weights[k] * dif * dif;
270 for (uint k = 0; k < IMAGE_REG_H; k++)
272 int dif = reg1[i].h[k] - reg2[j].h[k];
273 d += image_sig_cmp_features_weights[k + IMAGE_VEC_F] * dif * dif;
278 uint lfs = 0, lhs = 0;
279 for (uint i = 0; i < cnt1; i++)
281 uint f = mf[i][0], h = mh[i][0];
282 for (uint j = 1; j < cnt2; j++)
284 f = MIN(f, mf[i][j]);
285 h = MIN(h, mh[i][j]);
287 lf[i] = (df * 0x10000) / (df + fast_sqrt_u32(f));
288 lh[i] = (dh * 0x10000) / (dh + fast_sqrt_u32(h));
289 lfs += lf[i] * (6 * reg1[i].wa + 2 * reg1[i].wb);
290 lhs += lh[i] * reg1[i].wa;
292 for (uint i = 0; i < cnt2; i++)
294 uint f = mf[0][i], h = mh[0][i];
295 for (uint j = 1; j < cnt1; j++)
297 f = MIN(f, mf[j][i]);
298 h = MIN(h, mh[j][i]);
300 lf[i + cnt1] = (df * 0x10000) / (df + fast_sqrt_u32(f));
301 lh[i + cnt1] = (dh * 0x10000) / (dh + fast_sqrt_u32(h));
302 lfs += lf[i] * (6 * reg2[i].wa + 2 * reg2[i].wb);
303 lhs += lh[i] * reg2[i].wa;
306 uint measure = lfs * 6 + lhs * 2 * 8;
309 /* Display similarity vectors */
311 for (uint i = 0; i < cnt1 + cnt2; i++)
317 MSG("%.4f", (double)lf[i] / 0x10000);
321 for (uint i = 0; i < cnt1 + cnt2; i++)
327 MSG("%.4f", (double)lh[i] / 0x10000);
330 MSGL("Lfm=%.4f", lfs / (double)(1 << (3 + 8 + 16)));
331 MSGL("Lhm=%.4f", lhs / (double)(1 << (8 + 16)));
332 MSGL("measure=%.4f", measure / (double)(1 << (3 + 3 + 8 + 16)));
335 return (1 << (3 + 3 + 8 + 16)) - measure;
339 static uint image_signatures_dist_average(struct image_signature *sig1, struct image_signature *sig2)
341 static uint image_signatures_dist_average_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
345 byte buf[1024], *line = buf;
346 MSGL("Average matching");
350 for (uint i = 0; i < IMAGE_VEC_F; i++)
352 uint d = image_sig_cmp_features_weights[0] * isqr((int)sig1->vec.f[i] - (int)sig2->vec.f[i]);
353 MSGL("feature %u: d=%u (%u %u)", i, d, sig1->vec.f[i], sig2->vec.f[i]);
357 MSGL("dist=%u", dist);
362 #define CALL(x) image_signatures_dist_##x(sig1, sig2)
363 uint image_signatures_dist(struct image_signature *sig1, struct image_signature *sig2)
365 #define CALL(x) image_signatures_dist_##x##_explain(sig1, sig2, msg, param)
366 uint image_signatures_dist_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
370 return CALL(average);
372 switch (image_sig_compare_method)
375 return CALL(integrated);
379 return CALL(average);