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 
45 class CV_UndistortPointsBadArgTest : public cvtest::BadArgTest
46 {
47 public:
48     CV_UndistortPointsBadArgTest();
49 protected:
50     void run(int);
51     void run_func();
52 
53 private:
54     //common
55     cv::Size img_size;
56     bool useCPlus;
57     //static const int N_POINTS = 1;
58     static const int N_POINTS2 = 2;
59 
60     //C
61     CvMat* _camera_mat;
62     CvMat* matR;
63     CvMat* matP;
64     CvMat* _distortion_coeffs;
65     CvMat* _src_points;
66     CvMat* _dst_points;
67 
68 
69     //C++
70     cv::Mat camera_mat;
71     cv::Mat R;
72     cv::Mat P;
73     cv::Mat distortion_coeffs;
74     cv::Mat src_points;
75     std::vector<cv::Point2f> dst_points;
76 
77 };
78 
CV_UndistortPointsBadArgTest()79 CV_UndistortPointsBadArgTest::CV_UndistortPointsBadArgTest ()
80 {
81     useCPlus = false;
82     _camera_mat = matR = matP = _distortion_coeffs = _src_points = _dst_points = NULL;
83 }
84 
run_func()85 void CV_UndistortPointsBadArgTest::run_func()
86 {
87     if (useCPlus)
88     {
89         cv::undistortPoints(src_points,dst_points,camera_mat,distortion_coeffs,R,P);
90     }
91     else
92     {
93         cvUndistortPoints(_src_points,_dst_points,_camera_mat,_distortion_coeffs,matR,matP);
94     }
95 }
96 
run(int)97 void CV_UndistortPointsBadArgTest::run(int)
98 {
99     //RNG& rng = ts->get_rng();
100     int errcount = 0;
101     useCPlus = false;
102 //initializing
103     img_size.width = 800;
104     img_size.height = 600;
105     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};
106     double dist[4] = {0.01,0.02,0.001,0.0005};
107     double s_points[N_POINTS2] = {
108         static_cast<double>(img_size.width) / 4.0,
109         static_cast<double>(img_size.height) / 4.0,
110     };
111     double d_points[N_POINTS2];
112     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};
113     double r[9] = {1,0,0,0,1,0,0,0,1};
114 
115     CvMat _camera_mat_orig = cvMat(3,3,CV_64F,cam);
116     CvMat _distortion_coeffs_orig = cvMat(1,4,CV_64F,dist);
117     CvMat _P_orig = cvMat(3,3,CV_64F,p);
118     CvMat _R_orig = cvMat(3,3,CV_64F,r);
119     CvMat _src_points_orig = cvMat(1,4,CV_64FC2,s_points);
120     CvMat _dst_points_orig = cvMat(1,4,CV_64FC2,d_points);
121 
122     _camera_mat = &_camera_mat_orig;
123     _distortion_coeffs = &_distortion_coeffs_orig;
124     matP = &_P_orig;
125     matR = &_R_orig;
126     _src_points = &_src_points_orig;
127     _dst_points = &_dst_points_orig;
128 
129 //tests
130     CvMat* temp1;
131     CvMat* temp;
132     IplImage* temp_img = cvCreateImage(cvSize(img_size.width,img_size.height),8,3);
133 
134 //-----------
135     temp = (CvMat*)temp_img;
136     _src_points = temp;
137     errcount += run_test_case( CV_StsAssert, "Input data is not CvMat*" );
138     _src_points = &_src_points_orig;
139 
140     temp = (CvMat*)temp_img;
141     _dst_points = temp;
142     errcount += run_test_case( CV_StsAssert, "Output data is not CvMat*" );
143     _dst_points = &_dst_points_orig;
144 
145     temp = cvCreateMat(2,3,CV_64F);
146     _src_points = temp;
147     errcount += run_test_case( CV_StsAssert, "Invalid input data matrix size" );
148     _src_points = &_src_points_orig;
149     cvReleaseMat(&temp);
150 
151     temp = cvCreateMat(2,3,CV_64F);
152     _dst_points = temp;
153     errcount += run_test_case(CV_StsAssert, "Invalid output data matrix size" );
154     _dst_points = &_dst_points_orig;
155     cvReleaseMat(&temp);
156 
157     temp = cvCreateMat(1,3,CV_64F);
158     temp1 = cvCreateMat(4,1,CV_64F);
159     _dst_points = temp;
160     _src_points = temp1;
161     errcount += run_test_case(CV_StsAssert, "Output and input data sizes mismatch" );
162     _dst_points = &_dst_points_orig;
163     _src_points = &_src_points_orig;
164     cvReleaseMat(&temp);
165     cvReleaseMat(&temp1);
166 
167     temp = cvCreateMat(1,3,CV_32S);
168     _dst_points = temp;
169     errcount += run_test_case(CV_StsAssert, "Invalid output data matrix type" );
170     _dst_points = &_dst_points_orig;
171     cvReleaseMat(&temp);
172 
173     temp = cvCreateMat(1,3,CV_32S);
174     _src_points = temp;
175     errcount += run_test_case(CV_StsAssert, "Invalid input data matrix type" );
176     _src_points = &_src_points_orig;
177     cvReleaseMat(&temp);
178 //------------
179     temp = cvCreateMat(2,3,CV_64F);
180     _camera_mat = temp;
181     errcount += run_test_case( CV_StsAssert, "Invalid camera data matrix size" );
182     _camera_mat = &_camera_mat_orig;
183     cvReleaseMat(&temp);
184 
185     temp = cvCreateMat(3,4,CV_64F);
186     _camera_mat = temp;
187     errcount += run_test_case( CV_StsAssert, "Invalid camera data matrix size" );
188     _camera_mat = &_camera_mat_orig;
189     cvReleaseMat(&temp);
190 
191     temp = (CvMat*)temp_img;
192     _camera_mat = temp;
193     errcount += run_test_case( CV_StsAssert, "Camera data is not CvMat*" );
194     _camera_mat = &_camera_mat_orig;
195 //----------
196 
197     temp = (CvMat*)temp_img;
198     _distortion_coeffs = temp;
199     errcount += run_test_case( CV_StsAssert, "Distortion coefficients data is not CvMat*" );
200     _distortion_coeffs = &_distortion_coeffs_orig;
201 
202     temp = cvCreateMat(1,6,CV_64F);
203     _distortion_coeffs = temp;
204     errcount += run_test_case( CV_StsAssert, "Invalid distortion coefficients data matrix size" );
205     _distortion_coeffs = &_distortion_coeffs_orig;
206     cvReleaseMat(&temp);
207 
208     temp = cvCreateMat(3,3,CV_64F);
209     _distortion_coeffs = temp;
210     errcount += run_test_case( CV_StsAssert, "Invalid distortion coefficients data matrix size" );
211     _distortion_coeffs = &_distortion_coeffs_orig;
212     cvReleaseMat(&temp);
213 //----------
214     temp = (CvMat*)temp_img;
215     matR = temp;
216     errcount += run_test_case( CV_StsAssert, "R data is not CvMat*" );
217     matR = &_R_orig;
218 
219     temp = cvCreateMat(4,3,CV_64F);
220     matR = temp;
221     errcount += run_test_case( CV_StsAssert, "Invalid R data matrix size" );
222     matR = &_R_orig;
223     cvReleaseMat(&temp);
224 
225     temp = cvCreateMat(3,2,CV_64F);
226     matR = temp;
227     errcount += run_test_case( CV_StsAssert, "Invalid R data matrix size" );
228     matR = &_R_orig;
229     cvReleaseMat(&temp);
230 
231 //-----------
232     temp = (CvMat*)temp_img;
233     matP = temp;
234     errcount += run_test_case( CV_StsAssert, "P data is not CvMat*" );
235     matP = &_P_orig;
236 
237     temp = cvCreateMat(4,3,CV_64F);
238     matP = temp;
239     errcount += run_test_case( CV_StsAssert, "Invalid P data matrix size" );
240     matP = &_P_orig;
241     cvReleaseMat(&temp);
242 
243     temp = cvCreateMat(3,2,CV_64F);
244     matP = temp;
245     errcount += run_test_case( CV_StsAssert, "Invalid P data matrix size" );
246     matP = &_P_orig;
247     cvReleaseMat(&temp);
248 //------------
249     //C++ tests
250     useCPlus = true;
251 
252     camera_mat = cv::cvarrToMat(&_camera_mat_orig);
253     distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig);
254     P = cv::cvarrToMat(&_P_orig);
255     R = cv::cvarrToMat(&_R_orig);
256     src_points = cv::cvarrToMat(&_src_points_orig);
257 
258     temp = cvCreateMat(2,2,CV_32FC2);
259     src_points = cv::cvarrToMat(temp);
260     errcount += run_test_case( CV_StsAssert, "Invalid input data matrix size" );
261     src_points = cv::cvarrToMat(&_src_points_orig);
262     cvReleaseMat(&temp);
263 
264     temp = cvCreateMat(1,4,CV_64FC2);
265     src_points = cv::cvarrToMat(temp);
266     errcount += run_test_case( CV_StsAssert, "Invalid input data matrix type" );
267     src_points = cv::cvarrToMat(&_src_points_orig);
268     cvReleaseMat(&temp);
269 
270     src_points = cv::Mat();
271     errcount += run_test_case( CV_StsAssert, "Input data matrix is not continuous" );
272     src_points = cv::cvarrToMat(&_src_points_orig);
273     cvReleaseMat(&temp);
274 
275 
276 
277 //------------
278     cvReleaseImage(&temp_img);
279     ts->set_failed_test_info(errcount > 0 ? cvtest::TS::FAIL_BAD_ARG_CHECK : cvtest::TS::OK);
280 }
281 
282 
283 //=========
284 class CV_InitUndistortRectifyMapBadArgTest : public cvtest::BadArgTest
285 {
286 public:
287     CV_InitUndistortRectifyMapBadArgTest();
288 protected:
289     void run(int);
290     void run_func();
291 
292 private:
293     //common
294     cv::Size img_size;
295     bool useCPlus;
296 
297     //C
298     CvMat* _camera_mat;
299     CvMat* matR;
300     CvMat* _new_camera_mat;
301     CvMat* _distortion_coeffs;
302     CvMat* _mapx;
303     CvMat* _mapy;
304 
305 
306     //C++
307     cv::Mat camera_mat;
308     cv::Mat R;
309     cv::Mat new_camera_mat;
310     cv::Mat distortion_coeffs;
311     cv::Mat mapx;
312     cv::Mat mapy;
313     int mat_type;
314 
315 };
316 
CV_InitUndistortRectifyMapBadArgTest()317 CV_InitUndistortRectifyMapBadArgTest::CV_InitUndistortRectifyMapBadArgTest ()
318 {
319     useCPlus = false;
320     _camera_mat = matR = _new_camera_mat = _distortion_coeffs = _mapx = _mapy = NULL;
321 }
322 
run_func()323 void CV_InitUndistortRectifyMapBadArgTest::run_func()
324 {
325     if (useCPlus)
326     {
327         cv::initUndistortRectifyMap(camera_mat,distortion_coeffs,R,new_camera_mat,img_size,mat_type,mapx,mapy);
328     }
329     else
330     {
331         cvInitUndistortRectifyMap(_camera_mat,_distortion_coeffs,matR,_new_camera_mat,_mapx,_mapy);
332     }
333 }
334 
run(int)335 void CV_InitUndistortRectifyMapBadArgTest::run(int)
336 {
337     int errcount = 0;
338 //initializing
339     img_size.width = 800;
340     img_size.height = 600;
341     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};
342     double dist[4] = {0.01,0.02,0.001,0.0005};
343     float* arr_mapx = new float[img_size.width*img_size.height];
344     float* arr_mapy = new float[img_size.width*img_size.height];
345     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};
346     double r[9] = {1,0,0,0,1,0,0,0,1};
347 
348     CvMat _camera_mat_orig = cvMat(3,3,CV_64F,cam);
349     CvMat _distortion_coeffs_orig = cvMat(1,4,CV_64F,dist);
350     CvMat _new_camera_mat_orig = cvMat(3,3,CV_64F,arr_new_camera_mat);
351     CvMat _R_orig = cvMat(3,3,CV_64F,r);
352     CvMat _mapx_orig = cvMat(img_size.height,img_size.width,CV_32FC1,arr_mapx);
353     CvMat _mapy_orig = cvMat(img_size.height,img_size.width,CV_32FC1,arr_mapy);
354     int mat_type_orig = CV_32FC1;
355 
356     _camera_mat = &_camera_mat_orig;
357     _distortion_coeffs = &_distortion_coeffs_orig;
358     _new_camera_mat = &_new_camera_mat_orig;
359     matR = &_R_orig;
360     _mapx = &_mapx_orig;
361     _mapy = &_mapy_orig;
362     mat_type = mat_type_orig;
363 
364 //tests
365     useCPlus = true;
366     CvMat* temp;
367 
368     //C++ tests
369     useCPlus = true;
370 
371     camera_mat = cv::cvarrToMat(&_camera_mat_orig);
372     distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig);
373     new_camera_mat = cv::cvarrToMat(&_new_camera_mat_orig);
374     R = cv::cvarrToMat(&_R_orig);
375     mapx = cv::cvarrToMat(&_mapx_orig);
376     mapy = cv::cvarrToMat(&_mapy_orig);
377 
378 
379     mat_type = CV_64F;
380     errcount += run_test_case( CV_StsAssert, "Invalid map matrix type" );
381     mat_type = mat_type_orig;
382 
383     temp = cvCreateMat(3,2,CV_32FC1);
384     camera_mat = cv::cvarrToMat(temp);
385     errcount += run_test_case( CV_StsAssert, "Invalid camera data matrix size" );
386     camera_mat = cv::cvarrToMat(&_camera_mat_orig);
387     cvReleaseMat(&temp);
388 
389     temp = cvCreateMat(4,3,CV_32FC1);
390     R = cv::cvarrToMat(temp);
391     errcount += run_test_case( CV_StsAssert, "Invalid R data matrix size" );
392     R = cv::cvarrToMat(&_R_orig);
393     cvReleaseMat(&temp);
394 
395     temp = cvCreateMat(6,1,CV_32FC1);
396     distortion_coeffs = cv::cvarrToMat(temp);
397     errcount += run_test_case( CV_StsAssert, "Invalid distortion coefficients data matrix size" );
398     distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig);
399     cvReleaseMat(&temp);
400 
401 //------------
402     delete[] arr_mapx;
403     delete[] arr_mapy;
404     ts->set_failed_test_info(errcount > 0 ? cvtest::TS::FAIL_BAD_ARG_CHECK : cvtest::TS::OK);
405 }
406 
407 
408 //=========
409 class CV_UndistortBadArgTest : public cvtest::BadArgTest
410 {
411 public:
412     CV_UndistortBadArgTest();
413 protected:
414     void run(int);
415     void run_func();
416 
417 private:
418     //common
419     cv::Size img_size;
420     bool useCPlus;
421 
422     //C
423     CvMat* _camera_mat;
424     CvMat* _new_camera_mat;
425     CvMat* _distortion_coeffs;
426     CvMat* _src;
427     CvMat* _dst;
428 
429 
430     //C++
431     cv::Mat camera_mat;
432     cv::Mat new_camera_mat;
433     cv::Mat distortion_coeffs;
434     cv::Mat src;
435     cv::Mat dst;
436 
437 };
438 
CV_UndistortBadArgTest()439 CV_UndistortBadArgTest::CV_UndistortBadArgTest ()
440 {
441     useCPlus = false;
442     _camera_mat = _new_camera_mat = _distortion_coeffs = _src = _dst = NULL;
443 }
444 
run_func()445 void CV_UndistortBadArgTest::run_func()
446 {
447     if (useCPlus)
448     {
449         cv::undistort(src,dst,camera_mat,distortion_coeffs,new_camera_mat);
450     }
451     else
452     {
453         cvUndistort2(_src,_dst,_camera_mat,_distortion_coeffs,_new_camera_mat);
454     }
455 }
456 
run(int)457 void CV_UndistortBadArgTest::run(int)
458 {
459     int errcount = 0;
460 //initializing
461     img_size.width = 800;
462     img_size.height = 600;
463     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};
464     double dist[4] = {0.01,0.02,0.001,0.0005};
465     float* arr_src = new float[img_size.width*img_size.height];
466     float* arr_dst = new float[img_size.width*img_size.height];
467     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};
468 
469     CvMat _camera_mat_orig = cvMat(3,3,CV_64F,cam);
470     CvMat _distortion_coeffs_orig = cvMat(1,4,CV_64F,dist);
471     CvMat _new_camera_mat_orig = cvMat(3,3,CV_64F,arr_new_camera_mat);
472     CvMat _src_orig = cvMat(img_size.height,img_size.width,CV_32FC1,arr_src);
473     CvMat _dst_orig = cvMat(img_size.height,img_size.width,CV_32FC1,arr_dst);
474 
475     _camera_mat = &_camera_mat_orig;
476     _distortion_coeffs = &_distortion_coeffs_orig;
477     _new_camera_mat = &_new_camera_mat_orig;
478     _src = &_src_orig;
479     _dst = &_dst_orig;
480 
481 //tests
482     useCPlus = true;
483     CvMat* temp;
484     CvMat* temp1;
485 
486 //C tests
487     useCPlus = false;
488 
489     temp = cvCreateMat(800,600,CV_32F);
490     temp1 = cvCreateMat(800,601,CV_32F);
491     _src = temp;
492     _dst = temp1;
493     errcount += run_test_case( CV_StsAssert, "Input and output data matrix sizes mismatch" );
494     _src = &_src_orig;
495     _dst = &_dst_orig;
496     cvReleaseMat(&temp);
497     cvReleaseMat(&temp1);
498 
499     temp = cvCreateMat(800,600,CV_32F);
500     temp1 = cvCreateMat(800,600,CV_64F);
501     _src = temp;
502     _dst = temp1;
503     errcount += run_test_case( CV_StsAssert, "Input and output data matrix types mismatch" );
504     _src = &_src_orig;
505     _dst = &_dst_orig;
506     cvReleaseMat(&temp);
507     cvReleaseMat(&temp1);
508 
509     //C++ tests
510     useCPlus = true;
511 
512     camera_mat = cv::cvarrToMat(&_camera_mat_orig);
513     distortion_coeffs = cv::cvarrToMat(&_distortion_coeffs_orig);
514     new_camera_mat = cv::cvarrToMat(&_new_camera_mat_orig);
515     src = cv::cvarrToMat(&_src_orig);
516     dst = cv::cvarrToMat(&_dst_orig);
517 
518 //------------
519     delete[] arr_src;
520     delete[] arr_dst;
521     ts->set_failed_test_info(errcount > 0 ? cvtest::TS::FAIL_BAD_ARG_CHECK : cvtest::TS::OK);
522 }
523 
TEST(Calib3d_UndistortPoints,badarg)524 TEST(Calib3d_UndistortPoints, badarg) { CV_UndistortPointsBadArgTest test; test.safe_run(); }
TEST(Calib3d_InitUndistortRectifyMap,badarg)525 TEST(Calib3d_InitUndistortRectifyMap, badarg) { CV_InitUndistortRectifyMapBadArgTest test; test.safe_run(); }
TEST(Calib3d_Undistort,badarg)526 TEST(Calib3d_Undistort, badarg) { CV_UndistortBadArgTest test; test.safe_run(); }
527 
528 /* End of file. */
529