-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatching_MSE.cpp
232 lines (185 loc) · 6.4 KB
/
matching_MSE.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#pragma once
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include <stack>
#define _USE_MATH_DEFINES
#include <math.h>
#include "motion_vector.h"
using namespace std;
#pragma warning(disable: 4819)
void matchingMSE(IplImage* frame1, IplImage* frame2)
{
// Image size load
int height = frame1->height;
int width = frame1->width;
FILE* stream;
stream = fopen("motion.txt", "w");
// Memory allocate
IplImage* difference = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
IplImage* temp = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
IplImage* flow = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
IplImage* reconstruction = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
IplImage* flowanchor = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
int i, j;
int k; // color index
k = 0;
int nIdx = frame1->widthStep; // y position increment -- transform space
int mIdx = frame1->nChannels; // x position increment
for (j = 0; j < height; j++)
{
for (i = 0; i < width; i++)
{
reconstruction->imageData[j*nIdx + i*mIdx + k] = frame1->imageData[j*nIdx + i*mIdx + k];
}
}
for (j = 0; j < height; j++)
{
for (i = 0; i < width; i++)
{
for (k = 0; k < 3; k++)
{
flow->imageData[j*nIdx*3 + i*mIdx*3 + k] = 0;
flowanchor->imageData[j*nIdx*3 + i*mIdx*3 + k] = frame1->imageData[j*nIdx + i*mIdx];
}
}
}
// Input block size
int blockSize = 0;
int searchRange = 0;
printf(" Input Block Size : \n");
scanf("%d", &blockSize);
printf(" Image has been parted with %d x %d blocks", width / blockSize, height / blockSize);
printf(" Input Search Range : \n");
scanf("%d", &searchRange);
// Make Block & Search Area
int* makeBlock = new int[blockSize*blockSize];
int* searchBlock = new int[blockSize*blockSize];
int* resultBlock = new int[(2 * searchRange + 1)*(2 * searchRange + 1)];
int n = 0;
int m = 0;
int n_ref = height / blockSize;
int m_ref = width / blockSize;
int p = 0, q = 0;
// load each block
// Start : block Sweep
for (n = 0; n < n_ref; n++)
{
for (m = 0; m < m_ref; m++)
{
for (j = 0; j < blockSize; j++)
{
for (i = 0; i < blockSize; i++)
{
int f1 = (int)frame1->imageData[(j + n*blockSize)*width + (i + m*blockSize)];
makeBlock[j*blockSize + i] = (int)f1;
}
}
int sum = 0;
int result = 0;
// Start : searchBlock Sweep
for (q = -searchRange; q <= searchRange; q++)
{
for (p = -searchRange; p <= searchRange; p++)
{
sum = 0;
for (j = 0; j < blockSize; j++)
{
for (i = 0; i < blockSize; i++)
{
if (((j + q + n*blockSize)*width) <= 0 || (i + p + m*blockSize) <= 0) continue; // edge exception
char f2 = frame2->imageData[(j + q + n*blockSize)*width + (i + p + m*blockSize)];
searchBlock[j*blockSize + i] = (int)f2;
sum += pow((makeBlock[j*blockSize + i] - searchBlock[j*blockSize + i]),2);
//printf(" [p,q : %d, %d] [i,j : %d, %d] frame 1 : %d, frame 2 : %d, diff : %d, sum : %d \n",p,q,i,j, makeBlock[j + i], searchBlock[j + i], abs( makeBlock[j + i] - searchBlock[j + i]), sum);
result = sum;
}
}
resultBlock[(searchRange + q)*(2 * searchRange + 1) + (searchRange + p)] = result;
//k++;
//printf("[%d][m:%d][n:%d] [p:%d][q:%d] sum : %d\n", k, m, n, p, q, result);
sum = 0;
}
}
// End : searchBlock Sweep
int minimum = 999999999;
int value = 0;
int motion_x = 0;
int motion_y = 0;
// Start : find the minimum value
for (q = -searchRange; q <= searchRange; q++)
{
for (p = -searchRange; p <= searchRange; p++)
{
value = resultBlock[(searchRange + q)*(2 * searchRange + 1) + (searchRange + p)];
if (value < minimum)
minimum = value;
}
}
//printf(" minimum value : %d \n", minimum);
for (q = -searchRange; q <= searchRange; q++)
{
for (p = -searchRange; p <= searchRange; p++)
{
if (resultBlock[(searchRange + q)*(2 * searchRange + 1) + (searchRange + p)] <= blockSize*blockSize*5)
{
motion_x = 0;
motion_y = 0;
goto here;
}
if (resultBlock[(searchRange + q)*(2 * searchRange + 1) + (searchRange + p)] == minimum)
{
motion_x = p;
motion_y = q;
//printf(" block : [%d, %d] motion vector : [%d, %d] \n", m, n, motion_x, motion_y);
goto here;
}
}
//if (resultBlock[(searchRange + q)*(2 * searchRange + 1) + (searchRange + p)] == minimum)
//{
// break;
//}
}
// End : find the minimum value
here:
// Start : Save the Result
{
int tempm = m*blockSize + 0.5*blockSize;
int tempn = n*blockSize + 0.5*blockSize;
printf(" block : [%d, %d] motion vector : [%d, %d] \n", m, n, motion_x, motion_y);
fprintf(stream, "%d %d %d %d \n", tempm, tempn, motion_x, motion_y);
}
// End : Save the Result
cvDrawQuiver(flow, cvPoint(m*blockSize + 0.5*blockSize, n*blockSize + 0.5*blockSize), cvPoint(m*blockSize + 0.5*blockSize + motion_x, n*blockSize + 0.5*blockSize + motion_y), CV_RGB(0, 255, 0), 1, 3);
cvDrawQuiver(flowanchor, cvPoint(m*blockSize + 0.5*blockSize, n*blockSize + 0.5*blockSize), cvPoint(m*blockSize + 0.5*blockSize + motion_x, n*blockSize + 0.5*blockSize + motion_y), CV_RGB(0, 255, 0), 1, 3);
for (j = 0; j < blockSize; j++)
{
for (i = 0; i < blockSize; i++)
{
if ((j + n*blockSize + motion_y)*width <= 0 || (i + m*blockSize + motion_x) <= 0) continue;
reconstruction->imageData[(j + n*blockSize + motion_y)*width + (i + m*blockSize + motion_x)] = frame1->imageData[(j + n*blockSize)*width + (i + m*blockSize)];
}
}
//makeBlock[j*blockSize + i] = (int)frame1->imageData[(j + n*blockSize)*width + (i + m*blockSize)];
//printf("%d \n", makeBlock[j + i]);
} // End : block Sweep
}
fclose(stream);
printf("PSNR is %lf \n", getPSNR(frame2, reconstruction));
cvNamedWindow("flow", CV_WINDOW_AUTOSIZE);
cvShowImage("flow", flow);
cvSaveImage("flowMSE.bmp", flow);
display_img_info(flow);
cvNamedWindow("reconstruction", CV_WINDOW_AUTOSIZE);
cvShowImage("reconstruction", reconstruction);
cvSaveImage("reconstructionMSE.bmp", reconstruction);
display_img_info(reconstruction);
cvNamedWindow("flowanchor", CV_WINDOW_AUTOSIZE);
cvShowImage("flowanchor", flowanchor);
cvSaveImage("flowanchor.bmp", flowanchor);
display_img_info(reconstruction);
}