-
Notifications
You must be signed in to change notification settings - Fork 0
/
1205046_Hierarchical.cpp
187 lines (171 loc) · 6.32 KB
/
1205046_Hierarchical.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
/*-------property of the half blood prince-----*/
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cstdio>
#include <iostream>
#define ISVALID(cx,cy,m,n,M,N) (((cx)+(m))<(M) && ((cy)+(n))<(N) && (cx)>=0 && (cy)>=0)
using namespace cv;
using namespace std;
vector < vector <double> > ref_mat;
vector < vector <double> > test_mat;
int Parr[23]={512,256,128,120,111,100,98,72,65,64,50,48,32,27,24,16,12,11,8,4,3,2,1};
int totalframesearch=0;
double grayscaleval(int r, int g, int b){
return r;
}
double sq(double val){
return val*val;
}
double cost(vector< vector <double> > &rfrnc, vector < vector <double> > &test, int posx, int posy){
double s=0.0;
int tr=test_mat.size(); int rr=ref_mat.size();
int tc=test_mat[0].size(); int rc=ref_mat[0].size();
for(int i=0; i<rr; ++i){
for(int j=0;j<rc; ++j){
if((posx+i)>=tr || (posy+j)>=tc){s+=1000000000.00;printf("moga\n");}
else s=s+sq(rfrnc[i][j]-test[posx+i][posy+j]);
}
}
return s;
}
pair <int,int> exhaustivebestfit(vector < vector <double> > &rfrnc, vector < vector <double> > &test){
double mnn=1000000000000.00;
pair< int,int >bestpos=make_pair(-1,-1);
int refrow=rfrnc.size();
int refcol=rfrnc[0].size();
int testrow=test.size();
int testcol=test[0].size();
for(int i = 0;(i+refrow)<testrow ; ++i){
for( int j = 0; (j+refcol)<testcol; ++j){
double val=cost(rfrnc,test,i,j);
if(val<mnn){
mnn=val;
bestpos.first=i;
bestpos.second=j;
}
}
}
//cout<<mnn<<"\n";
return bestpos;
}
void fillrectangle(Mat &test_frame, int posi, int posj, Mat &ref_frame){
int refrow=ref_frame.rows;
int refcol=ref_frame.cols;
for(int y = posi; y<(posi+refrow); ++y){
for(int x= posj; x<(posj+refcol); ++x){
if(x==posj || x==(posj+refcol-1) || y==posi || y==(posi+refrow-1)){
Vec3b col=test_frame.at<Vec3b>(Point(x,y));
col[0]=0;
col[1]=0;
col[2]=255;
test_frame.at<Vec3b>(Point(x,y))=col;
}
}
}
}
vector< vector <double> >convert(Mat &frame){
vector< vector <double> >mat;
vector<double>line;
for(int y=0;y<frame.rows; ++y){
mat.push_back(line);
for(int x=0;x<frame.cols;++x){
Vec3b col=frame.at<Vec3b>(Point(x,y));
int b=(int)col[0];
int g=(int)col[1];
int r=(int)col[2];
double gray=grayscaleval(r,g,b);
mat[y].push_back(gray);
}
}
return mat;
}
pair <int,int> logsearch(int p, int cx, int cy, int start, double prevcost){
int refrow=ref_mat.size(); int refcol=ref_mat[0].size(); int testrow=test_mat.size(); int testcol=test_mat[0].size();
if(p==0)return make_pair(cx,cy);
//printf("log search e p %d te\n",p);
double mnn=100000000000000.00;int mnx=-1;int mny=-1;
if(!start){mnn=prevcost;mnx=cx;mny=cy;}
for(int x=-p;x<=p;x+=p){
for(int y=-p;y<=p;y+=p){
int curx=cx+x;
int cury=cy+y;
//printf("%d %d\n",curx,cury);
if(x==0 && y==0 && !start)continue;
if(ISVALID(curx,cury,refrow,refcol,testrow,testcol)){
double val=cost(ref_mat,test_mat,curx,cury);
totalframesearch++;
if(val<mnn){
mnn=val; mnx=curx; mny=cury;
}
}
}
}
//printf("minx: %d miny: %d\n",mnx,mny);
return logsearch(p/2,mnx,mny,0,mnn);
}
pair<int,int> hiersearch(Mat R, Mat T, int p, int level, int x, int y){
if(level==0){
ref_mat=convert(R); test_mat=convert(T);
pair<int,int> bestpos=logsearch(p,x,y,1,-1.0);
return bestpos;
}
Mat newr=R.clone(); Mat newt=T.clone();
Mat lowr; Mat lowt;
pyrDown(newr,lowr,Size(newr.cols/2,newr.rows/2)); pyrDown(newt,lowt,Size(newt.cols/2,newt.rows/2));
pair<int,int>thatpos=hiersearch(lowr,lowt,p/2,level-1,x/2,y/2);
int x1=thatpos.first; int y1=thatpos.second;
ref_mat=convert(R); test_mat=convert(T);
pair<int,int>bestpos=logsearch(1,2*x1,2*y1,1,-1.0);
return bestpos;
}
int main(){
for(int L=2;L>=2;--L){
for(int i=2;i<3;++i){
int P=Parr[i];
totalframesearch=0;
VideoCapture cap("input.MOV");
Mat ref_frame=imread("reference.jpg");
if(!cap.isOpened() || !ref_frame.data){
cout<<"oops!\n";
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
//cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("output3.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object
if (!oVideoWriter.isOpened()){
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
ref_mat=convert(ref_frame);
int cnt=0;
pair<int,int>bestpos=make_pair(-1,-1);
while(1){
Mat test_frame;
bool success=cap.read(test_frame);
if(!success){break;}
++cnt;
//printf("frame no: %d\n",cnt);
//if(cnt%10)continue;
test_mat=convert(test_frame);
if(bestpos.first==-1){
bestpos=exhaustivebestfit(ref_mat,test_mat);
}
else{
pair<int,int>tempos=bestpos;
bestpos=hiersearch(ref_frame,test_frame,P,L,tempos.first,tempos.second);
}
//printf("best match at pixel : %d %d\n",bestpos.second,bestpos.first);
fillrectangle(test_frame,bestpos.first,bestpos.second,ref_frame);
oVideoWriter.write(test_frame);
if(waitKey(10)==27){
cout<<"esc pressed\n";
break;
}
}
printf("%d\t%d\t%.2lf\n",L,P,(double)totalframesearch/(double)cnt);
}
}
return 0;
}