2 # define MSG(x...) do{ line += sprintf(line, x); }while(0)
3 # define LINE do{ line = buf; msg(line, param); }while(0)
5 static void explain_signature(struct image_signature *sig, void (*msg)(byte *text, void *param), void *param)
7 byte buf[1024], *line = buf;
8 MSG("signature: flags=0x%x df=%u dh=%u f=(%u", sig->flags, sig->df, sig->dh, sig->vec.f[0]);
9 for (uns i = 1; i < IMAGE_VEC_F; i++)
10 MSG(" %u", sig->vec.f[i]);
13 for (uns j = 0; j < sig->len; j++)
15 struct image_region *reg = sig->reg + j;
16 MSG("region %u: wa=%u wb=%u f=(%u", j, reg->wa, reg->wb, reg->f[0]);
17 for (uns i = 1; i < IMAGE_VEC_F; i++)
18 MSG(" %u", reg->f[i]);
19 MSG(") h=(%u", reg->h[0]);
20 for (uns i = 1; i < IMAGE_REG_H; i++)
21 MSG(" %u", reg->h[i]);
28 # define MSG(x...) do{}while(0)
29 # define LINE do{}while(0)
32 #define MSGL(x...) do{ MSG(x); LINE; }while(0)
35 static uns image_signatures_dist_integrated(struct image_signature *sig1, struct image_signature *sig2)
37 static uns image_signatures_dist_integrated_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
40 uns dist[IMAGE_REG_MAX * IMAGE_REG_MAX], p[IMAGE_REG_MAX], q[IMAGE_REG_MAX];
41 uns n, i, j, k, l, s, d;
42 struct image_region *reg1, *reg2;
44 byte buf[1024], *line = buf;
45 MSGL("Integrated matching");
46 explain_signature(sig1, msg, param);
47 explain_signature(sig2, msg, param);
50 /* FIXME: do not mux textured and non-textured images (should be split in clusters tree) */
51 if ((sig1->flags ^ sig2->flags) & IMAGE_SIG_TEXTURED)
53 MSGL("Textured vs non-textured");
57 /* Compute distance matrix */
59 MSGL("Distance matrix:");
60 /* ... for non-textured images */
61 if (!((sig1->flags | sig2->flags) & IMAGE_SIG_TEXTURED))
62 for (j = 0, reg2 = sig2->reg; j < sig2->len; j++, reg2++)
63 for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
65 uns dt = 0, ds = 0, dp = 0, d;
66 for (uns i = 0; i < IMAGE_VEC_F; i++)
67 dt += image_sig_cmp_features_weights[i] * isqr((int)reg1->f[i] - (int)reg2->f[i]);
68 for (uns i = 0; i < 3; i++)
69 ds += image_sig_cmp_features_weights[IMAGE_VEC_F + i] * isqr((int)reg1->h[i] - (int)reg2->h[i]);
70 for (uns i = 3; i < 5; i++)
71 dp += image_sig_cmp_features_weights[IMAGE_VEC_F + i] * isqr((int)reg1->h[i] - (int)reg2->h[i]);
74 if (sig1->cols > sig1->rows)
77 y1 = ((int)reg1->h[4] - 64) * (int)sig1->rows / (int)sig1->cols + 64;
82 x1 = ((int)reg1->h[3] - 64) * (int)sig1->cols / (int)sig1->rows + 64;
84 if (sig2->cols > sig2->rows)
87 y2 = ((int)reg2->h[4] - 64) * (int)sig2->rows / (int)sig2->cols + 64;
92 x2 = ((int)reg2->h[3] - 64) * (int)sig2->cols / (int)sig2->rows + 64;
94 MSGL("%d %d %d %d", x1, y1, x2, y2);
95 dp = image_sig_cmp_features_weights[IMAGE_VEC_F + 3] * isqr(x1 - x2) +
96 image_sig_cmp_features_weights[IMAGE_VEC_F + 4] * isqr(y1 - y2);
99 d = dt * (4 + MIN(8, (ds >> 12))) * (4 + MIN(8, (dp >> 10))) + (ds >> 11) + (dp >> 10);
100 MSG("[%u, %u] d=%u=(%u * %u * %u + %u + %u) dt=%u ds=%u dp=%u df=(%d", i, j, d,
101 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]);
124 dist[n++] = (d << 8) + i + (j << 4);
125 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]);
127 for (uns i = 1; i < IMAGE_VEC_F; i++)
128 MSG(" %d", (int)reg1->f[i] - (int)reg2->f[i]);
129 MSG(") dh=(%d", (int)reg1->h[0] - (int)reg2->h[0]);
130 for (uns i = 1; i < IMAGE_REG_H; i++)
131 MSG(" %d", (int)reg1->h[i] - (int)reg2->h[i]);
135 /* ... for textured images (ignore shape properties) */
137 for (j = 0, reg2 = sig2->reg; j < sig2->len; j++, reg2++)
138 for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
141 for (uns i = 0; i < IMAGE_VEC_F; i++)
142 dt += image_sig_cmp_features_weights[i] * isqr((int)reg1->f[i] - (int)reg2->f[i]);
143 dist[n++] = (dt << 12) + i + (j << 4);
145 MSG("[%u, %u] dt=%u df=(%d", i, j, dt, (int)reg1->f[0] - (int)reg2->f[0]);
146 for (uns i = 1; i < IMAGE_VEC_F; i++)
147 MSG(" %d", (int)reg1->f[i] - (int)reg2->f[i]);
152 /* One or both signatures have no regions */
156 /* Get percentages */
157 for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
159 for (i = 0, reg2 = sig2->reg; i < sig2->len; i++, reg2++)
162 /* Sort entries in distance matrix */
163 image_signatures_dist_integrated_sort(dist, n);
165 /* Compute significance matrix and resulting distance */
167 MSGL("Significance matrix:");
168 for (k = 0, l = 128; l; k++)
171 j = (dist[k] >> 4) & 15;
188 reg1 = sig1->reg + i;
189 reg2 = sig2->reg + j;
190 MSG("[%u, %u] s=%u d=%u df=(%d", i, j, s, d, (int)reg1->f[0] - (int)reg2->f[0]);
191 for (uns i = 1; i < IMAGE_VEC_F; i++)
192 MSG(" %d", (int)reg1->f[i] - (int)reg2->f[i]);
193 if (!((sig1->flags | sig2->flags) & IMAGE_SIG_TEXTURED))
195 MSG(") dh=(%d", (int)reg1->h[0] - (int)reg2->h[0]);
196 for (uns i = 1; i < IMAGE_REG_H; i++)
197 MSG(" %d", (int)reg1->h[i] - (int)reg2->h[i]);
205 uns a = sig1->cols * sig2->rows;
206 uns b = sig1->rows * sig2->cols;
207 if (a < 2 * b && b < 2 * a)
209 else if (a < 4 * b && b < 4 * a)
214 a = sig1->cols * sig1->rows;
215 b = sig2->cols * sig2->rows;
217 if ((a < 1000 && b > 5000) || (b < 1000 && a > 5000))
219 else if ((a < 5000 && b > 20000) || (b < 5000 && a > 20000))
226 static uns image_signatures_dist_fuzzy(struct image_signature *sig1, struct image_signature *sig2)
228 static uns image_signatures_dist_fuzzy_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
232 byte buf[1024], *line = buf;
233 MSGL("Fuzzy matching");
234 explain_signature(sig1, msg, param);
235 explain_signature(sig2, msg, param);
238 /* FIXME: do not mux textured and non-textured images (should be split in clusters tree) */
239 if ((sig1->flags ^ sig2->flags) & IMAGE_SIG_TEXTURED)
241 MSGL("Textured vs non-textured");
245 uns cnt1 = sig1->len;
246 uns cnt2 = sig2->len;
247 struct image_region *reg1 = sig1->reg;
248 struct image_region *reg2 = sig2->reg;
249 uns mf[IMAGE_REG_MAX][IMAGE_REG_MAX], mh[IMAGE_REG_MAX][IMAGE_REG_MAX];
250 uns lf[IMAGE_REG_MAX * 2], lh[IMAGE_REG_MAX * 2];
251 uns df = sig1->df + sig2->df, dh = sig1->dh + sig2->dh;
253 /* Compute distance matrix */
254 for (uns i = 0; i < cnt1; i++)
255 for (uns j = 0; j < cnt2; j++)
258 for (uns k = 0; k < IMAGE_VEC_F; k++)
260 int dif = reg1[i].f[k] - reg2[j].f[k];
261 d += image_sig_cmp_features_weights[k] * dif * dif;
265 for (uns k = 0; k < IMAGE_REG_H; k++)
267 int dif = reg1[i].h[k] - reg2[j].h[k];
268 d += image_sig_cmp_features_weights[k + IMAGE_VEC_F] * dif * dif;
273 uns lfs = 0, lhs = 0;
274 for (uns i = 0; i < cnt1; i++)
276 uns f = mf[i][0], h = mh[i][0];
277 for (uns j = 1; j < cnt2; j++)
279 f = MIN(f, mf[i][j]);
280 h = MIN(h, mh[i][j]);
282 lf[i] = (df * 0x10000) / (df + fast_sqrt_u32(f));
283 lh[i] = (dh * 0x10000) / (dh + fast_sqrt_u32(h));
284 lfs += lf[i] * (6 * reg1[i].wa + 2 * reg1[i].wb);
285 lhs += lh[i] * reg1[i].wa;
287 for (uns i = 0; i < cnt2; i++)
289 uns f = mf[0][i], h = mh[0][i];
290 for (uns j = 1; j < cnt1; j++)
292 f = MIN(f, mf[j][i]);
293 h = MIN(h, mh[j][i]);
295 lf[i + cnt1] = (df * 0x10000) / (df + fast_sqrt_u32(f));
296 lh[i + cnt1] = (dh * 0x10000) / (dh + fast_sqrt_u32(h));
297 lfs += lf[i] * (6 * reg2[i].wa + 2 * reg2[i].wb);
298 lhs += lh[i] * reg2[i].wa;
301 uns measure = lfs * 6 + lhs * 2 * 8;
304 /* Display similarity vectors */
306 for (uns i = 0; i < cnt1 + cnt2; i++)
312 MSG("%.4f", (double)lf[i] / 0x10000);
316 for (uns i = 0; i < cnt1 + cnt2; i++)
322 MSG("%.4f", (double)lh[i] / 0x10000);
325 MSGL("Lfm=%.4f", lfs / (double)(1 << (3 + 8 + 16)));
326 MSGL("Lhm=%.4f", lhs / (double)(1 << (8 + 16)));
327 MSGL("measure=%.4f", measure / (double)(1 << (3 + 3 + 8 + 16)));
330 return (1 << (3 + 3 + 8 + 16)) - measure;
334 static uns image_signatures_dist_average(struct image_signature *sig1, struct image_signature *sig2)
336 static uns image_signatures_dist_average_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
340 byte buf[1024], *line = buf;
341 MSGL("Average matching");
345 for (uns i = 0; i < IMAGE_VEC_F; i++)
347 uns d = image_sig_cmp_features_weights[0] * isqr((int)sig1->vec.f[i] - (int)sig2->vec.f[i]);
348 MSGL("feature %u: d=%u (%u %u)", i, d, sig1->vec.f[i], sig2->vec.f[i]);
352 MSGL("dist=%u", dist);
357 #define CALL(x) image_signatures_dist_##x(sig1, sig2)
358 uns image_signatures_dist(struct image_signature *sig1, struct image_signature *sig2)
360 #define CALL(x) image_signatures_dist_##x##_explain(sig1, sig2, msg, param)
361 uns image_signatures_dist_explain(struct image_signature *sig1, struct image_signature *sig2, void (*msg)(byte *text, void *param), void *param)
365 return CALL(average);
367 switch (image_sig_compare_method)
370 return CALL(integrated);
374 return CALL(average);