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 class CV_CannyTest : public CvArrTest
50 void get_test_array_types_and_sizes( int test_case_idx, CvSize** sizes, int** types );
51 double get_success_error_level( int test_case_idx, int i, int j );
52 int prepare_test_case( int test_case_idx );
54 void prepare_to_validation( int );
56 int aperture_size, use_true_gradient;
57 double threshold1, threshold2;
61 CV_CannyTest::CV_CannyTest()
62 : CvArrTest( "canny", "cvCanny, cvSobel", "" )
64 test_array[INPUT].push(NULL);
65 test_array[OUTPUT].push(NULL);
66 test_array[REF_OUTPUT].push(NULL);
67 element_wise_relative_error = true;
68 aperture_size = use_true_gradient = 0;
69 threshold1 = threshold2 = 0;
71 support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
72 default_timing_param_names = 0;
76 void CV_CannyTest::get_test_array_types_and_sizes( int test_case_idx,
77 CvSize** sizes, int** types )
79 CvRNG* rng = ts->get_rng();
82 CvArrTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
83 types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_8U;
85 aperture_size = cvTsRandInt(rng) % 2 ? 5 : 3;
86 thresh_range = aperture_size == 3 ? 300 : 1000;
88 threshold1 = cvTsRandReal(rng)*thresh_range;
89 threshold2 = cvTsRandReal(rng)*thresh_range*0.3;
91 if( cvTsRandInt(rng) % 2 )
92 CV_SWAP( threshold1, threshold2, thresh_range );
94 use_true_gradient = cvTsRandInt(rng) % 2;
98 int CV_CannyTest::prepare_test_case( int test_case_idx )
100 int code = CvArrTest::prepare_test_case( test_case_idx );
103 CvMat* src = &test_mat[INPUT][0];
104 cvSmooth( src, src, CV_GAUSSIAN, 11, 11, 5, 5 );
111 double CV_CannyTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
117 void CV_CannyTest::run_func()
119 cvCanny( test_array[INPUT][0], test_array[OUTPUT][0], threshold1, threshold2,
120 aperture_size + (use_true_gradient ? CV_CANNY_L2_GRADIENT : 0));
125 icvTsCannyFollow( int x, int y, float lowThreshold, const CvMat* mag, CvMat* dst )
127 static const int ofs[][2] = {{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
130 dst->data.ptr[dst->step*y + x] = (uchar)255;
132 for( i = 0; i < 8; i++ )
134 int x1 = x + ofs[i][0];
135 int y1 = y + ofs[i][1];
136 if( (unsigned)x1 < (unsigned)mag->cols &&
137 (unsigned)y1 < (unsigned)mag->rows &&
138 mag->data.fl[y1*mag->cols+x1] > lowThreshold &&
139 !dst->data.ptr[dst->step*y1+x1] )
140 icvTsCannyFollow( x1, y1, lowThreshold, mag, dst );
146 icvTsCanny( const CvMat* src, CvMat* dst,
147 double threshold1, double threshold2,
148 int aperture_size, int use_true_gradient )
150 int m = aperture_size;
151 CvMat* _src = cvCreateMat( src->rows + m - 1, src->cols + m - 1, CV_16S );
152 CvMat* dx = cvCreateMat( src->rows, src->cols, CV_16S );
153 CvMat* dy = cvCreateMat( src->rows, src->cols, CV_16S );
154 CvMat* kernel = cvCreateMat( m, m, CV_32F );
155 CvPoint anchor = {m/2, m/2};
156 CvMat* mag = cvCreateMat( src->rows, src->cols, CV_32F );
157 const double tan_pi_8 = tan(CV_PI/8.);
158 const double tan_3pi_8 = tan(CV_PI*3/8);
159 float lowThreshold = (float)MIN(threshold1, threshold2);
160 float highThreshold = (float)MAX(threshold1, threshold2);
162 int x, y, width = src->cols, height = src->rows;
164 cvTsConvert( src, dx );
165 cvTsPrepareToFilter( dx, _src, anchor, CV_TS_BORDER_REPLICATE );
166 cvTsCalcSobelKernel2D( 1, 0, m, 0, kernel );
167 cvTsConvolve2D( _src, dx, kernel, anchor );
168 cvTsCalcSobelKernel2D( 0, 1, m, 0, kernel );
169 cvTsConvolve2D( _src, dy, kernel, anchor );
171 /* estimate magnitude and angle */
172 for( y = 0; y < height; y++ )
174 const short* _dx = (short*)(dx->data.ptr + dx->step*y);
175 const short* _dy = (short*)(dy->data.ptr + dy->step*y);
176 float* _mag = (float*)(mag->data.ptr + mag->step*y);
178 for( x = 0; x < width; x++ )
180 float mval = use_true_gradient ?
181 (float)sqrt((double)(_dx[x]*_dx[x] + _dy[x]*_dy[x])) :
182 (float)(abs(_dx[x]) + abs(_dy[x]));
187 /* nonmaxima suppression */
188 for( y = 0; y < height; y++ )
190 const short* _dx = (short*)(dx->data.ptr + dx->step*y);
191 const short* _dy = (short*)(dy->data.ptr + dy->step*y);
192 float* _mag = (float*)(mag->data.ptr + mag->step*y);
194 for( x = 0; x < width; x++ )
196 int y1 = 0, y2 = 0, x1 = 0, x2 = 0;
198 float a = _mag[x], b = 0, c = 0;
200 if( a <= lowThreshold )
204 tg = (double)_dy[x]/_dx[x];
206 tg = DBL_MAX*CV_SIGN(_dy[x]);
208 if( fabs(tg) < tan_pi_8 )
210 y1 = y2 = y; x1 = x + 1; x2 = x - 1;
212 else if( tan_pi_8 <= tg && tg <= tan_3pi_8 )
214 y1 = y + 1; y2 = y - 1; x1 = x + 1; x2 = x - 1;
216 else if( -tan_3pi_8 <= tg && tg <= -tan_pi_8 )
218 y1 = y - 1; y2 = y + 1; x1 = x + 1; x2 = x - 1;
222 assert( fabs(tg) > tan_3pi_8 );
223 x1 = x2 = x; y1 = y + 1; y2 = y - 1;
226 if( (unsigned)y1 < (unsigned)height && (unsigned)x1 < (unsigned)width )
227 b = (float)fabs((double)mag->data.fl[y1*width+x1]);
229 if( (unsigned)y2 < (unsigned)height && (unsigned)x2 < (unsigned)width )
230 c = (float)fabs((double)mag->data.fl[y2*width+x2]);
232 if( (a > b || a == b && (x1 == x+1 && y1 == y || x1 == x && y1 == y+1)) && a > c )
241 /* hysteresis threshold */
242 for( y = 0; y < height; y++ )
244 const float* _mag = (float*)(mag->data.ptr + mag->step*y);
245 uchar* _dst = dst->data.ptr + dst->step*y;
247 for( x = 0; x < width; x++ )
248 if( _mag[x] > highThreshold && !_dst[x] )
249 icvTsCannyFollow( x, y, lowThreshold, mag, dst );
252 cvReleaseMat( &_src );
255 cvReleaseMat( &kernel );
256 cvReleaseMat( &mag );
260 void CV_CannyTest::prepare_to_validation( int )
262 icvTsCanny( &test_mat[INPUT][0], &test_mat[REF_OUTPUT][0],
263 threshold1, threshold2, aperture_size, use_true_gradient );
266 CV_CannyTest canny_test;