2 # define MSG(x...) do{ line += sprintf(line, x); }while(0)
3 # define LINE do{ line = buf; msg(line, param); }while(0)
6 explain_signature(struct image_signature *sig, void (*msg)(byte *text, void *param), void *param)
8 byte buf[1024], *line = buf;
9 MSG("signature: flags=0x%x df=%u dh=%u f=(%u", sig->flags, sig->df, sig->dh, sig->vec.f[0]);
10 for (uns i = 1; i < IMAGE_VEC_F; i++)
11 MSG(" %u", sig->vec.f[i]);
14 for (uns j = 0; j < sig->len; j++)
16 struct image_region *reg = sig->reg + j;
17 MSG("region %u: wa=%u wb=%u f=(%u", j, reg->wa, reg->wb, reg->f[0]);
18 for (uns i = 1; i < IMAGE_VEC_F; i++)
19 MSG(" %u", reg->f[i]);
20 MSG(") h=(%u", reg->h[0]);
21 for (uns i = 1; i < IMAGE_REG_H; i++)
22 MSG(" %u", reg->h[i]);
29 # define MSG(x...) do{}while(0)
30 # define LINE do{}while(0)
33 #define MSGL(x...) do{ MSG(x); LINE; }while(0)
37 image_signatures_dist_integrated(struct image_signature *sig1, struct image_signature *sig2)
40 image_signatures_dist_integrated_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
43 uns dist[IMAGE_REG_MAX * IMAGE_REG_MAX], p[IMAGE_REG_MAX], q[IMAGE_REG_MAX];
44 uns n, i, j, k, l, s, d;
45 struct image_region *reg1, *reg2;
47 byte buf[1024], *line = buf;
48 MSGL("Integrated matching");
49 explain_signature(sig1, msg, param);
50 explain_signature(sig2, msg, param);
53 /* FIXME: do not mux textured and non-textured images (should be split in clusters tree) */
54 if ((sig1->flags ^ sig2->flags) & IMAGE_SIG_TEXTURED)
56 MSGL("Textured vs non-textured");
60 /* Compute distance matrix */
62 MSGL("Distance matrix:");
63 /* ... for non-textured images */
64 if (!((sig1->flags | sig2->flags) & IMAGE_SIG_TEXTURED))
65 for (j = 0, reg2 = sig2->reg; j < sig2->len; j++, reg2++)
66 for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
68 uns dt = 0, ds = 0, dp = 0, d;
69 for (uns i = 0; i < IMAGE_VEC_F; i++)
70 dt += image_sig_cmp_features_weights[i] * isqr((int)reg1->f[i] - (int)reg2->f[i]);
71 for (uns i = 0; i < 3; i++)
72 ds += image_sig_cmp_features_weights[IMAGE_VEC_F + i] * isqr((int)reg1->h[i] - (int)reg2->h[i]);
73 for (uns i = 3; i < 5; i++)
74 dp += image_sig_cmp_features_weights[IMAGE_VEC_F + i] * isqr((int)reg1->h[i] - (int)reg2->h[i]);
77 if (sig1->cols > sig1->rows)
80 y1 = ((int)reg1->h[4] - 64) * (int)sig1->rows / (int)sig1->cols + 64;
85 x1 = ((int)reg1->h[3] - 64) * (int)sig1->cols / (int)sig1->rows + 64;
87 if (sig2->cols > sig2->rows)
90 y2 = ((int)reg2->h[4] - 64) * (int)sig2->rows / (int)sig2->cols + 64;
95 x2 = ((int)reg2->h[3] - 64) * (int)sig2->cols / (int)sig2->rows + 64;
97 MSGL("%d %d %d %d", x1, y1, x2, y2);
98 dp = image_sig_cmp_features_weights[IMAGE_VEC_F + 3] * isqr(x1 - x2) +
99 image_sig_cmp_features_weights[IMAGE_VEC_F + 4] * isqr(y1 - y2);
102 d = dt * (4 + MIN(8, (ds >> 12))) * (4 + MIN(8, (dp >> 10))) + (ds >> 11) + (dp >> 10);
103 MSG("[%u, %u] d=%u=(%u * %u * %u + %u + %u) dt=%u ds=%u dp=%u df=(%d", i, j, d,
104 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]);
127 dist[n++] = (d << 8) + i + (j << 4);
128 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]);
130 for (uns i = 1; i < IMAGE_VEC_F; i++)
131 MSG(" %d", (int)reg1->f[i] - (int)reg2->f[i]);
132 MSG(") dh=(%d", (int)reg1->h[0] - (int)reg2->h[0]);
133 for (uns i = 1; i < IMAGE_REG_H; i++)
134 MSG(" %d", (int)reg1->h[i] - (int)reg2->h[i]);
138 /* ... for textured images (ignore shape properties) */
140 for (j = 0, reg2 = sig2->reg; j < sig2->len; j++, reg2++)
141 for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
144 for (uns i = 0; i < IMAGE_VEC_F; i++)
145 dt += image_sig_cmp_features_weights[i] * isqr((int)reg1->f[i] - (int)reg2->f[i]);
146 dist[n++] = (dt << 12) + i + (j << 4);
148 MSG("[%u, %u] dt=%u df=(%d", i, j, dt, (int)reg1->f[0] - (int)reg2->f[0]);
149 for (uns i = 1; i < IMAGE_VEC_F; i++)
150 MSG(" %d", (int)reg1->f[i] - (int)reg2->f[i]);
155 /* One or both signatures have no regions */
159 /* Get percentages */
160 for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
162 for (i = 0, reg2 = sig2->reg; i < sig2->len; i++, reg2++)
165 /* Sort entries in distance matrix */
166 image_signatures_dist_integrated_sort(dist, n);
168 /* Compute significance matrix and resulting distance */
170 MSGL("Significance matrix:");
171 for (k = 0, l = 128; l; k++)
174 j = (dist[k] >> 4) & 15;
191 reg1 = sig1->reg + i;
192 reg2 = sig2->reg + j;
193 MSG("[%u, %u] s=%u d=%u df=(%d", i, j, s, d, (int)reg1->f[0] - (int)reg2->f[0]);
194 for (uns i = 1; i < IMAGE_VEC_F; i++)
195 MSG(" %d", (int)reg1->f[i] - (int)reg2->f[i]);
196 if (!((sig1->flags | sig2->flags) & IMAGE_SIG_TEXTURED))
198 MSG(") dh=(%d", (int)reg1->h[0] - (int)reg2->h[0]);
199 for (uns i = 1; i < IMAGE_REG_H; i++)
200 MSG(" %d", (int)reg1->h[i] - (int)reg2->h[i]);
208 uns a = sig1->cols * sig2->rows;
209 uns b = sig1->rows * sig2->cols;
210 if (a < 2 * b && b < 2 * a)
212 else if (a < 4 * b && b < 4 * a)
217 a = sig1->cols * sig1->rows;
218 b = sig2->cols * sig2->rows;
220 if ((a < 1000 && b > 5000) || (b < 1000 && a > 5000))
222 else if ((a < 5000 && b > 20000) || (b < 5000 && a > 20000))
230 image_signatures_dist_fuzzy(struct image_signature *sig1, struct image_signature *sig2)
233 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 uns cnt1 = sig1->len;
251 uns cnt2 = sig2->len;
252 struct image_region *reg1 = sig1->reg;
253 struct image_region *reg2 = sig2->reg;
254 uns mf[IMAGE_REG_MAX][IMAGE_REG_MAX], mh[IMAGE_REG_MAX][IMAGE_REG_MAX];
255 uns lf[IMAGE_REG_MAX * 2], lh[IMAGE_REG_MAX * 2];
256 uns df = sig1->df + sig2->df, dh = sig1->dh + sig2->dh;
258 /* Compute distance matrix */
259 for (uns i = 0; i < cnt1; i++)
260 for (uns j = 0; j < cnt2; j++)
263 for (uns 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 (uns 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 uns lfs = 0, lhs = 0;
279 for (uns i = 0; i < cnt1; i++)
281 uns f = mf[i][0], h = mh[i][0];
282 for (uns 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 (uns i = 0; i < cnt2; i++)
294 uns f = mf[0][i], h = mh[0][i];
295 for (uns 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 uns measure = lfs * 6 + lhs * 2 * 8;
309 /* Display similarity vectors */
311 for (uns i = 0; i < cnt1 + cnt2; i++)
317 MSG("%.4f", (double)lf[i] / 0x10000);
321 for (uns 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;
340 image_signatures_dist_average(struct image_signature *sig1, struct image_signature *sig2)
343 image_signatures_dist_average_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
347 byte buf[1024], *line = buf;
348 MSGL("Average matching");
352 for (uns i = 0; i < IMAGE_VEC_F; i++)
354 uns d = image_sig_cmp_features_weights[0] * isqr((int)sig1->vec.f[i] - (int)sig2->vec.f[i]);
355 MSGL("feature %u: d=%u (%u %u)", i, d, sig1->vec.f[i], sig2->vec.f[i]);
359 MSGL("dist=%u", dist);
364 #define CALL(x) image_signatures_dist_##x(sig1, sig2)
366 image_signatures_dist(struct image_signature *sig1, struct image_signature *sig2)
368 #define CALL(x) image_signatures_dist_##x##_explain(sig1, sig2, msg, param)
370 image_signatures_dist_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
374 return CALL(average);
376 switch (image_sig_compare_method)
379 return CALL(integrated);
383 return CALL(average);