2 * Image Library -- Comparitions 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.
14 #include "images/images.h"
15 #include "images/signature.h"
19 image_signatures_dist(struct image_signature *sig1, struct image_signature *sig2)
21 DBG("image_signatures_dist()");
25 struct image_region *reg1 = sig1->reg;
26 struct image_region *reg2 = sig2->reg;
27 uns mf[IMAGE_REG_MAX][IMAGE_REG_MAX], mh[IMAGE_REG_MAX][IMAGE_REG_MAX];
28 uns lf[IMAGE_REG_MAX * 2], lh[IMAGE_REG_MAX * 2];
29 uns df = sig1->df + sig2->df, dh = sig1->dh + sig2->dh;
31 /* Compute distance matrix */
32 for (uns i = 0; i < cnt1; i++)
33 for (uns j = 0; j < cnt2; j++)
36 for (uns k = 0; k < IMAGE_REG_F; k++)
38 int dif = reg1[i].f[k] - reg2[j].f[k];
43 for (uns k = 0; k < IMAGE_REG_H; k++)
45 int dif = reg1[i].h[k] - reg2[j].h[k];
52 for (uns i = 0; i < cnt1; i++)
54 uns f = mf[i][0], h = mh[i][0];
55 for (uns j = 1; j < cnt2; j++)
60 lf[i] = (df * 0x10000) / (df + (int)sqrt(f));
61 lh[i] = (dh * 0x10000) / (dh + (int)sqrt(h));
62 lfs += lf[i] * (6 * reg1[i].wa + 2 * reg1[i].wb);
63 lhs += lh[i] * reg1[i].wa;
65 for (uns i = 0; i < cnt2; i++)
67 uns f = mf[0][i], h = mh[0][i];
68 for (uns j = 1; j < cnt1; j++)
73 lf[i + cnt1] = (df * 0x10000) / (df + (int)sqrt(f));
74 lh[i + cnt1] = (dh * 0x10000) / (dh + (int)sqrt(h));
75 lfs += lf[i] * (6 * reg2[i].wa + 2 * reg2[i].wb);
76 lhs += lh[i] * reg2[i].wa;
79 uns measure = lfs * 6 + lhs * 2 * 8;
82 /* Display similarity vectors */
83 byte buf[2 * IMAGE_REG_MAX * 16 + 3], *b = buf;
84 for (uns i = 0; i < cnt1 + cnt2; i++)
89 *b++ = '~', *b++ = ' ';
90 b += sprintf(b, "%.4f", (double)lf[i] / 0x10000);
95 for (uns i = 0; i < cnt1 + cnt2; i++)
100 *b++ = '~', *b++ = ' ';
101 b += sprintf(b, "%.4f", (double)lh[i] / 0x10000);
105 DBG("Lfm=%.4f", lfs / (double)(1 << (3 + 8 + 16)));
106 DBG("Lhm=%.4f", lhs / (double)(1 << (8 + 16)));
107 DBG("measure=%.4f", measure / (double)(1 << (3 + 3 + 8 + 16)));
110 return (1 << (3 + 3 + 8 + 16)) - measure;