]> mj.ucw.cz Git - libucw.git/blob - images/sig-cmp-gen.h
more verbose IMAGESIM explain for debug purposes
[libucw.git] / images / sig-cmp-gen.h
1 #ifndef SIG_EXPLAIN
2
3 #define MSG(x...) do{}while(0)
4
5 static uns
6 image_signatures_dist_2(struct image_signature *sig1, struct image_signature *sig2)
7
8 #else
9
10 #define MSG(x...) bprintf(fb, x)
11
12 static void
13 dump_signature(struct image_signature *sig, struct fastbuf *fb)
14 {
15   MSG("signature: flags=0x%x df=%u dh=%u f=(%u", sig->flags, sig->df, sig->dh, sig->vec.f[0]);
16   for (uns i = 1; i < IMAGE_VEC_F; i++)
17     MSG(" %u", sig->vec.f[i]);
18   MSG(")\n");
19   for (uns j = 0; j < sig->len; j++)
20     {
21       struct image_region *reg = sig->reg + j;
22       MSG("region %u: wa=%u wb=%u f=(%u", j, reg->wa, reg->wb, reg->f[0]);
23       for (uns i = 1; i < IMAGE_VEC_F; i++)
24         MSG(" %u", reg->f[i]);
25       MSG(") h=(%u", reg->h[0]);
26       for (uns i = 1; i < IMAGE_REG_H; i++)
27         MSG(" %u", reg->h[i]);
28       MSG(")\n");
29     }
30 }
31
32 static uns
33 image_signatures_dist_2_explain(struct image_signature *sig1, struct image_signature *sig2, struct fastbuf *fb)
34
35 #endif
36 {
37   DBG("image_signatures_dist_2()");
38
39   uns dist[IMAGE_REG_MAX * IMAGE_REG_MAX], p[IMAGE_REG_MAX], q[IMAGE_REG_MAX];
40   uns n, i, j, k, l, s, d;
41   struct image_region *reg1, *reg2;
42
43 #ifdef SIG_EXPLAIN
44   dump_signature(sig1, fb);
45   dump_signature(sig2, fb);
46 #endif
47
48   /* Compute distance matrix */
49   n = 0;
50   MSG("Distance matrix:\n");
51   /* ... for non-textured images */
52   if (!((sig1->flags | sig2->flags) & IMAGE_SIG_TEXTURED))
53     for (j = 0, reg2 = sig2->reg; j < sig2->len; j++, reg2++)
54       for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
55         {
56           uns ds =
57             isqr((int)reg1->h[0] - (int)reg2->h[0]) +
58             isqr((int)reg1->h[1] - (int)reg2->h[1]) +
59             isqr((int)reg1->h[2] - (int)reg2->h[2]);
60           uns dt =
61             isqr((int)reg1->f[0] - (int)reg2->f[0]) +
62             isqr((int)reg1->f[1] - (int)reg2->f[1]) +
63             isqr((int)reg1->f[2] - (int)reg2->f[2]) +
64             isqr((int)reg1->f[3] - (int)reg2->f[3]) +
65             isqr((int)reg1->f[4] - (int)reg2->f[4]) +
66             isqr((int)reg1->f[5] - (int)reg2->f[5]);
67           if (ds < 1000)
68             dt *= 8;
69           else if (ds < 10000)
70             dt *= 12;
71           else
72             dt *= 16;
73           DBG("[%u][%u] ... dt=%u ds=%u", i, j, dt, ds);
74           dist[n++] = (dt << 8) + i + (j << 4);
75           MSG("[%u, %u] ... dt=%u ds=%u\n", i, j, dt, ds);
76         }
77   /* ... for textured images (ignore shape properties) */
78   else
79     for (j = 0, reg2 = sig2->reg; j < sig2->len; j++, reg2++)
80       for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
81         {
82           uns dt =
83             isqr((int)reg1->f[0] - (int)reg2->f[0]) +
84             isqr((int)reg1->f[1] - (int)reg2->f[1]) +
85             isqr((int)reg1->f[2] - (int)reg2->f[2]) +
86             isqr((int)reg1->f[3] - (int)reg2->f[3]) +
87             isqr((int)reg1->f[4] - (int)reg2->f[4]) +
88             isqr((int)reg1->f[5] - (int)reg2->f[5]);
89           dist[n++] = (dt << 12) + i + (j << 4);
90           MSG("[%u, %u] ... dt=%u\n", i, j, dt);
91         }
92
93   /* One or both signatures have no regions */
94   if (!n)
95     return 0xffffffff;
96
97   /* Get percentages */
98   for (i = 0, reg1 = sig1->reg; i < sig1->len; i++, reg1++)
99     p[i] = reg1->wb;
100   for (i = 0, reg2 = sig2->reg; i < sig2->len; i++, reg2++)
101     q[i] = reg2->wb;
102
103   /* Sort entries in distance matrix */
104   image_signatures_dist_2_sort(n, dist);
105
106   /* Compute significance matrix and resulting distance */
107   uns sum = 0;
108   MSG("Significance matrix:\n");
109   for (k = 0, l = 128; l; k++)
110     {
111       i = dist[k] & 15;
112       j = (dist[k] >> 4) & 15;
113       d = dist[k] >> 8;
114       if (p[i] <= q[j])
115         {
116           s = p[i];
117           q[j] -= p[i];
118           p[i] = 0;
119         }
120       else
121         {
122           s = q[j];
123           p[i] -= q[j];
124           q[j] = 0;
125         }
126       l -= s;
127       sum += s * d;
128       DBG("s[%u][%u]=%u d=%u", i, j, s, d);
129       MSG("[%u, %u]=%u d=%u\n", i, j, s, d);
130     }
131
132   return sum;
133 }
134
135 #undef SIG_EXPLAIN
136 #undef MSG