• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <limits>
45 #include "test_chessboardgenerator.hpp"
46 
47 using namespace std;
48 using namespace cv;
49 
50 class CV_ChessboardSubpixelTest : public cvtest::BaseTest
51 {
52 public:
53     CV_ChessboardSubpixelTest();
54 
55 protected:
56     Mat intrinsic_matrix_;
57     Mat distortion_coeffs_;
58     Size image_size_;
59 
60     void run(int);
61     void generateIntrinsicParams();
62 };
63 
64 
calcDistance(const vector<Point2f> & set1,const vector<Point2f> & set2,double & mean_dist)65 int calcDistance(const vector<Point2f>& set1, const vector<Point2f>& set2, double& mean_dist)
66 {
67     if(set1.size() != set2.size())
68     {
69         return 0;
70     }
71 
72     std::vector<int> indices;
73     double sum_dist = 0.0;
74     for(size_t i = 0; i < set1.size(); i++)
75     {
76         double min_dist = std::numeric_limits<double>::max();
77         int min_idx = -1;
78 
79         for(int j = 0; j < (int)set2.size(); j++)
80         {
81             double dist = norm(set1[i] - set2[j]);
82             if(dist < min_dist)
83             {
84                 min_idx = j;
85                 min_dist = dist;
86             }
87         }
88 
89         // check validity of min_idx
90         if(min_idx == -1)
91         {
92             return 0;
93         }
94         std::vector<int>::iterator it = std::find(indices.begin(), indices.end(), min_idx);
95         if(it != indices.end())
96         {
97             // there are two points in set1 corresponding to the same point in set2
98             return 0;
99         }
100         indices.push_back(min_idx);
101 
102 //        printf("dist %d = %f\n", (int)i, min_dist);
103 
104         sum_dist += min_dist*min_dist;
105     }
106 
107     mean_dist = sqrt(sum_dist/set1.size());
108 //    printf("sum_dist = %f, set1.size() = %d, mean_dist = %f\n", sum_dist, (int)set1.size(), mean_dist);
109 
110     return 1;
111 }
112 
CV_ChessboardSubpixelTest()113 CV_ChessboardSubpixelTest::CV_ChessboardSubpixelTest() :
114     intrinsic_matrix_(Size(3, 3), CV_64FC1), distortion_coeffs_(Size(1, 4), CV_64FC1),
115     image_size_(640, 480)
116 {
117 }
118 
119 /* ///////////////////// chess_corner_test ///////////////////////// */
run(int)120 void CV_ChessboardSubpixelTest::run( int )
121 {
122     int code = cvtest::TS::OK;
123     int  progress = 0;
124 
125     RNG& rng = ts->get_rng();
126 
127     const int runs_count = 20;
128     const int max_pattern_size = 8;
129     const int min_pattern_size = 5;
130     Mat bg(image_size_, CV_8UC1);
131     bg = Scalar(0);
132 
133     double sum_dist = 0.0;
134     int count = 0;
135     for(int i = 0; i < runs_count; i++)
136     {
137         const int pattern_width = min_pattern_size + cvtest::randInt(rng) % (max_pattern_size - min_pattern_size);
138         const int pattern_height = min_pattern_size + cvtest::randInt(rng) % (max_pattern_size - min_pattern_size);
139         Size pattern_size;
140         if(pattern_width > pattern_height)
141         {
142             pattern_size = Size(pattern_height, pattern_width);
143         }
144         else
145         {
146             pattern_size = Size(pattern_width, pattern_height);
147         }
148         ChessBoardGenerator gen_chessboard(Size(pattern_size.width + 1, pattern_size.height + 1));
149 
150         // generates intrinsic camera and distortion matrices
151         generateIntrinsicParams();
152 
153         vector<Point2f> corners;
154         Mat chessboard_image = gen_chessboard(bg, intrinsic_matrix_, distortion_coeffs_, corners);
155 
156         vector<Point2f> test_corners;
157         bool result = findChessboardCorners(chessboard_image, pattern_size, test_corners, 15);
158         if(!result)
159         {
160 #if 0
161             ts->printf(cvtest::TS::LOG, "Warning: chessboard was not detected! Writing image to test.png\n");
162             ts->printf(cvtest::TS::LOG, "Size = %d, %d\n", pattern_size.width, pattern_size.height);
163             ts->printf(cvtest::TS::LOG, "Intrinsic params: fx = %f, fy = %f, cx = %f, cy = %f\n",
164                        intrinsic_matrix_.at<double>(0, 0), intrinsic_matrix_.at<double>(1, 1),
165                        intrinsic_matrix_.at<double>(0, 2), intrinsic_matrix_.at<double>(1, 2));
166             ts->printf(cvtest::TS::LOG, "Distortion matrix: %f, %f, %f, %f, %f\n",
167                        distortion_coeffs_.at<double>(0, 0), distortion_coeffs_.at<double>(0, 1),
168                        distortion_coeffs_.at<double>(0, 2), distortion_coeffs_.at<double>(0, 3),
169                        distortion_coeffs_.at<double>(0, 4));
170 
171             imwrite("test.png", chessboard_image);
172 #endif
173             continue;
174         }
175 
176         double dist1 = 0.0;
177         int ret = calcDistance(corners, test_corners, dist1);
178         if(ret == 0)
179         {
180             ts->printf(cvtest::TS::LOG, "findChessboardCorners returns invalid corner coordinates!\n");
181             code = cvtest::TS::FAIL_INVALID_OUTPUT;
182             break;
183         }
184 
185         IplImage chessboard_image_header = chessboard_image;
186         cvFindCornerSubPix(&chessboard_image_header, (CvPoint2D32f*)&test_corners[0],
187             (int)test_corners.size(), cvSize(3, 3), cvSize(1, 1), cvTermCriteria(CV_TERMCRIT_EPS|CV_TERMCRIT_ITER,300,0.1));
188         find4QuadCornerSubpix(chessboard_image, test_corners, Size(5, 5));
189 
190         double dist2 = 0.0;
191         ret = calcDistance(corners, test_corners, dist2);
192         if(ret == 0)
193         {
194             ts->printf(cvtest::TS::LOG, "findCornerSubpix returns invalid corner coordinates!\n");
195             code = cvtest::TS::FAIL_INVALID_OUTPUT;
196             break;
197         }
198 
199         ts->printf(cvtest::TS::LOG, "Error after findChessboardCorners: %f, after findCornerSubPix: %f\n",
200                    dist1, dist2);
201         sum_dist += dist2;
202         count++;
203 
204         const double max_reduce_factor = 0.8;
205         if(dist1 < dist2*max_reduce_factor)
206         {
207             ts->printf(cvtest::TS::LOG, "findCornerSubPix increases average error!\n");
208             code = cvtest::TS::FAIL_INVALID_OUTPUT;
209             break;
210         }
211 
212         progress = update_progress( progress, i-1, runs_count, 0 );
213     }
214     ASSERT_NE(0, count);
215     sum_dist /= count;
216     ts->printf(cvtest::TS::LOG, "Average error after findCornerSubpix: %f\n", sum_dist);
217 
218     if( code < 0 )
219         ts->set_failed_test_info( code );
220 }
221 
generateIntrinsicParams()222 void CV_ChessboardSubpixelTest::generateIntrinsicParams()
223 {
224     RNG& rng = ts->get_rng();
225     const double max_focus_length = 1000.0;
226     const double max_focus_diff = 5.0;
227 
228     double fx = cvtest::randReal(rng)*max_focus_length;
229     double fy = fx + cvtest::randReal(rng)*max_focus_diff;
230     double cx = image_size_.width/2;
231     double cy = image_size_.height/2;
232 
233     double k1 = 0.5*cvtest::randReal(rng);
234     double k2 = 0.05*cvtest::randReal(rng);
235     double p1 = 0.05*cvtest::randReal(rng);
236     double p2 = 0.05*cvtest::randReal(rng);
237     double k3 = 0.0;
238 
239     intrinsic_matrix_ = (Mat_<double>(3, 3) << fx, 0.0, cx, 0.0, fy, cy, 0.0, 0.0, 1.0);
240     distortion_coeffs_ = (Mat_<double>(1, 5) << k1, k2, p1, p2, k3);
241 }
242 
TEST(Calib3d_ChessboardSubPixDetector,accuracy)243 TEST(Calib3d_ChessboardSubPixDetector, accuracy) { CV_ChessboardSubpixelTest test; test.safe_run(); }
244 
245 /* End of file. */
246