1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
44 static const int imgwarp_depths[] = { CV_8U, CV_16U, CV_32F, -1 };
45 static const int imgwarp_channels[] = { 1, 3, 4, -1 };
46 static const CvSize imgwarp_sizes[] = {{320, 240}, {1024,768}, {-1,-1}};
48 static const double imgwarp_resize_coeffs[] = { 0.5, 0.333, 2, 2.9 };
49 static const char* imgwarp_resize_methods[] = { "nearest", "linear", "cubic", "area", 0 };
50 static const char* imgwarp_resize_param_names[] = { "method", "coeff", "size", "channels", "depth", 0 };
52 static const double imgwarp_affine_rotate_scale[][4] = { {0.5,0.5,30.,1.4}, {0.5,0.5,-130,0.4}, {-1,-1,-1,-1} };
53 static const char* imgwarp_affine_param_names[] = { "rotate_scale", "size", "channels", "depth", 0 };
55 static const double imgwarp_perspective_shift_vtx[][8] = { {0.03,0.01,0.04,0.02,0.01,0.01,0.01,0.02}, {-1} };
56 static const char* imgwarp_perspective_param_names[] = { "shift_vtx", "size", "channels", "depth", 0 };
58 class CV_ImgWarpBaseTestImpl : public CvArrTest
61 CV_ImgWarpBaseTestImpl( const char* test_name, const char* test_funcs, bool warp_matrix );
64 int read_params( CvFileStorage* fs );
65 int prepare_test_case( int test_case_idx );
66 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
67 void get_minmax_bounds( int i, int j, int type, CvScalar* low, CvScalar* high );
68 void fill_array( int test_case_idx, int i, int j, CvMat* arr );
71 int max_interpolation;
72 double spatial_scale_zoom, spatial_scale_decimate;
76 CV_ImgWarpBaseTestImpl::CV_ImgWarpBaseTestImpl( const char* test_name, const char* test_funcs, bool warp_matrix )
77 : CvArrTest( test_name, test_funcs, "" )
79 test_array[INPUT].push(NULL);
81 test_array[INPUT].push(NULL);
82 test_array[INPUT_OUTPUT].push(NULL);
83 test_array[REF_INPUT_OUTPUT].push(NULL);
84 max_interpolation = 4;
86 element_wise_relative_error = false;
87 spatial_scale_zoom = 0.01;
88 spatial_scale_decimate = 0.005;
90 size_list = whole_size_list = imgwarp_sizes;
91 depth_list = imgwarp_depths;
92 cn_list = imgwarp_channels;
93 default_timing_param_names = 0;
97 int CV_ImgWarpBaseTestImpl::read_params( CvFileStorage* fs )
99 int code = CvArrTest::read_params( fs );
104 void CV_ImgWarpBaseTestImpl::get_minmax_bounds( int i, int j, int type, CvScalar* low, CvScalar* high )
106 CvArrTest::get_minmax_bounds( i, j, type, low, high );
107 if( CV_MAT_DEPTH(type) == CV_32F )
109 *low = cvScalarAll(-10.);
110 *high = cvScalarAll(10);
115 void CV_ImgWarpBaseTestImpl::get_test_array_types_and_sizes( int test_case_idx,
116 CvSize** sizes, int** types )
118 CvRNG* rng = ts->get_rng();
119 int depth = cvTsRandInt(rng) % 3;
120 int cn = cvTsRandInt(rng) % 3 + 1;
121 CvArrTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
122 depth = depth == 0 ? CV_8U : depth == 1 ? CV_16U : CV_32F;
125 types[INPUT][0] = types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = CV_MAKETYPE(depth, cn);
126 if( test_array[INPUT].size() > 1 )
127 types[INPUT][1] = cvTsRandInt(rng) & 1 ? CV_32FC1 : CV_64FC1;
129 interpolation = cvTsRandInt(rng) % max_interpolation;
133 void CV_ImgWarpBaseTestImpl::fill_array( int test_case_idx, int i, int j, CvMat* arr )
135 if( i != INPUT || j != 0 )
136 CvArrTest::fill_array( test_case_idx, i, j, arr );
139 int CV_ImgWarpBaseTestImpl::prepare_test_case( int test_case_idx )
141 int code = CvArrTest::prepare_test_case( test_case_idx );
142 CvMat* img = &test_mat[INPUT][0];
143 int i, j, cols = img->cols;
144 int type = CV_MAT_TYPE(img->type), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
145 double scale = depth == CV_16U ? 1000. : 255.*0.5;
146 double space_scale = spatial_scale_decimate;
152 if( test_mat[INPUT_OUTPUT][0].cols >= img->cols &&
153 test_mat[INPUT_OUTPUT][0].rows >= img->rows )
154 space_scale = spatial_scale_zoom;
156 buffer = (float*)cvAlloc( img->cols*cn*sizeof(buffer[0]) );
158 for( i = 0; i < img->rows; i++ )
160 uchar* ptr = img->data.ptr + i*img->step;
164 for( j = 0; j < cols; j++ )
165 buffer[j] = (float)((sin((i+1)*space_scale)*sin((j+1)*space_scale)+1.)*scale);
168 for( j = 0; j < cols; j++ )
170 buffer[j*2] = (float)((sin((i+1)*space_scale)+1.)*scale);
171 buffer[j*2+1] = (float)((sin((i+j)*space_scale)+1.)*scale);
175 for( j = 0; j < cols; j++ )
177 buffer[j*3] = (float)((sin((i+1)*space_scale)+1.)*scale);
178 buffer[j*3+1] = (float)((sin(j*space_scale)+1.)*scale);
179 buffer[j*3+2] = (float)((sin((i+j)*space_scale)+1.)*scale);
183 for( j = 0; j < cols; j++ )
185 buffer[j*4] = (float)((sin((i+1)*space_scale)+1.)*scale);
186 buffer[j*4+1] = (float)((sin(j*space_scale)+1.)*scale);
187 buffer[j*4+2] = (float)((sin((i+j)*space_scale)+1.)*scale);
188 buffer[j*4+3] = (float)((sin((i-j)*space_scale)+1.)*scale);
198 for( j = 0; j < cols*cn; j++ )
199 ptr[j] = (uchar)cvRound(buffer[j]);
202 for( j = 0; j < cols*cn; j++ )
203 ((ushort*)ptr)[j] = (ushort)cvRound(buffer[j]);
206 for( j = 0; j < cols*cn; j++ )
207 ((float*)ptr)[j] = (float)buffer[j];
219 CV_ImgWarpBaseTestImpl imgwarp_base( "warp", "", false );
222 class CV_ImgWarpBaseTest : public CV_ImgWarpBaseTestImpl
225 CV_ImgWarpBaseTest( const char* test_name, const char* test_funcs, bool warp_matrix );
229 CV_ImgWarpBaseTest::CV_ImgWarpBaseTest( const char* test_name, const char* test_funcs, bool warp_matrix )
230 : CV_ImgWarpBaseTestImpl( test_name, test_funcs, warp_matrix )
232 size_list = whole_size_list = 0;
238 /////////////////////////
240 class CV_ResizeTest : public CV_ImgWarpBaseTest
246 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
248 void prepare_to_validation( int /*test_case_idx*/ );
249 double get_success_error_level( int test_case_idx, int i, int j );
251 int write_default_params(CvFileStorage* fs);
252 void get_timing_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types,
253 CvSize** whole_sizes, bool *are_images );
254 void print_timing_params( int test_case_idx, char* ptr, int params_left );
258 CV_ResizeTest::CV_ResizeTest()
259 : CV_ImgWarpBaseTest( "warp-resize", "cvResize", false )
261 default_timing_param_names = imgwarp_resize_param_names;
265 int CV_ResizeTest::write_default_params( CvFileStorage* fs )
267 int code = CV_ImgWarpBaseTest::write_default_params( fs );
271 if( ts->get_testing_mode() == CvTS::TIMING_MODE )
273 start_write_param( fs );
274 write_real_list( fs, "coeff", imgwarp_resize_coeffs, CV_DIM(imgwarp_resize_coeffs) );
275 write_string_list( fs, "method", imgwarp_resize_methods );
282 void CV_ResizeTest::get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types )
284 CvRNG* rng = ts->get_rng();
285 CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
288 sz.width = (cvTsRandInt(rng) % sizes[INPUT][0].width) + 1;
289 sz.height = (cvTsRandInt(rng) % sizes[INPUT][0].height) + 1;
291 if( cvTsRandInt(rng) & 1 )
293 int xfactor = cvTsRandInt(rng) % 10 + 1;
294 int yfactor = cvTsRandInt(rng) % 10 + 1;
296 if( cvTsRandInt(rng) & 1 )
299 sz.width = sizes[INPUT][0].width / xfactor;
300 sz.width = MAX(sz.width,1);
301 sz.height = sizes[INPUT][0].height / yfactor;
302 sz.height = MAX(sz.height,1);
303 sizes[INPUT][0].width = sz.width * xfactor;
304 sizes[INPUT][0].height = sz.height * yfactor;
307 if( cvTsRandInt(rng) & 1 )
308 sizes[INPUT_OUTPUT][0] = sizes[REF_INPUT_OUTPUT][0] = sz;
311 sizes[INPUT_OUTPUT][0] = sizes[REF_INPUT_OUTPUT][0] = sizes[INPUT][0];
312 sizes[INPUT][0] = sz;
317 void CV_ResizeTest::get_timing_test_array_types_and_sizes( int test_case_idx,
318 CvSize** sizes, int** types, CvSize** whole_sizes, bool *are_images )
320 CV_ImgWarpBaseTest::get_timing_test_array_types_and_sizes( test_case_idx, sizes, types,
321 whole_sizes, are_images );
322 const char* method_str = cvReadString( find_timing_param( "method" ), "linear" );
323 double coeff = cvReadReal( find_timing_param( "coeff" ), 1. );
324 CvSize size = sizes[INPUT][0];
326 size.width = cvRound(size.width*coeff);
327 size.height = cvRound(size.height*coeff);
328 sizes[INPUT_OUTPUT][0] = whole_sizes[INPUT_OUTPUT][0] = size;
330 interpolation = strcmp( method_str, "nearest" ) == 0 ? CV_INTER_NN :
331 strcmp( method_str, "linear" ) == 0 ? CV_INTER_LINEAR :
332 strcmp( method_str, "cubic" ) == 0 ? CV_INTER_CUBIC : CV_INTER_AREA;
336 void CV_ResizeTest::print_timing_params( int test_case_idx, char* ptr, int params_left )
338 sprintf( ptr, "coeff=%.3f,", cvReadReal( find_timing_param( "coeff" ), 1. ) );
340 sprintf( ptr, "method=%s,", cvReadString( find_timing_param( "method" ), "linear" ) );
344 CV_ImgWarpBaseTest::print_timing_params( test_case_idx, ptr, params_left );
348 void CV_ResizeTest::run_func()
350 cvResize( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], interpolation );
354 double CV_ResizeTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
356 int depth = CV_MAT_DEPTH(test_mat[INPUT][0].type);
357 return depth == CV_8U ? 16 : depth == CV_16U ? 1024 : 1e-1;
361 void CV_ResizeTest::prepare_to_validation( int /*test_case_idx*/ )
363 CvMat* src = &test_mat[INPUT][0];
364 CvMat* dst = &test_mat[REF_INPUT_OUTPUT][0];
366 CvMat* x_idx = cvCreateMat( 1, dst->cols, CV_32SC1 );
367 CvMat* y_idx = cvCreateMat( 1, dst->rows, CV_32SC1 );
368 int* x_tab = x_idx->data.i;
369 int elem_size = CV_ELEM_SIZE(src->type);
370 int drows = dst->rows, dcols = dst->cols;
372 if( interpolation == CV_INTER_NN )
374 for( j = 0; j < dcols; j++ )
376 int t = (j*src->cols*2 + MIN(src->cols,dcols) - 1)/(dcols*2);
378 x_idx->data.i[j] = t*elem_size;
381 for( j = 0; j < drows; j++ )
383 int t = (j*src->rows*2 + MIN(src->rows,drows) - 1)/(drows*2);
385 y_idx->data.i[j] = t;
390 double scale_x = (double)src->cols/dcols;
391 double scale_y = (double)src->rows/drows;
393 for( j = 0; j < dcols; j++ )
395 double f = ((j+0.5)*scale_x - 0.5);
397 x_idx->data.i[j] = (i < 0 ? 0 : i >= src->cols ? src->cols - 1 : i)*elem_size;
400 for( j = 0; j < drows; j++ )
402 double f = ((j+0.5)*scale_y - 0.5);
404 y_idx->data.i[j] = i < 0 ? 0 : i >= src->rows ? src->rows - 1 : i;
408 for( i = 0; i < drows; i++ )
410 uchar* dptr = dst->data.ptr + dst->step*i;
411 const uchar* sptr0 = src->data.ptr + src->step*y_idx->data.i[i];
413 for( j = 0; j < dcols; j++, dptr += elem_size )
415 const uchar* sptr = sptr0 + x_tab[j];
416 for( k = 0; k < elem_size; k++ )
421 cvReleaseMat( &x_idx );
422 cvReleaseMat( &y_idx );
425 CV_ResizeTest warp_resize_test;
428 /////////////////////////
430 void cvTsRemap( const CvMat* src, CvMat* dst,
431 const CvMat* mapx, const CvMat* mapy,
432 CvMat* mask, int interpolation=CV_INTER_LINEAR )
435 int drows = dst->rows, dcols = dst->cols;
436 int srows = src->rows, scols = src->cols;
437 uchar* sptr0 = src->data.ptr;
438 int depth = CV_MAT_DEPTH(src->type), cn = CV_MAT_CN(src->type);
439 int elem_size = CV_ELEM_SIZE(src->type);
440 int step = src->step / CV_ELEM_SIZE(depth);
443 if( interpolation != CV_INTER_CUBIC )
446 scols -= 1; srows -= 1;
451 scols = MAX(scols - 3, 0);
452 srows = MAX(srows - 3, 0);
455 int scols1 = MAX(scols - 2, 0);
456 int srows1 = MAX(srows - 2, 0);
461 for( y = 0; y < drows; y++ )
463 uchar* dptr = dst->data.ptr + dst->step*y;
464 const float* mx = (const float*)(mapx->data.ptr + mapx->step*y);
465 const float* my = (const float*)(mapy->data.ptr + mapy->step*y);
466 uchar* m = mask ? mask->data.ptr + mask->step*y : 0;
468 for( x = 0; x < dcols; x++, dptr += elem_size )
472 int ixs = cvFloor(xs);
473 int iys = cvFloor(ys);
475 if( (unsigned)(ixs - delta - 1) >= (unsigned)scols1 ||
476 (unsigned)(iys - delta - 1) >= (unsigned)srows1 )
480 if( (unsigned)(ixs - delta) >= (unsigned)scols ||
481 (unsigned)(iys - delta) >= (unsigned)srows )
492 const uchar* sptr = sptr0 + iys*step + ixs*cn;
493 for( k = 0; k < cn; k++ )
496 float v01 = sptr[cn + k];
497 float v10 = sptr[step + k];
498 float v11 = sptr[step + cn + k];
500 v00 = v00 + xs*(v01 - v00);
501 v10 = v10 + xs*(v11 - v10);
502 v00 = v00 + ys*(v10 - v00);
503 dptr[k] = (uchar)cvRound(v00);
509 const ushort* sptr = (const ushort*)sptr0 + iys*step + ixs*cn;
510 for( k = 0; k < cn; k++ )
513 float v01 = sptr[cn + k];
514 float v10 = sptr[step + k];
515 float v11 = sptr[step + cn + k];
517 v00 = v00 + xs*(v01 - v00);
518 v10 = v10 + xs*(v11 - v10);
519 v00 = v00 + ys*(v10 - v00);
520 ((ushort*)dptr)[k] = (ushort)cvRound(v00);
526 const float* sptr = (const float*)sptr0 + iys*step + ixs*cn;
527 for( k = 0; k < cn; k++ )
530 float v01 = sptr[cn + k];
531 float v10 = sptr[step + k];
532 float v11 = sptr[step + cn + k];
534 v00 = v00 + xs*(v01 - v00);
535 v10 = v10 + xs*(v11 - v10);
536 v00 = v00 + ys*(v10 - v00);
537 ((float*)dptr)[k] = (float)v00;
548 /////////////////////////
550 class CV_WarpAffineTest : public CV_ImgWarpBaseTest
556 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
558 int prepare_test_case( int test_case_idx );
559 void prepare_to_validation( int /*test_case_idx*/ );
560 double get_success_error_level( int test_case_idx, int i, int j );
562 int write_default_params(CvFileStorage* fs);
563 void get_timing_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types,
564 CvSize** whole_sizes, bool *are_images );
565 void print_timing_params( int test_case_idx, char* ptr, int params_left );
569 CV_WarpAffineTest::CV_WarpAffineTest()
570 : CV_ImgWarpBaseTest( "warp-affine", "cvWarpAffine", true )
572 //spatial_scale_zoom = spatial_scale_decimate;
573 test_array[TEMP].push(NULL);
574 test_array[TEMP].push(NULL);
576 spatial_scale_decimate = spatial_scale_zoom;
578 default_timing_param_names = imgwarp_affine_param_names;
582 int CV_WarpAffineTest::write_default_params( CvFileStorage* fs )
584 int code = CV_ImgWarpBaseTest::write_default_params( fs );
588 if( ts->get_testing_mode() == CvTS::TIMING_MODE )
591 start_write_param( fs );
593 cvStartWriteStruct( fs, "rotate_scale", CV_NODE_SEQ+CV_NODE_FLOW );
594 for( i = 0; imgwarp_affine_rotate_scale[i][0] >= 0; i++ )
596 cvStartWriteStruct( fs, 0, CV_NODE_SEQ+CV_NODE_FLOW );
597 cvWriteRawData( fs, imgwarp_affine_rotate_scale[i], 4, "d" );
598 cvEndWriteStruct(fs);
600 cvEndWriteStruct(fs);
607 void CV_WarpAffineTest::get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types )
609 CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
610 CvSize sz = sizes[INPUT][0];
611 // run for the second time to get output of a different size
612 CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
613 sizes[INPUT][0] = sz;
614 sizes[INPUT][1] = cvSize( 3, 2 );
615 sizes[TEMP][0] = sizes[TEMP][1] = sizes[INPUT_OUTPUT][0];
616 types[TEMP][0] = types[TEMP][1] = CV_32FC1;
620 void CV_WarpAffineTest::get_timing_test_array_types_and_sizes( int test_case_idx,
621 CvSize** sizes, int** types, CvSize** whole_sizes, bool *are_images )
623 CV_ImgWarpBaseTest::get_timing_test_array_types_and_sizes( test_case_idx, sizes, types,
624 whole_sizes, are_images );
626 sizes[INPUT][1] = whole_sizes[INPUT][1] = cvSize(3,2);
627 sizes[TEMP][0] = whole_sizes[TEMP][0] =
628 sizes[TEMP][1] = whole_sizes[TEMP][1] = cvSize(0,0);
629 types[INPUT][1] = CV_64FC1;
631 interpolation = CV_INTER_LINEAR;
635 void CV_WarpAffineTest::print_timing_params( int test_case_idx, char* ptr, int params_left )
638 const CvFileNode* node = find_timing_param( "rotate_scale" );
639 assert( node && CV_NODE_IS_SEQ(node->tag) );
640 cvReadRawData( ts->get_file_storage(), node, coeffs, "4d" );
642 sprintf( ptr, "fx=%.2f,fy=%.2f,angle=%.1fdeg,scale=%.1f,", coeffs[0], coeffs[1], coeffs[2], coeffs[3] );
646 CV_ImgWarpBaseTest::print_timing_params( test_case_idx, ptr, params_left );
650 void CV_WarpAffineTest::run_func()
652 cvWarpAffine( test_array[INPUT][0], test_array[INPUT_OUTPUT][0],
653 &test_mat[INPUT][1], interpolation );
657 double CV_WarpAffineTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
659 int depth = CV_MAT_DEPTH(test_mat[INPUT][0].type);
660 return depth == CV_8U ? 16 : depth == CV_16U ? 1024 : 5e-2;
664 int CV_WarpAffineTest::prepare_test_case( int test_case_idx )
666 CvRNG* rng = ts->get_rng();
667 int code = CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
668 const CvMat* src = &test_mat[INPUT][0];
669 const CvMat* dst = &test_mat[INPUT_OUTPUT][0];
670 CvMat* mat = &test_mat[INPUT][1];
677 if( ts->get_testing_mode() == CvTS::CORRECTNESS_CHECK_MODE )
680 CvMat tmp = cvMat( 2, 3, mat->type, buf );
682 center.x = (float)((cvTsRandReal(rng)*1.2 - 0.1)*src->cols);
683 center.y = (float)((cvTsRandReal(rng)*1.2 - 0.1)*src->rows);
684 angle = cvTsRandReal(rng)*360;
685 scale = ((double)dst->rows/src->rows + (double)dst->cols/src->cols)*0.5;
686 cv2DRotationMatrix( center, angle, scale, mat );
687 cvRandArr( rng, &tmp, CV_RAND_NORMAL, cvScalarAll(1.), cvScalarAll(0.01) );
688 cvMaxS( &tmp, 0.9, &tmp );
689 cvMinS( &tmp, 1.1, &tmp );
690 cvMul( &tmp, mat, mat, 1. );
695 const CvFileNode* node = find_timing_param( "rotate_scale" );
697 assert( node && CV_NODE_IS_SEQ(node->tag) );
698 cvReadRawData( ts->get_file_storage(), node, coeffs, "4d" );
700 center.x = (float)(coeffs[0]*src->cols);
701 center.y = (float)(coeffs[1]*src->rows);
704 cv2DRotationMatrix( center, angle, scale, mat );
711 void CV_WarpAffineTest::prepare_to_validation( int /*test_case_idx*/ )
713 CvMat* src = &test_mat[INPUT][0];
714 CvMat* dst = &test_mat[REF_INPUT_OUTPUT][0];
715 CvMat* dst0 = &test_mat[INPUT_OUTPUT][0];
716 CvMat* mapx = &test_mat[TEMP][0];
717 CvMat* mapy = &test_mat[TEMP][1];
720 CvMat srcAb = cvMat(2, 3, CV_64FC1, tm ), A, b, invA, invAb, dstAb = cvMat( 2, 3, CV_64FC1, m );
722 //cvInvert( &tM, &M, CV_LU );
723 // [R|t] -> [R^-1 | -(R^-1)*t]
724 cvTsConvert( &test_mat[INPUT][1], &srcAb );
725 cvGetCols( &srcAb, &A, 0, 2 );
726 cvGetCol( &srcAb, &b, 2 );
727 cvGetCols( &dstAb, &invA, 0, 2 );
728 cvGetCol( &dstAb, &invAb, 2 );
729 cvInvert( &A, &invA, CV_SVD );
730 cvGEMM( &invA, &b, -1, 0, 0, &invAb );
732 for( y = 0; y < dst->rows; y++ )
734 float* mx = (float*)(mapx->data.ptr + y*mapx->step);
735 float* my = (float*)(mapy->data.ptr + y*mapy->step);
737 for( x = 0; x < dst->cols; x++ )
739 mx[x] = (float)(x*m[0] + y*m[1] + m[2]);
740 my[x] = (float)(x*m[3] + y*m[4] + m[5]);
744 CvMat* mask = cvCreateMat( dst->rows, dst->cols, CV_8U );
745 cvTsRemap( src, dst, mapx, mapy, mask );
746 cvTsZero( dst, mask );
747 cvTsZero( dst0, mask );
748 cvReleaseMat( &mask );
752 CV_WarpAffineTest warp_affine_test;
756 /////////////////////////
758 class CV_WarpPerspectiveTest : public CV_ImgWarpBaseTest
761 CV_WarpPerspectiveTest();
764 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
766 int prepare_test_case( int test_case_idx );
767 void prepare_to_validation( int /*test_case_idx*/ );
768 double get_success_error_level( int test_case_idx, int i, int j );
770 int write_default_params(CvFileStorage* fs);
771 void get_timing_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types,
772 CvSize** whole_sizes, bool *are_images );
773 void print_timing_params( int test_case_idx, char* ptr, int params_left );
777 CV_WarpPerspectiveTest::CV_WarpPerspectiveTest()
778 : CV_ImgWarpBaseTest( "warp-perspective", "cvWarpPerspective", true )
780 //spatial_scale_zoom = spatial_scale_decimate;
781 test_array[TEMP].push(NULL);
782 test_array[TEMP].push(NULL);
784 spatial_scale_decimate = spatial_scale_zoom;
785 default_timing_param_names = imgwarp_perspective_param_names;
789 int CV_WarpPerspectiveTest::write_default_params( CvFileStorage* fs )
791 int code = CV_ImgWarpBaseTest::write_default_params( fs );
795 if( ts->get_testing_mode() == CvTS::TIMING_MODE )
798 start_write_param( fs );
800 cvStartWriteStruct( fs, "shift_vtx", CV_NODE_SEQ+CV_NODE_FLOW );
801 for( i = 0; imgwarp_perspective_shift_vtx[i][0] >= 0; i++ )
803 cvStartWriteStruct( fs, 0, CV_NODE_SEQ+CV_NODE_FLOW );
804 cvWriteRawData( fs, imgwarp_perspective_shift_vtx[i], 8, "d" );
805 cvEndWriteStruct(fs);
807 cvEndWriteStruct(fs);
814 void CV_WarpPerspectiveTest::get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types )
816 CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
817 CvSize sz = sizes[INPUT][0];
818 // run for the second time to get output of a different size
819 CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
820 sizes[INPUT][0] = sz;
821 sizes[INPUT][1] = cvSize( 3, 3 );
823 sizes[TEMP][0] = sizes[TEMP][1] = sizes[INPUT_OUTPUT][0];
824 types[TEMP][0] = types[TEMP][1] = CV_32FC1;
828 void CV_WarpPerspectiveTest::get_timing_test_array_types_and_sizes( int test_case_idx,
829 CvSize** sizes, int** types, CvSize** whole_sizes, bool *are_images )
831 CV_ImgWarpBaseTest::get_timing_test_array_types_and_sizes( test_case_idx, sizes, types,
832 whole_sizes, are_images );
834 sizes[INPUT][1] = whole_sizes[INPUT][1] = cvSize(3,3);
835 sizes[TEMP][0] = whole_sizes[TEMP][0] =
836 sizes[TEMP][1] = whole_sizes[TEMP][1] = cvSize(0,0);
837 types[INPUT][1] = CV_64FC1;
839 interpolation = CV_INTER_LINEAR;
843 void CV_WarpPerspectiveTest::print_timing_params( int test_case_idx, char* ptr, int params_left )
845 CV_ImgWarpBaseTest::print_timing_params( test_case_idx, ptr, params_left );
849 void CV_WarpPerspectiveTest::run_func()
851 cvWarpPerspective( test_array[INPUT][0], test_array[INPUT_OUTPUT][0],
852 &test_mat[INPUT][1], interpolation );
856 double CV_WarpPerspectiveTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
858 int depth = CV_MAT_DEPTH(test_mat[INPUT][0].type);
859 return depth == CV_8U ? 16 : depth == CV_16U ? 1024 : 5e-2;
863 int CV_WarpPerspectiveTest::prepare_test_case( int test_case_idx )
865 CvRNG* rng = ts->get_rng();
866 int code = CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
867 const CvMat* src = &test_mat[INPUT][0];
868 const CvMat* dst = &test_mat[INPUT_OUTPUT][0];
869 CvMat* mat = &test_mat[INPUT][1];
870 CvPoint2D32f s[4], d[4];
876 s[0] = cvPoint2D32f(0,0);
877 d[0] = cvPoint2D32f(0,0);
878 s[1] = cvPoint2D32f(src->cols-1,0);
879 d[1] = cvPoint2D32f(dst->cols-1,0);
880 s[2] = cvPoint2D32f(src->cols-1,src->rows-1);
881 d[2] = cvPoint2D32f(dst->cols-1,dst->rows-1);
882 s[3] = cvPoint2D32f(0,src->rows-1);
883 d[3] = cvPoint2D32f(0,dst->rows-1);
885 if( ts->get_testing_mode() == CvTS::CORRECTNESS_CHECK_MODE )
888 CvMat tmp = cvMat( 1, 16, CV_32FC1, buf );
890 cvRandArr( rng, &tmp, CV_RAND_NORMAL, cvScalarAll(0.), cvScalarAll(0.1) );
892 for( i = 0; i < 4; i++ )
894 s[i].x += buf[i*4]*src->cols/2;
895 s[i].y += buf[i*4+1]*src->rows/2;
896 d[i].x += buf[i*4+2]*dst->cols/2;
897 d[i].y += buf[i*4+3]*dst->rows/2;
903 const CvFileNode* node = find_timing_param( "shift_vtx" );
905 assert( node && CV_NODE_IS_SEQ(node->tag) );
906 cvReadRawData( ts->get_file_storage(), node, coeffs, "8d" );
908 for( i = 0; i < 4; i++ )
910 d[i].x += (float)(coeffs[i*2]*src->cols*(i == 0 || i == 3 ? 1 : -1));
911 d[i].y += (float)(coeffs[i*2+1]*src->rows*(i == 0 || i == 1 ? 1 : -1));
915 cvWarpPerspectiveQMatrix( s, d, mat );
920 void CV_WarpPerspectiveTest::prepare_to_validation( int /*test_case_idx*/ )
922 CvMat* src = &test_mat[INPUT][0];
923 CvMat* dst = &test_mat[REF_INPUT_OUTPUT][0];
924 CvMat* dst0 = &test_mat[INPUT_OUTPUT][0];
925 CvMat* mapx = &test_mat[TEMP][0];
926 CvMat* mapy = &test_mat[TEMP][1];
929 CvMat srcM = cvMat(3, 3, CV_64FC1, tm ), dstM = cvMat( 3, 3, CV_64FC1, m );
931 //cvInvert( &tM, &M, CV_LU );
932 // [R|t] -> [R^-1 | -(R^-1)*t]
933 cvTsConvert( &test_mat[INPUT][1], &srcM );
934 cvInvert( &srcM, &dstM, CV_SVD );
936 for( y = 0; y < dst->rows; y++ )
938 float* mx = (float*)(mapx->data.ptr + y*mapx->step);
939 float* my = (float*)(mapy->data.ptr + y*mapy->step);
941 for( x = 0; x < dst->cols; x++ )
943 double xs = x*m[0] + y*m[1] + m[2];
944 double ys = x*m[3] + y*m[4] + m[5];
945 double ds = x*m[6] + y*m[7] + m[8];
956 CvMat* mask = cvCreateMat( dst->rows, dst->cols, CV_8U );
957 cvTsRemap( src, dst, mapx, mapy, mask );
958 cvTsZero( dst, mask );
959 cvTsZero( dst0, mask );
960 cvReleaseMat( &mask );
964 CV_WarpPerspectiveTest warp_perspective_test;
968 /////////////////////////
970 void cvTsInitUndistortMap( const CvMat* _a0, const CvMat* _k0, CvMat* mapx, CvMat* mapy )
973 double a[9], k[5]={0,0,0,0,0};
974 CvMat _a = cvMat(3, 3, CV_64F, a);
975 CvMat _k = cvMat(_k0->rows,_k0->cols,
976 CV_MAKETYPE(CV_64F,CV_MAT_CN(_k0->type)),k);
977 double fx, fy, cx, cy, ifx, ify, cxn, cyn;
979 cvTsConvert( _a0, &_a );
980 cvTsConvert( _k0, &_k );
981 fx = a[0]; fy = a[4]; cx = a[2]; cy = a[5];
982 ifx = 1./fx; ify = 1./fy;
983 cxn = cx;//(mapy->cols - 1)*0.5;
984 cyn = cy;//(mapy->rows - 1)*0.5;
986 for( v = 0; v < mapy->rows; v++ )
988 float* mx = (float*)(mapx->data.ptr + v*mapx->step);
989 float* my = (float*)(mapy->data.ptr + v*mapy->step);
991 for( u = 0; u < mapy->cols; u++ )
993 double x = (u - cxn)*ifx;
994 double y = (v - cyn)*ify;
995 double x2 = x*x, y2 = y*y;
997 double cdist = 1 + (k[0] + (k[1] + k[4]*r2)*r2)*r2;
998 double x1 = x*cdist + k[2]*2*x*y + k[3]*(r2 + 2*x2);
999 double y1 = y*cdist + k[3]*2*x*y + k[2]*(r2 + 2*y2);
1000 mx[u] = (float)(x1*fx + cx);
1001 my[u] = (float)(y1*fy + cy);
1007 static double remap_undistort_params[] = { 0.5, 0.5, 0.5, 0.5, 0.01, -0.01, 0.001, -0.001 };
1009 class CV_RemapTest : public CV_ImgWarpBaseTest
1015 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
1017 int prepare_test_case( int test_case_idx );
1018 void prepare_to_validation( int /*test_case_idx*/ );
1019 double get_success_error_level( int test_case_idx, int i, int j );
1020 void fill_array( int test_case_idx, int i, int j, CvMat* arr );
1022 int write_default_params(CvFileStorage* fs);
1023 void get_timing_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types,
1024 CvSize** whole_sizes, bool *are_images );
1025 void print_timing_params( int test_case_idx, char* ptr, int params_left );
1029 CV_RemapTest::CV_RemapTest()
1030 : CV_ImgWarpBaseTest( "warp-remap", "cvRemap", false )
1032 //spatial_scale_zoom = spatial_scale_decimate;
1033 test_array[INPUT].push(NULL);
1034 test_array[INPUT].push(NULL);
1036 spatial_scale_decimate = spatial_scale_zoom;
1037 //default_timing_param_names = imgwarp_perspective_param_names;
1038 support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
1039 default_timing_param_names = 0;
1043 int CV_RemapTest::write_default_params( CvFileStorage* fs )
1045 int code = CV_ImgWarpBaseTest::write_default_params( fs );
1049 if( ts->get_testing_mode() == CvTS::TIMING_MODE )
1052 start_write_param( fs );
1054 cvStartWriteStruct( fs, "params", CV_NODE_SEQ+CV_NODE_FLOW );
1055 for( i = 0; i < 8; i++ )
1056 cvWriteReal( fs, 0, remap_undistort_params[i] );
1057 cvEndWriteStruct(fs);
1064 void CV_RemapTest::get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types )
1066 CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
1067 types[INPUT][1] = types[INPUT][2] = CV_32FC1;
1068 interpolation = CV_INTER_LINEAR;
1072 void CV_RemapTest::fill_array( int test_case_idx, int i, int j, CvMat* arr )
1075 CV_ImgWarpBaseTestImpl::fill_array( test_case_idx, i, j, arr );
1078 void CV_RemapTest::get_timing_test_array_types_and_sizes( int test_case_idx,
1079 CvSize** sizes, int** types, CvSize** whole_sizes, bool *are_images )
1081 CV_ImgWarpBaseTest::get_timing_test_array_types_and_sizes( test_case_idx, sizes, types,
1082 whole_sizes, are_images );
1083 types[INPUT][1] = types[INPUT][2] = CV_32FC1;
1084 interpolation = CV_INTER_LINEAR;
1088 void CV_RemapTest::print_timing_params( int test_case_idx, char* ptr, int params_left )
1090 CV_ImgWarpBaseTest::print_timing_params( test_case_idx, ptr, params_left );
1094 void CV_RemapTest::run_func()
1096 cvRemap( test_array[INPUT][0], test_array[INPUT_OUTPUT][0],
1097 test_array[INPUT][1], test_array[INPUT][2], interpolation );
1101 double CV_RemapTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
1103 int depth = CV_MAT_DEPTH(test_mat[INPUT][0].type);
1104 return depth == CV_8U ? 16 : depth == CV_16U ? 1024 : 5e-2;
1108 int CV_RemapTest::prepare_test_case( int test_case_idx )
1110 CvRNG* rng = ts->get_rng();
1111 int code = CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
1112 const CvMat* src = &test_mat[INPUT][0];
1113 double a[9] = {0,0,0,0,0,0,0,0,1}, k[4];
1114 CvMat _a = cvMat( 3, 3, CV_64F, a );
1115 CvMat _k = cvMat( 4, 1, CV_64F, k );
1116 double sz = MAX(src->rows, src->cols);
1121 if( ts->get_testing_mode() == CvTS::CORRECTNESS_CHECK_MODE )
1123 double aspect_ratio = cvTsRandReal(rng)*0.6 + 0.7;
1124 a[2] = (src->cols - 1)*0.5 + cvTsRandReal(rng)*10 - 5;
1125 a[5] = (src->rows - 1)*0.5 + cvTsRandReal(rng)*10 - 5;
1126 a[0] = sz/(0.9 - cvTsRandReal(rng)*0.6);
1127 a[4] = aspect_ratio*a[0];
1128 k[0] = cvTsRandReal(rng)*0.06 - 0.03;
1129 k[1] = cvTsRandReal(rng)*0.06 - 0.03;
1132 k[2] = cvTsRandReal(rng)*0.004 - 0.002;
1133 k[3] = cvTsRandReal(rng)*0.004 - 0.002;
1138 a[2] = (src->cols - 1)*remap_undistort_params[0];
1139 a[5] = (src->rows - 1)*remap_undistort_params[1];
1140 a[0] = sz/remap_undistort_params[2];
1141 a[4] = sz/remap_undistort_params[3];
1142 for( i = 0; i < 4; i++ )
1143 k[i] = remap_undistort_params[i+4];
1146 cvTsInitUndistortMap( &_a, &_k, &test_mat[INPUT][1], &test_mat[INPUT][2] );
1151 void CV_RemapTest::prepare_to_validation( int /*test_case_idx*/ )
1153 CvMat* dst = &test_mat[REF_INPUT_OUTPUT][0];
1154 CvMat* dst0 = &test_mat[INPUT_OUTPUT][0];
1155 CvMat* mask = cvCreateMat( dst->rows, dst->cols, CV_8U );
1156 cvTsRemap( &test_mat[INPUT][0], dst,
1157 &test_mat[INPUT][1], &test_mat[INPUT][2],
1158 mask, interpolation );
1159 cvTsZero( dst, mask );
1160 cvTsZero( dst0, mask );
1161 cvReleaseMat( &mask );
1165 CV_RemapTest remap_test;
1168 ////////////////////////////// undistort /////////////////////////////////
1170 class CV_UndistortTest : public CV_ImgWarpBaseTest
1176 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
1178 int prepare_test_case( int test_case_idx );
1179 void prepare_to_validation( int /*test_case_idx*/ );
1180 double get_success_error_level( int test_case_idx, int i, int j );
1181 void fill_array( int test_case_idx, int i, int j, CvMat* arr );
1183 int write_default_params(CvFileStorage* fs);
1184 void get_timing_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types,
1185 CvSize** whole_sizes, bool *are_images );
1186 void print_timing_params( int test_case_idx, char* ptr, int params_left );
1190 CV_UndistortTest::CV_UndistortTest()
1191 : CV_ImgWarpBaseTest( "warp-undistort", "cvUndistort2", false )
1193 //spatial_scale_zoom = spatial_scale_decimate;
1194 test_array[INPUT].push(NULL);
1195 test_array[INPUT].push(NULL);
1197 spatial_scale_decimate = spatial_scale_zoom;
1198 //default_timing_param_names = imgwarp_perspective_param_names;
1199 support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
1200 default_timing_param_names = 0;
1204 int CV_UndistortTest::write_default_params( CvFileStorage* fs )
1206 int code = CV_ImgWarpBaseTest::write_default_params( fs );
1210 if( ts->get_testing_mode() == CvTS::TIMING_MODE )
1213 start_write_param( fs );
1215 cvStartWriteStruct( fs, "params", CV_NODE_SEQ+CV_NODE_FLOW );
1216 for( i = 0; i < 8; i++ )
1217 cvWriteReal( fs, 0, remap_undistort_params[i] );
1218 cvEndWriteStruct(fs);
1225 void CV_UndistortTest::get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types )
1227 CvRNG* rng = ts->get_rng();
1228 CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
1229 int type = types[INPUT][0];
1230 type = CV_MAKETYPE( CV_8U, CV_MAT_CN(type) );
1231 types[INPUT][0] = types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = type;
1232 types[INPUT][1] = cvTsRandInt(rng)%2 ? CV_64F : CV_32F;
1233 types[INPUT][2] = cvTsRandInt(rng)%2 ? CV_64F : CV_32F;
1234 sizes[INPUT][1] = cvSize(3,3);
1235 sizes[INPUT][2] = cvTsRandInt(rng)%2 ? cvSize(4,1) : cvSize(1,4);
1236 interpolation = CV_INTER_LINEAR;
1240 void CV_UndistortTest::fill_array( int test_case_idx, int i, int j, CvMat* arr )
1243 CV_ImgWarpBaseTestImpl::fill_array( test_case_idx, i, j, arr );
1246 void CV_UndistortTest::get_timing_test_array_types_and_sizes( int test_case_idx,
1247 CvSize** sizes, int** types, CvSize** whole_sizes, bool *are_images )
1249 CV_ImgWarpBaseTest::get_timing_test_array_types_and_sizes( test_case_idx, sizes, types,
1250 whole_sizes, are_images );
1251 types[INPUT][1] = types[INPUT][2] = CV_32FC1;
1252 interpolation = CV_INTER_LINEAR;
1256 void CV_UndistortTest::print_timing_params( int test_case_idx, char* ptr, int params_left )
1258 CV_ImgWarpBaseTest::print_timing_params( test_case_idx, ptr, params_left );
1262 void CV_UndistortTest::run_func()
1264 cvUndistort2( test_array[INPUT][0], test_array[INPUT_OUTPUT][0],
1265 &test_mat[INPUT][1], &test_mat[INPUT][2] );
1269 double CV_UndistortTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
1271 int depth = CV_MAT_DEPTH(test_mat[INPUT][0].type);
1272 return depth == CV_8U ? 16 : depth == CV_16U ? 1024 : 5e-2;
1276 int CV_UndistortTest::prepare_test_case( int test_case_idx )
1278 CvRNG* rng = ts->get_rng();
1279 int code = CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
1280 const CvMat* src = &test_mat[INPUT][0];
1281 double k[4], a[9] = {0,0,0,0,0,0,0,0,1};
1282 double sz = MAX(src->rows, src->cols);
1283 CvMat* _a0 = &test_mat[INPUT][1], *_k0 = &test_mat[INPUT][2];
1284 CvMat _a = cvMat(3,3,CV_64F,a);
1285 CvMat _k = cvMat(_k0->rows,_k0->cols, CV_MAKETYPE(CV_64F,CV_MAT_CN(_k0->type)),k);
1290 if( ts->get_testing_mode() == CvTS::CORRECTNESS_CHECK_MODE )
1292 double aspect_ratio = cvTsRandReal(rng)*0.6 + 0.7;
1293 a[2] = (src->cols - 1)*0.5 + cvTsRandReal(rng)*10 - 5;
1294 a[5] = (src->rows - 1)*0.5 + cvTsRandReal(rng)*10 - 5;
1295 a[0] = sz/(0.9 - cvTsRandReal(rng)*0.6);
1296 a[4] = aspect_ratio*a[0];
1297 k[0] = cvTsRandReal(rng)*0.06 - 0.03;
1298 k[1] = cvTsRandReal(rng)*0.06 - 0.03;
1301 if( cvTsRandInt(rng)%4 != 0 )
1303 k[2] = cvTsRandReal(rng)*0.004 - 0.002;
1304 k[3] = cvTsRandReal(rng)*0.004 - 0.002;
1312 a[2] = (src->cols - 1)*remap_undistort_params[0];
1313 a[5] = (src->rows - 1)*remap_undistort_params[1];
1314 a[0] = sz/remap_undistort_params[2];
1315 a[4] = sz/remap_undistort_params[3];
1316 for( i = 0; i < 4; i++ )
1317 k[i] = remap_undistort_params[i+4];
1320 cvTsConvert( &_a, _a0 );
1321 cvTsConvert( &_k, _k0 );
1327 void CV_UndistortTest::prepare_to_validation( int /*test_case_idx*/ )
1329 CvMat* src = &test_mat[INPUT][0];
1330 CvMat* dst = &test_mat[REF_INPUT_OUTPUT][0];
1331 CvMat* dst0 = &test_mat[INPUT_OUTPUT][0];
1332 CvMat* mapx = cvCreateMat( dst->rows, dst->cols, CV_32FC1 );
1333 CvMat* mapy = cvCreateMat( dst->rows, dst->cols, CV_32FC1 );
1334 cvTsInitUndistortMap( &test_mat[INPUT][1], &test_mat[INPUT][2],
1336 CvMat* mask = cvCreateMat( dst->rows, dst->cols, CV_8U );
1337 cvTsRemap( src, dst, mapx, mapy, mask, interpolation );
1338 cvTsZero( dst, mask );
1339 cvTsZero( dst0, mask );
1341 cvReleaseMat( &mapx );
1342 cvReleaseMat( &mapy );
1343 cvReleaseMat( &mask );
1347 CV_UndistortTest undistort_test;
1351 class CV_UndistortMapTest : public CvArrTest
1354 CV_UndistortMapTest();
1357 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
1359 int prepare_test_case( int test_case_idx );
1360 void prepare_to_validation( int /*test_case_idx*/ );
1361 double get_success_error_level( int test_case_idx, int i, int j );
1362 void fill_array( int test_case_idx, int i, int j, CvMat* arr );
1364 int write_default_params(CvFileStorage* fs);
1365 void get_timing_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types,
1366 CvSize** whole_sizes, bool *are_images );
1367 void print_timing_params( int test_case_idx, char* ptr, int params_left );
1371 CV_UndistortMapTest::CV_UndistortMapTest()
1372 : CvArrTest( "warp-undistort-map", "cvInitUndistortMap", "" )
1374 test_array[INPUT].push(NULL);
1375 test_array[INPUT].push(NULL);
1376 test_array[OUTPUT].push(NULL);
1377 test_array[OUTPUT].push(NULL);
1378 test_array[REF_OUTPUT].push(NULL);
1379 test_array[REF_OUTPUT].push(NULL);
1381 element_wise_relative_error = false;
1383 support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
1384 default_timing_param_names = 0;
1388 int CV_UndistortMapTest::write_default_params( CvFileStorage* fs )
1390 int code = CvArrTest::write_default_params( fs );
1394 if( ts->get_testing_mode() == CvTS::TIMING_MODE )
1397 start_write_param( fs );
1399 cvStartWriteStruct( fs, "params", CV_NODE_SEQ+CV_NODE_FLOW );
1400 for( i = 0; i < 8; i++ )
1401 cvWriteReal( fs, 0, remap_undistort_params[i] );
1402 cvEndWriteStruct(fs);
1409 void CV_UndistortMapTest::get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types )
1411 CvRNG* rng = ts->get_rng();
1412 CvArrTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
1413 int depth = cvTsRandInt(rng)%2 ? CV_64F : CV_32F;
1414 CvSize sz = sizes[OUTPUT][0];
1415 types[INPUT][0] = types[INPUT][1] = depth;
1416 types[OUTPUT][0] = types[OUTPUT][1] =
1417 types[REF_OUTPUT][0] = types[REF_OUTPUT][1] = CV_32F;
1418 sizes[INPUT][0] = cvSize(3,3);
1419 sizes[INPUT][1] = cvTsRandInt(rng)%2 ? cvSize(4,1) : cvSize(1,4);
1421 sz.width = MAX(sz.width,16);
1422 sz.height = MAX(sz.height,16);
1423 sizes[OUTPUT][0] = sizes[OUTPUT][1] =
1424 sizes[REF_OUTPUT][0] = sizes[REF_OUTPUT][1] = sz;
1428 void CV_UndistortMapTest::fill_array( int test_case_idx, int i, int j, CvMat* arr )
1431 CvArrTest::fill_array( test_case_idx, i, j, arr );
1434 void CV_UndistortMapTest::get_timing_test_array_types_and_sizes( int test_case_idx,
1435 CvSize** sizes, int** types, CvSize** whole_sizes, bool *are_images )
1437 CvArrTest::get_timing_test_array_types_and_sizes( test_case_idx, sizes, types,
1438 whole_sizes, are_images );
1442 void CV_UndistortMapTest::print_timing_params( int test_case_idx, char* ptr, int params_left )
1444 CvArrTest::print_timing_params( test_case_idx, ptr, params_left );
1448 void CV_UndistortMapTest::run_func()
1450 cvInitUndistortMap( &test_mat[INPUT][0], &test_mat[INPUT][1],
1451 test_array[OUTPUT][0], test_array[OUTPUT][1] );
1455 double CV_UndistortMapTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
1461 int CV_UndistortMapTest::prepare_test_case( int test_case_idx )
1463 CvRNG* rng = ts->get_rng();
1464 int code = CvArrTest::prepare_test_case( test_case_idx );
1465 const CvMat* mapx = &test_mat[OUTPUT][0];
1466 double k[4], a[9] = {0,0,0,0,0,0,0,0,1};
1467 double sz = MAX(mapx->rows, mapx->cols);
1468 CvMat* _a0 = &test_mat[INPUT][0], *_k0 = &test_mat[INPUT][1];
1469 CvMat _a = cvMat(3,3,CV_64F,a);
1470 CvMat _k = cvMat(_k0->rows,_k0->cols, CV_MAKETYPE(CV_64F,CV_MAT_CN(_k0->type)),k);
1475 if( ts->get_testing_mode() == CvTS::CORRECTNESS_CHECK_MODE )
1477 double aspect_ratio = cvTsRandReal(rng)*0.6 + 0.7;
1478 a[2] = (mapx->cols - 1)*0.5 + cvTsRandReal(rng)*10 - 5;
1479 a[5] = (mapx->rows - 1)*0.5 + cvTsRandReal(rng)*10 - 5;
1480 a[0] = sz/(0.9 - cvTsRandReal(rng)*0.6);
1481 a[4] = aspect_ratio*a[0];
1482 k[0] = cvTsRandReal(rng)*0.06 - 0.03;
1483 k[1] = cvTsRandReal(rng)*0.06 - 0.03;
1486 k[2] = cvTsRandReal(rng)*0.004 - 0.002;
1487 k[3] = cvTsRandReal(rng)*0.004 - 0.002;
1492 a[2] = (mapx->cols - 1)*remap_undistort_params[0];
1493 a[5] = (mapx->rows - 1)*remap_undistort_params[1];
1494 a[0] = sz/remap_undistort_params[2];
1495 a[4] = sz/remap_undistort_params[3];
1496 for( i = 0; i < 4; i++ )
1497 k[i] = remap_undistort_params[i+4];
1500 cvTsConvert( &_a, _a0 );
1501 cvTsConvert( &_k, _k0 );
1507 void CV_UndistortMapTest::prepare_to_validation( int /*test_case_idx*/ )
1509 cvTsInitUndistortMap( &test_mat[INPUT][0], &test_mat[INPUT][1],
1510 &test_mat[REF_OUTPUT][0], &test_mat[REF_OUTPUT][1] );
1514 CV_UndistortMapTest undistortmap_test;
1518 ////////////////////////////// GetRectSubPix /////////////////////////////////
1520 static const CvSize rectsubpix_sizes[] = {{11, 11}, {21,21}, {41,41},{-1,-1}};
1523 cvTsGetQuadrangeSubPix( const CvMat* src, CvMat* dst, double* a )
1526 int sstep = src->step / sizeof(float);
1527 int scols = src->cols, srows = src->rows;
1529 assert( CV_MAT_DEPTH(src->type) == CV_32F &&
1530 CV_ARE_TYPES_EQ(src, dst));
1532 cn = CV_MAT_CN(dst->type);
1534 for( y = 0; y < dst->rows; y++ )
1535 for( x = 0; x < dst->cols; x++ )
1537 float* d = (float*)(dst->data.ptr + y*dst->step) + x*cn;
1538 float sx = (float)(a[0]*x + a[1]*y + a[2]);
1539 float sy = (float)(a[3]*x + a[4]*y + a[5]);
1540 int ix = cvFloor(sx), iy = cvFloor(sy);
1541 int dx = cn, dy = sstep;
1545 if( (unsigned)ix >= (unsigned)(scols-1) )
1546 ix = ix < 0 ? 0 : scols - 1, sx = 0, dx = 0;
1547 if( (unsigned)iy >= (unsigned)(srows-1) )
1548 iy = iy < 0 ? 0 : srows - 1, sy = 0, dy = 0;
1550 s = src->data.fl + sstep*iy + ix*cn;
1551 for( k = 0; k < cn; k++, s++ )
1553 float t0 = s[0] + sx*(s[dx] - s[0]);
1554 float t1 = s[dy] + sx*(s[dy + dx] - s[dy]);
1555 d[k] = t0 + sy*(t1 - t0);
1561 class CV_GetRectSubPixTest : public CV_ImgWarpBaseTest
1564 CV_GetRectSubPixTest();
1567 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
1569 int prepare_test_case( int test_case_idx );
1570 void prepare_to_validation( int /*test_case_idx*/ );
1571 double get_success_error_level( int test_case_idx, int i, int j );
1572 void fill_array( int test_case_idx, int i, int j, CvMat* arr );
1574 int write_default_params(CvFileStorage* fs);
1575 void get_timing_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types,
1576 CvSize** whole_sizes, bool *are_images );
1577 void print_timing_params( int test_case_idx, char* ptr, int params_left );
1578 CvPoint2D32f center;
1582 CV_GetRectSubPixTest::CV_GetRectSubPixTest()
1583 : CV_ImgWarpBaseTest( "warp-subpix-rect", "cvGetRectSubPix", false )
1585 //spatial_scale_zoom = spatial_scale_decimate;
1586 spatial_scale_decimate = spatial_scale_zoom;
1587 //default_timing_param_names = imgwarp_perspective_param_names;
1588 support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
1589 default_timing_param_names = 0;
1593 int CV_GetRectSubPixTest::write_default_params( CvFileStorage* fs )
1595 int code = CV_ImgWarpBaseTest::write_default_params( fs );
1599 if( ts->get_testing_mode() == CvTS::TIMING_MODE )
1602 start_write_param( fs );
1604 cvStartWriteStruct( fs, "rect_size", CV_NODE_SEQ+CV_NODE_FLOW );
1605 for( i = 0; rectsubpix_sizes[i].width > 0; i++ )
1607 cvStartWriteStruct( fs, 0, CV_NODE_SEQ+CV_NODE_FLOW );
1608 cvWriteInt( fs, 0, rectsubpix_sizes[i].width );
1609 cvWriteInt( fs, 0, rectsubpix_sizes[i].height );
1610 cvEndWriteStruct(fs);
1612 cvEndWriteStruct(fs);
1619 void CV_GetRectSubPixTest::get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types )
1621 CvRNG* rng = ts->get_rng();
1622 CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
1623 int src_depth = cvTsRandInt(rng) % 2, dst_depth;
1624 int cn = cvTsRandInt(rng) % 2 ? 3 : 1;
1625 CvSize src_size, dst_size;
1627 dst_depth = src_depth = src_depth == 0 ? CV_8U : CV_32F;
1628 if( src_depth < CV_32F && cvTsRandInt(rng) % 2 )
1631 types[INPUT][0] = CV_MAKETYPE(src_depth,cn);
1632 types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = CV_MAKETYPE(dst_depth,cn);
1634 src_size = sizes[INPUT][0];
1635 dst_size.width = cvRound(sqrt(cvTsRandReal(rng)*src_size.width) + 1);
1636 dst_size.height = cvRound(sqrt(cvTsRandReal(rng)*src_size.height) + 1);
1637 dst_size.width = MIN(dst_size.width,src_size.width);
1638 dst_size.height = MIN(dst_size.width,src_size.height);
1639 sizes[INPUT_OUTPUT][0] = sizes[REF_INPUT_OUTPUT][0] = dst_size;
1641 center.x = (float)(cvTsRandReal(rng)*src_size.width);
1642 center.y = (float)(cvTsRandReal(rng)*src_size.height);
1643 interpolation = CV_INTER_LINEAR;
1647 void CV_GetRectSubPixTest::fill_array( int test_case_idx, int i, int j, CvMat* arr )
1650 CV_ImgWarpBaseTestImpl::fill_array( test_case_idx, i, j, arr );
1653 void CV_GetRectSubPixTest::get_timing_test_array_types_and_sizes( int test_case_idx,
1654 CvSize** sizes, int** types, CvSize** whole_sizes, bool *are_images )
1656 CV_ImgWarpBaseTest::get_timing_test_array_types_and_sizes( test_case_idx, sizes, types,
1657 whole_sizes, are_images );
1658 interpolation = CV_INTER_LINEAR;
1662 void CV_GetRectSubPixTest::print_timing_params( int test_case_idx, char* ptr, int params_left )
1664 CV_ImgWarpBaseTest::print_timing_params( test_case_idx, ptr, params_left );
1668 void CV_GetRectSubPixTest::run_func()
1670 cvGetRectSubPix( test_array[INPUT][0], test_array[INPUT_OUTPUT][0], center );
1674 double CV_GetRectSubPixTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
1676 int in_depth = CV_MAT_DEPTH(test_mat[INPUT][0].type);
1677 int out_depth = CV_MAT_DEPTH(test_mat[INPUT_OUTPUT][0].type);
1679 return in_depth >= CV_32F ? 1e-3 : out_depth >= CV_32F ? 1e-2 : 1;
1683 int CV_GetRectSubPixTest::prepare_test_case( int test_case_idx )
1685 return CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
1689 void CV_GetRectSubPixTest::prepare_to_validation( int /*test_case_idx*/ )
1691 CvMat* src0 = &test_mat[INPUT][0];
1692 CvMat* dst0 = &test_mat[REF_INPUT_OUTPUT][0];
1693 CvMat* src = src0, *dst = dst0;
1694 int ftype = CV_MAKETYPE(CV_32F,CV_MAT_CN(src0->type));
1695 double a[] = { 1, 0, center.x - dst->cols*0.5 + 0.5,
1696 0, 1, center.y - dst->rows*0.5 + 0.5 };
1697 if( CV_MAT_DEPTH(src->type) != CV_32F )
1699 src = cvCreateMat( src0->rows, src0->cols, ftype );
1700 cvTsConvert( src0, src );
1703 if( CV_MAT_DEPTH(dst->type) != CV_32F )
1704 dst = cvCreateMat( dst0->rows, dst0->cols, ftype );
1706 cvTsGetQuadrangeSubPix( src, dst, a );
1710 cvTsConvert( dst, dst0 );
1711 cvReleaseMat( &dst );
1714 cvReleaseMat( &src );
1718 CV_GetRectSubPixTest subpix_rect_test;
1721 class CV_GetQuadSubPixTest : public CV_ImgWarpBaseTest
1724 CV_GetQuadSubPixTest();
1727 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
1729 int prepare_test_case( int test_case_idx );
1730 void prepare_to_validation( int /*test_case_idx*/ );
1731 double get_success_error_level( int test_case_idx, int i, int j );
1733 int write_default_params(CvFileStorage* fs);
1734 void get_timing_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types,
1735 CvSize** whole_sizes, bool *are_images );
1736 void print_timing_params( int test_case_idx, char* ptr, int params_left );
1740 CV_GetQuadSubPixTest::CV_GetQuadSubPixTest()
1741 : CV_ImgWarpBaseTest( "warp-subpix-quad", "cvGetQuadSubPix", true )
1743 //spatial_scale_zoom = spatial_scale_decimate;
1744 spatial_scale_decimate = spatial_scale_zoom;
1745 //default_timing_param_names = imgwarp_affine_param_names;
1746 support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
1747 default_timing_param_names = 0;
1751 int CV_GetQuadSubPixTest::write_default_params( CvFileStorage* fs )
1753 int code = CV_ImgWarpBaseTest::write_default_params( fs );
1757 if( ts->get_testing_mode() == CvTS::TIMING_MODE )
1760 start_write_param( fs );
1762 cvStartWriteStruct( fs, "rotate_scale", CV_NODE_SEQ+CV_NODE_FLOW );
1763 for( i = 0; imgwarp_affine_rotate_scale[i][0] >= 0; i++ )
1765 cvStartWriteStruct( fs, 0, CV_NODE_SEQ+CV_NODE_FLOW );
1766 cvWriteRawData( fs, imgwarp_affine_rotate_scale[i], 4, "d" );
1767 cvEndWriteStruct(fs);
1769 cvEndWriteStruct(fs);
1776 void CV_GetQuadSubPixTest::get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types )
1779 CV_ImgWarpBaseTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
1780 CvSize sz = sizes[INPUT][0], dsz;
1781 CvRNG* rng = ts->get_rng();
1782 int msz, src_depth = cvTsRandInt(rng) % 2, dst_depth;
1783 int cn = cvTsRandInt(rng) % 2 ? 3 : 1;
1785 dst_depth = src_depth = src_depth == 0 ? CV_8U : CV_32F;
1786 if( src_depth < CV_32F && cvTsRandInt(rng) % 2 )
1789 types[INPUT][0] = CV_MAKETYPE(src_depth,cn);
1790 types[INPUT_OUTPUT][0] = types[REF_INPUT_OUTPUT][0] = CV_MAKETYPE(dst_depth,cn);
1792 sz.width = MAX(sz.width,min_size);
1793 sz.height = MAX(sz.height,min_size);
1794 sizes[INPUT][0] = sz;
1795 msz = MIN( sz.width, sz.height );
1797 dsz.width = cvRound(sqrt(cvTsRandReal(rng)*msz) + 1);
1798 dsz.height = cvRound(sqrt(cvTsRandReal(rng)*msz) + 1);
1799 dsz.width = MIN(dsz.width,msz);
1800 dsz.height = MIN(dsz.width,msz);
1801 dsz.width = MAX(dsz.width,min_size);
1802 dsz.height = MAX(dsz.height,min_size);
1803 sizes[INPUT_OUTPUT][0] = sizes[REF_INPUT_OUTPUT][0] = dsz;
1804 sizes[INPUT][1] = cvSize( 3, 2 );
1808 void CV_GetQuadSubPixTest::get_timing_test_array_types_and_sizes( int test_case_idx,
1809 CvSize** sizes, int** types, CvSize** whole_sizes, bool *are_images )
1811 CV_ImgWarpBaseTest::get_timing_test_array_types_and_sizes( test_case_idx, sizes, types,
1812 whole_sizes, are_images );
1814 sizes[INPUT][1] = whole_sizes[INPUT][1] = cvSize(3,2);
1815 sizes[TEMP][0] = whole_sizes[TEMP][0] =
1816 sizes[TEMP][1] = whole_sizes[TEMP][1] = cvSize(0,0);
1817 types[INPUT][1] = CV_64FC1;
1819 interpolation = CV_INTER_LINEAR;
1823 void CV_GetQuadSubPixTest::print_timing_params( int test_case_idx, char* ptr, int params_left )
1826 const CvFileNode* node = find_timing_param( "rotate_scale" );
1827 assert( node && CV_NODE_IS_SEQ(node->tag) );
1828 cvReadRawData( ts->get_file_storage(), node, coeffs, "4d" );
1830 sprintf( ptr, "fx=%.2f,fy=%.2f,angle=%.1fdeg,scale=%.1f,", coeffs[0], coeffs[1], coeffs[2], coeffs[3] );
1834 CV_ImgWarpBaseTest::print_timing_params( test_case_idx, ptr, params_left );
1838 void CV_GetQuadSubPixTest::run_func()
1840 cvGetQuadrangleSubPix( test_array[INPUT][0],
1841 test_array[INPUT_OUTPUT][0], &test_mat[INPUT][1] );
1845 double CV_GetQuadSubPixTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
1847 int in_depth = CV_MAT_DEPTH(test_mat[INPUT][0].type);
1848 //int out_depth = CV_MAT_DEPTH(test_mat[INPUT_OUTPUT][0].type);
1850 return in_depth >= CV_32F ? 1e-2 : 4;
1854 int CV_GetQuadSubPixTest::prepare_test_case( int test_case_idx )
1856 CvRNG* rng = ts->get_rng();
1857 int code = CV_ImgWarpBaseTest::prepare_test_case( test_case_idx );
1858 const CvMat* src = &test_mat[INPUT][0];
1859 CvMat* mat = &test_mat[INPUT][1];
1860 CvPoint2D32f center;
1861 double scale, angle;
1866 if( ts->get_testing_mode() == CvTS::CORRECTNESS_CHECK_MODE )
1869 CvMat A = cvMat( 2, 3, CV_64FC1, a );
1871 center.x = (float)((cvTsRandReal(rng)*1.2 - 0.1)*src->cols);
1872 center.y = (float)((cvTsRandReal(rng)*1.2 - 0.1)*src->rows);
1873 angle = cvTsRandReal(rng)*360;
1874 scale = cvTsRandReal(rng)*0.2 + 0.9;
1876 // y = Ax + b -> x = A^-1(y - b) = A^-1*y - A^-1*b
1878 angle = angle*(CV_PI/180.);
1879 a[0] = a[4] = cos(angle)*scale;
1880 a[1] = sin(angle)*scale;
1882 a[2] = center.x - a[0]*center.x - a[1]*center.y;
1883 a[5] = center.y - a[3]*center.x - a[4]*center.y;
1884 cvTsConvert( &A, mat );
1891 void CV_GetQuadSubPixTest::prepare_to_validation( int /*test_case_idx*/ )
1893 CvMat* src0 = &test_mat[INPUT][0];
1894 CvMat* dst0 = &test_mat[REF_INPUT_OUTPUT][0];
1895 CvMat* src = src0, *dst = dst0;
1896 int ftype = CV_MAKETYPE(CV_32F,CV_MAT_CN(src0->type));
1897 double a[6], dx = (dst0->cols - 1)*0.5, dy = (dst0->rows - 1)*0.5;
1898 CvMat A = cvMat( 2, 3, CV_64F, a );
1900 if( CV_MAT_DEPTH(src->type) != CV_32F )
1902 src = cvCreateMat( src0->rows, src0->cols, ftype );
1903 cvTsConvert( src0, src );
1906 if( CV_MAT_DEPTH(dst->type) != CV_32F )
1907 dst = cvCreateMat( dst0->rows, dst0->cols, ftype );
1909 cvTsConvert( &test_mat[INPUT][1], &A );
1910 a[2] -= a[0]*dx + a[1]*dy;
1911 a[5] -= a[3]*dx + a[4]*dy;
1912 cvTsGetQuadrangeSubPix( src, dst, a );
1916 cvTsConvert( dst, dst0 );
1917 cvReleaseMat( &dst );
1921 cvReleaseMat( &src );
1925 CV_GetQuadSubPixTest warp_subpix_quad_test;