]> mj.ucw.cz Git - libucw.git/blob - images/sig-cmp.c
sources backup... changed:
[libucw.git] / images / sig-cmp.c
1 /*
2  *      Image Library -- Comparitions of image signatures
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #define LOCAL_DEBUG
11
12 #include "lib/lib.h"
13 #include "lib/math.h"
14 #include "images/images.h"
15 #include "images/signature.h"
16 #include <stdio.h>
17
18 uns
19 image_signatures_dist(struct image_signature *sig1, struct image_signature *sig2)
20 {
21   DBG("image_signatures_dist()");
22
23   uns cnt1 = sig1->len;
24   uns cnt2 = sig2->len;
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;
30
31   /* Compute distance matrix */
32   for (uns i = 0; i < cnt1; i++)
33     for (uns j = i + 1; j < cnt2; j++)
34       {
35         uns d = 0;
36         for (uns k = 0; k < IMAGE_REG_F; k++)
37           {
38             int dif = reg1[i].f[k] - reg2[j].f[k];
39             d += dif * dif;
40           }
41         mf[i][j] = d;
42         d = 0;
43         for (uns k = 0; k < IMAGE_REG_H; k++)
44           {
45             int dif = reg1[i].h[k] - reg2[j].h[k];
46             d += dif * dif;
47           }
48         mh[i][j] = d;
49       }
50
51   uns lfs = 0, lhs = 0;
52   for (uns i = 0; i < cnt1; i++)
53     {
54       uns f = mf[i][0], h = mh[i][0];
55       for (uns j = 1; j < cnt2; j++)
56         {
57           f = MIN(f, mf[i][j]);
58           h = MIN(h, mh[i][j]);
59         }
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;
64     }
65   for (uns i = 0; i < cnt2; i++)
66     {
67       uns f = mf[0][i], h = mh[0][i];
68       for (uns j = 1; j < cnt1; j++)
69         {
70           f = MIN(f, mf[j][i]);
71           h = MIN(h, mh[j][i]);
72         }
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;
77     }
78
79   uns measure = lfs * 6 + lhs * 2 * 8;
80
81 #ifdef LOCAL_DEBUG
82   /* Display similarity vectors */
83   byte buf[2 * IMAGE_REG_MAX * 16 + 3], *b = buf;
84   for (uns i = 0; i < cnt1 + cnt2; i++)
85     {
86       if (i)
87         *b++ = ' ';
88       if (i == cnt1)
89         *b++ = '~', *b++ = ' ';
90       b += sprintf(b, "%.4f", (double)lf[i] / 0x10000);
91     }
92   *b = 0;
93   DBG("Lf=(%s)", buf);
94   b = buf;
95   for (uns i = 0; i < cnt1 + cnt2; i++)
96     {
97       if (i)
98         *b++ = ' ';
99       if (i == cnt1)
100         *b++ = '~', *b++ = ' ';
101       b += sprintf(b, "%.4f", (double)lh[i] / 0x10000);
102     }
103   *b = 0;
104   DBG("Lh=(%s)", buf);
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)));
108 #endif
109
110   return measure;
111 }