forked from TurtleZhong/camera_lidar_calibration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorner_detecter.cc
179 lines (155 loc) · 5.43 KB
/
corner_detecter.cc
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
/**
* This program used for detect 1 corner (subpixel) in the image
* Input: config/config.yaml imgs/image_lists.txt
* Output: data/image_points.txt
* Author: xinliangzhong@foxmail.com
* Data: 2018.07.05
* Note: Remember to undistort the image.
*/
#include <config.h>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <sstream>
using namespace std;
using namespace cv;
cv::Mat org,img,tmp;
ofstream outFile;
Point2f keypoint;
void cornerDetect(const cv::Mat &img, const Point &left_up, const Point &right_down)
{
/// Create Mask
Mat image = img.clone();
cvtColor(image, image, COLOR_BGR2GRAY);
Mat mask(image.rows, image.cols, CV_8U, Scalar(0));
Mat detect_roi = image.rowRange(left_up.y, right_down.y).colRange(left_up.x, right_down.x);
detect_roi.copyTo(mask.rowRange(left_up.y, right_down.y).colRange(left_up.x, right_down.x));
/// Shi-Tomasi
vector<Point2f> corners;
double qualityLevel = 0.01;
double minDistance = 10;
int blockSize = 3;
bool useHarrisDetector = false;
double k = 0.04;
int maxCorners = 1;
goodFeaturesToTrack( image,
corners,
maxCorners,
qualityLevel,
minDistance,
mask,
blockSize,
useHarrisDetector,
k );
/// subpixel
Size winSize = Size( 3, 3 );
Size zeroZone = Size( -1, -1 );
TermCriteria criteria = TermCriteria(
CV_TERMCRIT_EPS + CV_TERMCRIT_ITER,
40, //maxCount=40
0.001 ); //epsilon=0.001
cornerSubPix( image, corners, winSize, zeroZone, criteria );
cvtColor(image, image, COLOR_GRAY2BGR);
circle( image, corners[0], 1, Scalar(0,0,255), -1, 8, 0 );
imshow("corner", image);
cout << "corners: " << corners[0].x << " " << corners[0].y << endl;
keypoint = corners[0];
}
void on_mouse(int event,int x,int y,int flags,void *ustc)
{
static Point pre_pt(-1,-1);
static Point cur_pt(-1,-1);
char temp[16];
if (event == CV_EVENT_LBUTTONDOWN)
{
org.copyTo(img);
sprintf(temp,"(%d,%d)",x,y);
pre_pt = Point(x,y);
putText(img,temp,pre_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255),1,8);
circle(img,pre_pt,2,Scalar(255,0,0),CV_FILLED,CV_AA,0);
imshow("img",img);
}
else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
{
img.copyTo(tmp);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = Point(x,y);
putText(tmp,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,255));
imshow("img",tmp);
}
else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
{
img.copyTo(tmp);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = Point(x,y);
putText(tmp,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,255));
rectangle(tmp,pre_pt,cur_pt,Scalar(255,0,0),1,8,0);
imshow("img",tmp);
}
else if (event == CV_EVENT_LBUTTONUP)
{
org.copyTo(img);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = Point(x,y);
putText(img,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,255));
circle(img,pre_pt,2,Scalar(255,0,0),CV_FILLED,CV_AA,0);
rectangle(img,pre_pt,cur_pt,Scalar(255,0,0),1,8,0);
imshow("img",img);
/// Corner detector
cornerDetect(org, pre_pt, cur_pt);
}
}
int main()
{
Config::setParameterFile("/home/m/work/camera_lidar_calibration/config/config.yaml");
string work_dir = Config::get<string>("working.dir");
outFile.open(work_dir + "data/image_points.txt");
/// Load images
string images_dir = Config::get<string>("images.dir");
string image_path = images_dir + "/image_lists.txt";
vector<string> image_paths;
ifstream inFile;
inFile.open(image_path);
while (inFile.good())
{
string image_name;
inFile >> image_name;
string img_path = images_dir + "/" + image_name;
image_paths.push_back(img_path);
inFile.get();
}
/// Corner detect
double fx = Config::get<double>("fx");
double fy = Config::get<double>("fy");
double cx = Config::get<double>("cx");
double cy = Config::get<double>("cy");
double k1 = Config::get<double>("k1");
double k2 = Config::get<double>("k2");
double p1 = Config::get<double>("p1");
double p2 = Config::get<double>("p2");
cv::Mat K = (Mat_<double>(3, 3) << fx, 0., cx, 0., fy, cy, 0., 0., 1.);
cv::Mat D = (Mat_<double>(5,1) << k1, k2, p1, p2, 0.0);
cout << "K:\n" << K << endl;
cout << "D:\n" << D << endl;
cout << "We need to process " << image_paths.size() << " images" << endl;
for (int i = 0; i < image_paths.size(); ++i)
{
cout << "Processing images: " << i << endl;
/// TO DO: Undistort the image
org = imread(image_paths[i]);
cv::undistort(org,img,K,D);
org = img.clone();
// org.copyTo(img);
org.copyTo(tmp);
namedWindow("img");
setMouseCallback("img",on_mouse,0);
imshow("img",img);
cv::waitKey(0);
destroyWindow("corner");
outFile << keypoint.x << " " << keypoint.y << endl;
cout << "Save " << i << " keypoint suscessfully" << endl;
}
outFile.close();
cout << "Save the sequence suscessfully" << endl;
cout << "The output file saved in: " << work_dir + "data/image_points.txt" << endl;
return 0;
}