Magick ColorYUV compare_block_by_yuv Magick Image img1 Magick Image im

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Magick::ColorYUV compare_block_by_yuv(Magick::Image *img1, Magick::Image *img2) {
unsigned int n,m;
#ifdef _DEBUG
std::cerr << "D [compare_block_by_yuv]: Comparing (" << img1->columns() << "," << img1->rows() << ")"
<< " x (" << img2->columns() << "," << img2->rows() << ")" << std::endl;
#endif
assert((img1->columns()) == (n = (img2->columns())));
assert(((img1->rows()) == (m = (img2->rows()))));
long long nm = n*m;
Magick::Pixels img1px(*img1);
Magick::Pixels img2px(*img2);
Magick::PixelPacket *img1pxp = img1px.get(0,0,n,m);
Magick::PixelPacket *img2pxp = img2px.get(0,0,n,m);
Magick::ColorYUV res;
Magick::ColorYUV cpx1;
Magick::ColorYUV cpx2;
double d_y = 0.0, d_u = d_y, d_v = d_u;
double r_y = 0.0, r_u = r_y, r_v = r_u;
for (unsigned int i=0; i<n; i++) {
for (unsigned int j=0; j<m; j++) {
cpx1 = *(img1pxp + n*i + j);
d_y += cpx1.y(); d_u += cpx1.u(); d_v += cpx1.v();
cpx2 = *(img2pxp + n*i + j);
r_y += cpx2.y(); r_u += cpx2.u(); r_v += cpx2.v();
}
}
d_y /= nm; d_u /= nm; d_v /= nm;
r_y /= nm; r_u /= nm; r_v /= nm;
double alpha_y = 0.0, alpha_u = alpha_y, alpha_v = alpha_u;
double beta_y = 0.0, beta_u = beta_y, beta_v = beta_u;
for (unsigned int i=0; i<n; i++) {
for (unsigned int j=0; j<m; j++) {
cpx1 = *(img1pxp + n*i + j);
cpx2 = *(img2pxp + n*i + j);
alpha_y += (cpx1.y() - d_y)*(cpx2.y() - r_y);
beta_y += pow((cpx1.y() - d_y),2);
alpha_u += (cpx1.u() - d_u)*(cpx2.u() - r_u);
beta_u += pow((cpx1.u() - d_u),2);
alpha_v += (cpx1.v() - d_v)*(cpx2.v() - r_v);
beta_v += pow((cpx1.v() - d_v),2);
}
}
double o_y, o_u, o_v;
double s_y, s_u, s_v;
o_y = r_y - d_y*(alpha_y/beta_y);
s_y = alpha_y/beta_y;
o_u = r_u - d_u*(alpha_u/beta_u);
s_u = alpha_u/beta_u;
o_v = r_v - d_v*(alpha_v/beta_v);
s_v = alpha_v/beta_v;
double res_y = 0.0, res_u = res_y, res_v = res_u;
for (unsigned int i=0; i<n; i++) {
for (unsigned int j=0; j<m; j++) {
cpx1 = *(img1pxp + n*i + j);
cpx2 = *(img2pxp + n*i + j);
res_y += pow((s_y*cpx1.y()+o_y-cpx2.y()),2);
res_u += pow((s_u*cpx1.u()+o_u-cpx2.u()),2);
res_v += pow((s_v*cpx1.v()+o_v-cpx2.v()),2);
}
}
#ifdef _DEBUG
std::cerr << "D [compare_block_by_yuv]: Optimized brightness (" << o_y << "," << o_u << "," << o_v << ");"
<< " contrast (" << s_y << "," << s_u << "," << s_v << ");"
<< " difference (" << res_y << "," << res_u << "," << res_v << ");" << std::endl;
#endif
res.y(res_y);
res.u(res_u);
res.v(res_v);
return res;
}