Merge remote-tracking branch 'upstream/3.4' into merge-3.4
[platform/upstream/opencv.git] / modules / calib3d / test / test_undistort_badarg.cpp
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/core/core_c.h"
44
45 namespace opencv_test { namespace {
46
47 class CV_UndistortPointsBadArgTest : public cvtest::BadArgTest
48 {
49 public:
50     CV_UndistortPointsBadArgTest();
51 protected:
52     void run(int);
53     void run_func();
54
55 private:
56     //common
57     cv::Size img_size;
58     //static const int N_POINTS = 1;
59     static const int N_POINTS2 = 2;
60
61     //C++
62     cv::Mat camera_mat;
63     cv::Mat R;
64     cv::Mat P;
65     cv::Mat distortion_coeffs;
66     cv::Mat src_points;
67     std::vector<cv::Point2f> dst_points;
68
69 };
70
71 CV_UndistortPointsBadArgTest::CV_UndistortPointsBadArgTest ()
72 {
73 }
74
75 void CV_UndistortPointsBadArgTest::run_func()
76 {
77     cv::undistortPoints(src_points,dst_points,camera_mat,distortion_coeffs,R,P);
78 }
79
80 void CV_UndistortPointsBadArgTest::run(int)
81 {
82     //RNG& rng = ts->get_rng();
83     int errcount = 0;
84 //initializing
85     img_size.width = 800;
86     img_size.height = 600;
87     double cam[9] = {150.f, 0.f, img_size.width/2.f, 0, 300.f, img_size.height/2.f, 0.f, 0.f, 1.f};
88     double dist[4] = {0.01,0.02,0.001,0.0005};
89     double s_points[N_POINTS2] = {
90         static_cast<double>(img_size.width) / 4.0,
91         static_cast<double>(img_size.height) / 4.0,
92     };
93     double p[9] = {155.f, 0.f, img_size.width/2.f+img_size.width/50.f, 0, 310.f, img_size.height/2.f+img_size.height/50.f, 0.f, 0.f, 1.f};
94     double r[9] = {1,0,0,0,1,0,0,0,1};
95
96     CvMat _camera_mat_orig = cvMat(3,3,CV_64F,cam);
97     CvMat _distortion_coeffs_orig = cvMat(1,4,CV_64F,dist);
98     CvMat _P_orig = cvMat(3,3,CV_64F,p);
99     CvMat _R_orig = cvMat(3,3,CV_64F,r);
100     CvMat _src_points_orig = cvMat(1,4,CV_64FC2,s_points);
101
102     camera_mat = cv::cvarrToMat(&_camera_mat_orig);
103     distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig);
104     P = cv::cvarrToMat(&_P_orig);
105     R = cv::cvarrToMat(&_R_orig);
106     src_points = cv::cvarrToMat(&_src_points_orig);
107
108     src_points.create(2, 2, CV_32FC2);
109     errcount += run_test_case( CV_StsAssert, "Invalid input data matrix size" );
110     src_points = cv::cvarrToMat(&_src_points_orig);
111
112     src_points.create(1, 4, CV_64FC2);
113     errcount += run_test_case( CV_StsAssert, "Invalid input data matrix type" );
114     src_points = cv::cvarrToMat(&_src_points_orig);
115
116     src_points = cv::Mat();
117     errcount += run_test_case( CV_StsBadArg, "Input data matrix is not continuous" );
118     src_points = cv::cvarrToMat(&_src_points_orig);
119
120 //------------
121     ts->set_failed_test_info(errcount > 0 ? cvtest::TS::FAIL_BAD_ARG_CHECK : cvtest::TS::OK);
122 }
123
124
125 //=========
126 class CV_InitUndistortRectifyMapBadArgTest : public cvtest::BadArgTest
127 {
128 public:
129     CV_InitUndistortRectifyMapBadArgTest();
130 protected:
131     void run(int);
132     void run_func();
133
134 private:
135     cv::Size img_size;
136     cv::Mat camera_mat;
137     cv::Mat R;
138     cv::Mat new_camera_mat;
139     cv::Mat distortion_coeffs;
140     cv::Mat mapx;
141     cv::Mat mapy;
142     int mat_type;
143
144 };
145
146 CV_InitUndistortRectifyMapBadArgTest::CV_InitUndistortRectifyMapBadArgTest ()
147 {
148 }
149
150 void CV_InitUndistortRectifyMapBadArgTest::run_func()
151 {
152     cv::initUndistortRectifyMap(camera_mat,distortion_coeffs,R,new_camera_mat,img_size,mat_type,mapx,mapy);
153 }
154
155 void CV_InitUndistortRectifyMapBadArgTest::run(int)
156 {
157     int errcount = 0;
158 //initializing
159     img_size.width = 800;
160     img_size.height = 600;
161     double cam[9] = {150.f, 0.f, img_size.width/2.f, 0, 300.f, img_size.height/2.f, 0.f, 0.f, 1.f};
162     double dist[4] = {0.01,0.02,0.001,0.0005};
163     std::vector<float> arr_mapx(img_size.width*img_size.height);
164     std::vector<float> arr_mapy(img_size.width*img_size.height);
165     double arr_new_camera_mat[9] = {155.f, 0.f, img_size.width/2.f+img_size.width/50.f, 0, 310.f, img_size.height/2.f+img_size.height/50.f, 0.f, 0.f, 1.f};
166     double r[9] = {1,0,0,0,1,0,0,0,1};
167
168     CvMat _camera_mat_orig = cvMat(3,3,CV_64F,cam);
169     CvMat _distortion_coeffs_orig = cvMat(1,4,CV_64F,dist);
170     CvMat _new_camera_mat_orig = cvMat(3,3,CV_64F,arr_new_camera_mat);
171     CvMat _R_orig = cvMat(3,3,CV_64F,r);
172     CvMat _mapx_orig = cvMat(img_size.height,img_size.width,CV_32FC1,&arr_mapx[0]);
173     CvMat _mapy_orig = cvMat(img_size.height,img_size.width,CV_32FC1,&arr_mapy[0]);
174     int mat_type_orig = CV_32FC1;
175
176     camera_mat = cv::cvarrToMat(&_camera_mat_orig);
177     distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig);
178     new_camera_mat = cv::cvarrToMat(&_new_camera_mat_orig);
179     R = cv::cvarrToMat(&_R_orig);
180     mapx = cv::cvarrToMat(&_mapx_orig);
181     mapy = cv::cvarrToMat(&_mapy_orig);
182
183     mat_type = CV_64F;
184     errcount += run_test_case( CV_StsAssert, "Invalid map matrix type" );
185     mat_type = mat_type_orig;
186
187     camera_mat.create(3, 2, CV_32F);
188     errcount += run_test_case( CV_StsAssert, "Invalid camera data matrix size" );
189     camera_mat = cv::cvarrToMat(&_camera_mat_orig);
190
191     R.create(4, 3, CV_32F);
192     errcount += run_test_case( CV_StsAssert, "Invalid R data matrix size" );
193     R = cv::cvarrToMat(&_R_orig);
194
195     distortion_coeffs.create(6, 1, CV_32F);
196     errcount += run_test_case( CV_StsAssert, "Invalid distortion coefficients data matrix size" );
197     distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig);
198
199 //------------
200     ts->set_failed_test_info(errcount > 0 ? cvtest::TS::FAIL_BAD_ARG_CHECK : cvtest::TS::OK);
201 }
202
203
204 //=========
205 class CV_UndistortBadArgTest : public cvtest::BadArgTest
206 {
207 public:
208     CV_UndistortBadArgTest();
209 protected:
210     void run(int);
211     void run_func();
212
213 private:
214     //common
215     cv::Size img_size;
216
217     cv::Mat camera_mat;
218     cv::Mat new_camera_mat;
219     cv::Mat distortion_coeffs;
220     cv::Mat src;
221     cv::Mat dst;
222
223 };
224
225 CV_UndistortBadArgTest::CV_UndistortBadArgTest ()
226 {
227 }
228
229 void CV_UndistortBadArgTest::run_func()
230 {
231     cv::undistort(src,dst,camera_mat,distortion_coeffs,new_camera_mat);
232 }
233
234 void CV_UndistortBadArgTest::run(int)
235 {
236     int errcount = 0;
237 //initializing
238     img_size.width = 800;
239     img_size.height = 600;
240     double cam[9] = {150.f, 0.f, img_size.width/2.f, 0, 300.f, img_size.height/2.f, 0.f, 0.f, 1.f};
241     double dist[4] = {0.01,0.02,0.001,0.0005};
242     std::vector<float> arr_src(img_size.width*img_size.height);
243     std::vector<float> arr_dst(img_size.width*img_size.height);
244     double arr_new_camera_mat[9] = {155.f, 0.f, img_size.width/2.f+img_size.width/50.f, 0, 310.f, img_size.height/2.f+img_size.height/50.f, 0.f, 0.f, 1.f};
245
246     CvMat _camera_mat_orig = cvMat(3,3,CV_64F,cam);
247     CvMat _distortion_coeffs_orig = cvMat(1,4,CV_64F,dist);
248     CvMat _new_camera_mat_orig = cvMat(3,3,CV_64F,arr_new_camera_mat);
249     CvMat _src_orig = cvMat(img_size.height,img_size.width,CV_32FC1,&arr_src[0]);
250     CvMat _dst_orig = cvMat(img_size.height,img_size.width,CV_32FC1,&arr_dst[0]);
251
252     camera_mat = cv::cvarrToMat(&_camera_mat_orig);
253     distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig);
254     new_camera_mat = cv::cvarrToMat(&_new_camera_mat_orig);
255     src = cv::cvarrToMat(&_src_orig);
256     dst = cv::cvarrToMat(&_dst_orig);
257
258     camera_mat.create(5, 5, CV_64F);
259     errcount += run_test_case( CV_StsAssert, "Invalid camera data matrix size" );
260
261 //------------
262     ts->set_failed_test_info(errcount > 0 ? cvtest::TS::FAIL_BAD_ARG_CHECK : cvtest::TS::OK);
263 }
264
265 TEST(Calib3d_UndistortPoints, badarg) { CV_UndistortPointsBadArgTest test; test.safe_run(); }
266 TEST(Calib3d_InitUndistortRectifyMap, badarg) { CV_InitUndistortRectifyMapBadArgTest test; test.safe_run(); }
267 TEST(Calib3d_Undistort, badarg) { CV_UndistortBadArgTest test; test.safe_run(); }
268
269 }} // namespace
270 /* End of file. */