Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/test/test_cornerssubpix.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// Intel License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of Intel Corporation may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#include "test_precomp.hpp"
43
#include "opencv2/imgproc/imgproc_c.h"
44
#include "test_chessboardgenerator.hpp"
45
46
namespace opencv_test { namespace {
47
48
class CV_ChessboardSubpixelTest : public cvtest::BaseTest
49
{
50
public:
51
CV_ChessboardSubpixelTest();
52
53
protected:
54
Mat intrinsic_matrix_;
55
Mat distortion_coeffs_;
56
Size image_size_;
57
58
void run(int);
59
void generateIntrinsicParams();
60
};
61
62
63
int calcDistance(const vector<Point2f>& set1, const vector<Point2f>& set2, double& mean_dist)
64
{
65
if(set1.size() != set2.size())
66
{
67
return 0;
68
}
69
70
std::vector<int> indices;
71
double sum_dist = 0.0;
72
for(size_t i = 0; i < set1.size(); i++)
73
{
74
double min_dist = std::numeric_limits<double>::max();
75
int min_idx = -1;
76
77
for(int j = 0; j < (int)set2.size(); j++)
78
{
79
double dist = cv::norm(set1[i] - set2[j]); // TODO cvtest
80
if(dist < min_dist)
81
{
82
min_idx = j;
83
min_dist = dist;
84
}
85
}
86
87
// check validity of min_idx
88
if(min_idx == -1)
89
{
90
return 0;
91
}
92
std::vector<int>::iterator it = std::find(indices.begin(), indices.end(), min_idx);
93
if(it != indices.end())
94
{
95
// there are two points in set1 corresponding to the same point in set2
96
return 0;
97
}
98
indices.push_back(min_idx);
99
100
// printf("dist %d = %f\n", (int)i, min_dist);
101
102
sum_dist += min_dist*min_dist;
103
}
104
105
mean_dist = sqrt(sum_dist/set1.size());
106
// printf("sum_dist = %f, set1.size() = %d, mean_dist = %f\n", sum_dist, (int)set1.size(), mean_dist);
107
108
return 1;
109
}
110
111
CV_ChessboardSubpixelTest::CV_ChessboardSubpixelTest() :
112
intrinsic_matrix_(Size(3, 3), CV_64FC1), distortion_coeffs_(Size(1, 4), CV_64FC1),
113
image_size_(640, 480)
114
{
115
}
116
117
/* ///////////////////// chess_corner_test ///////////////////////// */
118
void CV_ChessboardSubpixelTest::run( int )
119
{
120
int code = cvtest::TS::OK;
121
int progress = 0;
122
123
RNG& rng = ts->get_rng();
124
125
const int runs_count = 20;
126
const int max_pattern_size = 8;
127
const int min_pattern_size = 5;
128
Mat bg(image_size_, CV_8UC1);
129
bg = Scalar(0);
130
131
double sum_dist = 0.0;
132
int count = 0;
133
for(int i = 0; i < runs_count; i++)
134
{
135
const int pattern_width = min_pattern_size + cvtest::randInt(rng) % (max_pattern_size - min_pattern_size);
136
const int pattern_height = min_pattern_size + cvtest::randInt(rng) % (max_pattern_size - min_pattern_size);
137
Size pattern_size;
138
if(pattern_width > pattern_height)
139
{
140
pattern_size = Size(pattern_height, pattern_width);
141
}
142
else
143
{
144
pattern_size = Size(pattern_width, pattern_height);
145
}
146
ChessBoardGenerator gen_chessboard(Size(pattern_size.width + 1, pattern_size.height + 1));
147
148
// generates intrinsic camera and distortion matrices
149
generateIntrinsicParams();
150
151
vector<Point2f> corners;
152
Mat chessboard_image = gen_chessboard(bg, intrinsic_matrix_, distortion_coeffs_, corners);
153
154
vector<Point2f> test_corners;
155
bool result = findChessboardCorners(chessboard_image, pattern_size, test_corners, 15);
156
if(!result)
157
{
158
#if 0
159
ts->printf(cvtest::TS::LOG, "Warning: chessboard was not detected! Writing image to test.png\n");
160
ts->printf(cvtest::TS::LOG, "Size = %d, %d\n", pattern_size.width, pattern_size.height);
161
ts->printf(cvtest::TS::LOG, "Intrinsic params: fx = %f, fy = %f, cx = %f, cy = %f\n",
162
intrinsic_matrix_.at<double>(0, 0), intrinsic_matrix_.at<double>(1, 1),
163
intrinsic_matrix_.at<double>(0, 2), intrinsic_matrix_.at<double>(1, 2));
164
ts->printf(cvtest::TS::LOG, "Distortion matrix: %f, %f, %f, %f, %f\n",
165
distortion_coeffs_.at<double>(0, 0), distortion_coeffs_.at<double>(0, 1),
166
distortion_coeffs_.at<double>(0, 2), distortion_coeffs_.at<double>(0, 3),
167
distortion_coeffs_.at<double>(0, 4));
168
169
imwrite("test.png", chessboard_image);
170
#endif
171
continue;
172
}
173
174
double dist1 = 0.0;
175
int ret = calcDistance(corners, test_corners, dist1);
176
if(ret == 0)
177
{
178
ts->printf(cvtest::TS::LOG, "findChessboardCorners returns invalid corner coordinates!\n");
179
code = cvtest::TS::FAIL_INVALID_OUTPUT;
180
break;
181
}
182
183
IplImage chessboard_image_header = cvIplImage(chessboard_image);
184
cvFindCornerSubPix(&chessboard_image_header, (CvPoint2D32f*)&test_corners[0],
185
(int)test_corners.size(), cvSize(3, 3), cvSize(1, 1), cvTermCriteria(CV_TERMCRIT_EPS|CV_TERMCRIT_ITER,300,0.1));
186
find4QuadCornerSubpix(chessboard_image, test_corners, Size(5, 5));
187
188
double dist2 = 0.0;
189
ret = calcDistance(corners, test_corners, dist2);
190
if(ret == 0)
191
{
192
ts->printf(cvtest::TS::LOG, "findCornerSubpix returns invalid corner coordinates!\n");
193
code = cvtest::TS::FAIL_INVALID_OUTPUT;
194
break;
195
}
196
197
ts->printf(cvtest::TS::LOG, "Error after findChessboardCorners: %f, after findCornerSubPix: %f\n",
198
dist1, dist2);
199
sum_dist += dist2;
200
count++;
201
202
const double max_reduce_factor = 0.8;
203
if(dist1 < dist2*max_reduce_factor)
204
{
205
ts->printf(cvtest::TS::LOG, "findCornerSubPix increases average error!\n");
206
code = cvtest::TS::FAIL_INVALID_OUTPUT;
207
break;
208
}
209
210
progress = update_progress( progress, i-1, runs_count, 0 );
211
}
212
ASSERT_NE(0, count);
213
sum_dist /= count;
214
ts->printf(cvtest::TS::LOG, "Average error after findCornerSubpix: %f\n", sum_dist);
215
216
if( code < 0 )
217
ts->set_failed_test_info( code );
218
}
219
220
void CV_ChessboardSubpixelTest::generateIntrinsicParams()
221
{
222
RNG& rng = ts->get_rng();
223
const double max_focus_length = 1000.0;
224
const double max_focus_diff = 5.0;
225
226
double fx = cvtest::randReal(rng)*max_focus_length;
227
double fy = fx + cvtest::randReal(rng)*max_focus_diff;
228
double cx = image_size_.width/2;
229
double cy = image_size_.height/2;
230
231
double k1 = 0.5*cvtest::randReal(rng);
232
double k2 = 0.05*cvtest::randReal(rng);
233
double p1 = 0.05*cvtest::randReal(rng);
234
double p2 = 0.05*cvtest::randReal(rng);
235
double k3 = 0.0;
236
237
intrinsic_matrix_ = (Mat_<double>(3, 3) << fx, 0.0, cx, 0.0, fy, cy, 0.0, 0.0, 1.0);
238
distortion_coeffs_ = (Mat_<double>(1, 5) << k1, k2, p1, p2, k3);
239
}
240
241
TEST(Calib3d_ChessboardSubPixDetector, accuracy) { CV_ChessboardSubpixelTest test; test.safe_run(); }
242
243
TEST(Calib3d_CornerSubPix, regression_7204)
244
{
245
cv::Mat image(cv::Size(70, 38), CV_8UC1, cv::Scalar::all(0));
246
image(cv::Rect(65, 26, 5, 5)).setTo(cv::Scalar::all(255));
247
image(cv::Rect(55, 31, 8, 1)).setTo(cv::Scalar::all(255));
248
image(cv::Rect(56, 35, 14, 2)).setTo(cv::Scalar::all(255));
249
image(cv::Rect(66, 24, 4, 2)).setTo(cv::Scalar::all(255));
250
image.at<uchar>(24, 69) = 0;
251
std::vector<cv::Point2f> corners;
252
corners.push_back(cv::Point2f(65, 30));
253
cv::cornerSubPix(image, corners, cv::Size(3, 3), cv::Size(-1, -1),
254
cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
255
}
256
257
}} // namespace
258
/* End of file. */
259
260