-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
203 lines (136 loc) · 4.88 KB
/
main.cpp
File metadata and controls
203 lines (136 loc) · 4.88 KB
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
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "OpticalFlow.h"
#include "opticalflowhs.h"
#include <fstream>
#include <string>
#include <cstring>
//#include <QDir>
//#include <QDebug>
using namespace std;
static const int mult = 16;
void copnvert2flow(const cv::Mat& velx, const cv::Mat& vely, cv::Mat& flow)
{
//cv::Mat flow(velx.size(), CV_32FC2);
for(int y = 0 ; y < flow.rows; ++y)
for(int x = 0 ; x < flow.cols; ++x)
flow.at<cv::Point2f>(y, x) = cv::Point2f(velx.at<float>(y, x), vely.at<float>(y, x));
}
void SaveFlow(const cv::Mat& flow, cv::Mat &cflow)
{
const float m2 = 0.3f;
const float minVel = 0.1f;
for(int y = 0; y < flow.rows; ++y)
for(int x = 0; x < flow.cols; ++x)
{
cv::Point2f f = flow.at<cv::Point2f>(y, x);
if (f.x * f.x + f.y * f.y > minVel * minVel)
{
cv::Point p1 = cv::Point(x, y) * mult;
cv::Point p2 = cv::Point(cvRound((x + f.x*m2) * mult), cvRound((y + f.y*m2) * mult));
cv::line(cflow, p1, p2, CV_RGB(0, 255, 0));
cv::circle(cflow, cv::Point(x, y) * mult, 2, CV_RGB(255, 0, 0));
}
}
// return cflow;
}
int main()
{
//cv::Size szImg(256,240);
cv::Size szImg(200,200);
cout<<szImg.width<<endl;
cout<<szImg.height<<endl;
getchar();
//cout << "Hello world!" << endl;
cv::Mat flow(szImg,CV_32FC2);
cv::Mat colorFlow(szImg.height*mult, szImg.width*mult, CV_8UC3);
// cout<<prev.rows<<endl;
// cout<<prev.cols<<endl;
cv::Mat vx(szImg.height,szImg.width,CV_32FC1);
cv::Mat vy(szImg.height,szImg.width,CV_32FC1);
cv::Mat prevGray(szImg.height,szImg.width,CV_8UC1);
cv::Mat curGray(szImg.height,szImg.width,CV_8UC1);
cout<<"vx step="<<vx.step1()<<endl;
// //cv::Mat vy(imgSize.width,imgSize.height,CV_32FC1);
// cout<<vx.rows<<endl;
// cout<<vx.cols<<endl;
bool UseHS = false;
string dir = "/home/xubuntu/Downloads/flow/sphere";
int count = 20;
char fileName[32];
if(!UseHS)
{
std::cout<<"Using LK Method"<<endl;
getchar();
OpticalFlowComputing* ofc = new OpticalFlowComputing(cv::Size(5,5), cv::Size(prevGray.cols,prevGray.rows),prevGray.step1(),false);
int i = 0;
while(i<count-1)
{
//ofc->InitializeVelocityVectors(vx.ptr<float>(),vy.ptr<float>(),vx.step1());
string prevPath = dir+"/";
sprintf(fileName,"sphere.%d.bmp",i);
prevPath += fileName;
string curPath = dir+"/";
sprintf(fileName,"sphere.%d.bmp",i+1);
curPath += fileName;
cout<<"Process "<<prevPath<<" and "<<curPath<<endl;
cv::Mat prevImage = cv::imread(prevPath.c_str());
cv::Mat curImage = cv::imread(curPath.c_str());
cv::cvtColor(prevImage,prevGray,CV_BGR2GRAY);
cv::cvtColor(curImage,curGray,CV_BGR2GRAY);
ofc->SetInputTwoImages(prevGray.ptr<unsigned char>(), curGray.ptr<unsigned char>());
ofc->CalFirstLine();
ofc->DoWork(vx.ptr<float>(),vy.ptr<float>(),vx.step1());
copnvert2flow(vx,vy,flow);
// //cv::Mat colorFlow = SaveFlow(prevGray,flow);
colorFlow.setTo(cv::Scalar(0,0,0));
SaveFlow(flow,colorFlow);
string savePath = dir+"/";
sprintf(fileName,"LKResult_sphere_%dvs%d.png",i+1,i);
savePath += fileName;
// QString saveFile = savePrefix.append(QString("%1_%2.png").arg(i).arg(i+1));
cv::imwrite(savePath.c_str(),colorFlow);
++i;
}
delete ofc;
}
else
{
std::cout<<"Using HS Method"<<endl;
getchar();
OpticalFlowHS* ofc = new OpticalFlowHS(szImg,false,0.5,prevGray.step1());
ofc->SetIterTerm(true,200,0.0);
int i = 0;
while(i<count-1)
{
ofc->InitializeVelocityVectors(vx.ptr<float>(),vy.ptr<float>(),vx.step1());
string prevPath = dir+"/";
sprintf(fileName,"sphere.%d.bmp",i);
prevPath += fileName;
string curPath = dir+"/";
sprintf(fileName,"sphere.%d.bmp",i+1);
curPath += fileName;
cout<<"Process "<<prevPath<<" and "<<curPath<<endl;
cv::Mat prevImage = cv::imread(prevPath.c_str());
cv::Mat curImage = cv::imread(curPath.c_str());
cv::cvtColor(prevImage,prevGray,CV_BGR2GRAY);
cv::cvtColor(curImage,curGray,CV_BGR2GRAY);
ofc->SetInputTwoImages(prevGray.ptr<unsigned char>(), curGray.ptr<unsigned char>());
ofc->CalcFirstLineSobel();
ofc->CalcSobel(vx.ptr<float>(),vy.ptr<float>(),vx.step1());
copnvert2flow(vx,vy,flow);
// //cv::Mat colorFlow = SaveFlow(prevGray,flow);
colorFlow.setTo(cv::Scalar(0,0,0));
SaveFlow(flow,colorFlow);
string savePath = dir+"/";
sprintf(fileName,"HSResult_sphere_%dvs%d.png",i+1,i);
savePath += fileName;
// QString saveFile = savePrefix.append(QString("%1_%2.png").arg(i).arg(i+1));
cv::imwrite(savePath.c_str(),colorFlow);
++i;
}
delete ofc;
}
return 0;
}