@@ -21,6 +21,64 @@ void ImageAlignment::alignSamples(bool useECC) {
21
21
}
22
22
}
23
23
24
+
25
+ double mutualInformation (const cv::Mat& a, const cv::Mat& b) {
26
+ std::vector<int > histo (256 *256 , 0 );
27
+ std::vector<int > aprob (256 , 0 );
28
+ std::vector<int > bprob (256 , 0 );
29
+ int width = a.cols ;
30
+ int height = a.rows ;
31
+ // x and y refer to the image pixels!
32
+ for (int y = 0 ; y < height; y++) {
33
+ for (int x = 0 ; x < width; x++) {
34
+ int ca = a.at <unsigned char >(y, x);
35
+ int cb = b.at <unsigned char >(y, x);
36
+ histo[ca + 256 *cb]++;
37
+ aprob[ca]++;
38
+ bprob[cb]++;
39
+ }
40
+ }
41
+ // w * h vettore, trova min e max, scala tra 0 e 1 e dopo lo metti su una qimg
42
+ //
43
+ double tot = height*width;
44
+ double info = 0.0 ;
45
+
46
+ for (int y = 0 ; y < 256 ; y++) {
47
+ for (int x = 0 ; x < 256 ; x++) {
48
+ double p = histo[x + 256 *y]/tot;
49
+ if (p == 0 ) continue ;
50
+ double pa = aprob[x]/tot;
51
+ double pb = bprob[y]/tot;
52
+ info += p * log (p/(pa*pb));
53
+ }
54
+ }
55
+ return info;
56
+ }
57
+
58
+
59
+ void ImageAlignment::testAlign () {
60
+ int k = 10 ;
61
+ int w = samples[0 ].cols ;
62
+ int h = samples[0 ].rows ;
63
+ cv::Mat a = samples[0 ](cv::Rect (k, k, w-2 *k, h-2 *k));
64
+ for (int i = 0 ; i < samples.size (); i++) {
65
+ cv::Mat result (2 *k+1 , 2 *k+1 , CV_32F);
66
+ for (int dy = -k; dy <= k; dy++) {
67
+ for (int dx = -k; dx <= k; dx++) {
68
+ cv::Mat b = samples[i](cv::Rect (k + dx, k+dy, w-2 *k, h-2 *k));
69
+ float my = mutualInformation (a, b);
70
+ // float my = computeECCValue(a, b);
71
+ result.at <float >(dy +k, dx+k) = my;
72
+ }
73
+ }
74
+ cv::Mat normalizedMat, uint8Mat;
75
+ cv::normalize (result, normalizedMat, 0 , 255 , cv::NORM_MINMAX);
76
+ normalizedMat.convertTo (uint8Mat, CV_8U);
77
+ cv::imwrite (std::to_string (i) + " .png" , uint8Mat);
78
+ }
79
+ }
80
+
81
+
24
82
double ImageAlignment::computeECCValue (const cv::Mat& src, const cv::Mat& ref) {
25
83
cv::Mat srcFloat, refFloat;
26
84
src.convertTo (srcFloat, CV_32F);
@@ -44,14 +102,16 @@ cv::Mat ImageAlignment::computeECC(const cv::Mat& src, const cv::Mat& ref) {
44
102
}
45
103
46
104
double ImageAlignment::computeMutualInformationValue (const cv::Mat& src, const cv::Mat& ref) {
105
+ return mutualInformation (src, ref);
47
106
const int histSize = 256 ;
48
107
float range[] = { 0 , 256 };
49
108
const float * histRange = { range };
50
109
51
110
cv::Mat histSrc, histRef, jointHist;
52
- cv::calcHist (&src, 1 , 0 , cv::Mat (), histSrc, 1 , &histSize, &histRange);
53
- cv::calcHist (&ref, 1 , 0 , cv::Mat (), histRef, 1 , &histSize, &histRange);
54
- cv::calcHist (&src, 1 , 0 , ref, jointHist, 2 , &histSize, &histRange);
111
+ int channels[] = {0 };
112
+ cv::calcHist (&src, 1 , channels, cv::Mat (), histSrc, 1 , &histSize, &histRange);
113
+ cv::calcHist (&ref, 1 , channels, cv::Mat (), histRef, 1 , &histSize, &histRange);
114
+ cv::calcHist (&src, 1 , channels, ref, jointHist, 2 , &histSize, &histRange);
55
115
56
116
cv::normalize (histSrc, histSrc, 1 , 0 , cv::NORM_L1);
57
117
cv::normalize (histRef, histRef, 1 , 0 , cv::NORM_L1);
0 commit comments