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
44 using namespace cv;
45 using namespace std;
46
47 class CV_CannyTest : public cvtest::ArrayTest
48 {
49 public:
50 CV_CannyTest();
51
52 protected:
53 void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
54 double get_success_error_level( int test_case_idx, int i, int j );
55 int prepare_test_case( int test_case_idx );
56 void run_func();
57 void prepare_to_validation( int );
58 int validate_test_results( int /*test_case_idx*/ );
59
60 int aperture_size;
61 bool use_true_gradient;
62 double threshold1, threshold2;
63 bool test_cpp;
64 };
65
66
CV_CannyTest()67 CV_CannyTest::CV_CannyTest()
68 {
69 test_array[INPUT].push_back(NULL);
70 test_array[OUTPUT].push_back(NULL);
71 test_array[REF_OUTPUT].push_back(NULL);
72 element_wise_relative_error = true;
73 aperture_size = 0;
74 use_true_gradient = false;
75 threshold1 = threshold2 = 0;
76
77 test_cpp = false;
78 }
79
80
get_test_array_types_and_sizes(int test_case_idx,vector<vector<Size>> & sizes,vector<vector<int>> & types)81 void CV_CannyTest::get_test_array_types_and_sizes( int test_case_idx,
82 vector<vector<Size> >& sizes,
83 vector<vector<int> >& types )
84 {
85 RNG& rng = ts->get_rng();
86 double thresh_range;
87
88 cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
89 types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_8U;
90
91 aperture_size = cvtest::randInt(rng) % 2 ? 5 : 3;
92 thresh_range = aperture_size == 3 ? 300 : 1000;
93
94 threshold1 = cvtest::randReal(rng)*thresh_range;
95 threshold2 = cvtest::randReal(rng)*thresh_range*0.3;
96
97 if( cvtest::randInt(rng) % 2 )
98 CV_SWAP( threshold1, threshold2, thresh_range );
99
100 use_true_gradient = cvtest::randInt(rng) % 2 != 0;
101 test_cpp = (cvtest::randInt(rng) & 256) == 0;
102 }
103
104
prepare_test_case(int test_case_idx)105 int CV_CannyTest::prepare_test_case( int test_case_idx )
106 {
107 int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
108 if( code > 0 )
109 {
110 Mat& src = test_mat[INPUT][0];
111 GaussianBlur(src, src, Size(11, 11), 5, 5);
112 }
113
114 return code;
115 }
116
117
get_success_error_level(int,int,int)118 double CV_CannyTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
119 {
120 return 0;
121 }
122
123
run_func()124 void CV_CannyTest::run_func()
125 {
126 if(!test_cpp)
127 cvCanny( test_array[INPUT][0], test_array[OUTPUT][0], threshold1, threshold2,
128 aperture_size + (use_true_gradient ? CV_CANNY_L2_GRADIENT : 0));
129 else
130 {
131 cv::Mat _out = cv::cvarrToMat(test_array[OUTPUT][0]);
132 cv::Canny(cv::cvarrToMat(test_array[INPUT][0]), _out, threshold1, threshold2,
133 aperture_size + (use_true_gradient ? CV_CANNY_L2_GRADIENT : 0));
134 }
135 }
136
137
138 static void
cannyFollow(int x,int y,float lowThreshold,const Mat & mag,Mat & dst)139 cannyFollow( int x, int y, float lowThreshold, const Mat& mag, Mat& dst )
140 {
141 static const int ofs[][2] = {{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
142 int i;
143
144 dst.at<uchar>(y, x) = (uchar)255;
145
146 for( i = 0; i < 8; i++ )
147 {
148 int x1 = x + ofs[i][0];
149 int y1 = y + ofs[i][1];
150 if( (unsigned)x1 < (unsigned)mag.cols &&
151 (unsigned)y1 < (unsigned)mag.rows &&
152 mag.at<float>(y1, x1) > lowThreshold &&
153 !dst.at<uchar>(y1, x1) )
154 cannyFollow( x1, y1, lowThreshold, mag, dst );
155 }
156 }
157
158
159 static void
test_Canny(const Mat & src,Mat & dst,double threshold1,double threshold2,int aperture_size,bool use_true_gradient)160 test_Canny( const Mat& src, Mat& dst,
161 double threshold1, double threshold2,
162 int aperture_size, bool use_true_gradient )
163 {
164 int m = aperture_size;
165 Point anchor(m/2, m/2);
166 const double tan_pi_8 = tan(CV_PI/8.);
167 const double tan_3pi_8 = tan(CV_PI*3/8);
168 float lowThreshold = (float)MIN(threshold1, threshold2);
169 float highThreshold = (float)MAX(threshold1, threshold2);
170
171 int x, y, width = src.cols, height = src.rows;
172
173 Mat dxkernel = cvtest::calcSobelKernel2D( 1, 0, m, 0 );
174 Mat dykernel = cvtest::calcSobelKernel2D( 0, 1, m, 0 );
175 Mat dx, dy, mag(height, width, CV_32F);
176 cvtest::filter2D(src, dx, CV_16S, dxkernel, anchor, 0, BORDER_REPLICATE);
177 cvtest::filter2D(src, dy, CV_16S, dykernel, anchor, 0, BORDER_REPLICATE);
178
179 // calc gradient magnitude
180 for( y = 0; y < height; y++ )
181 {
182 for( x = 0; x < width; x++ )
183 {
184 int dxval = dx.at<short>(y, x), dyval = dy.at<short>(y, x);
185 mag.at<float>(y, x) = use_true_gradient ?
186 (float)sqrt((double)(dxval*dxval + dyval*dyval)) :
187 (float)(fabs((double)dxval) + fabs((double)dyval));
188 }
189 }
190
191 // calc gradient direction, do nonmaxima suppression
192 for( y = 0; y < height; y++ )
193 {
194 for( x = 0; x < width; x++ )
195 {
196
197 float a = mag.at<float>(y, x), b = 0, c = 0;
198 int y1 = 0, y2 = 0, x1 = 0, x2 = 0;
199
200 if( a <= lowThreshold )
201 continue;
202
203 int dxval = dx.at<short>(y, x);
204 int dyval = dy.at<short>(y, x);
205
206 double tg = dxval ? (double)dyval/dxval : DBL_MAX*CV_SIGN(dyval);
207
208 if( fabs(tg) < tan_pi_8 )
209 {
210 y1 = y2 = y; x1 = x + 1; x2 = x - 1;
211 }
212 else if( tan_pi_8 <= tg && tg <= tan_3pi_8 )
213 {
214 y1 = y + 1; y2 = y - 1; x1 = x + 1; x2 = x - 1;
215 }
216 else if( -tan_3pi_8 <= tg && tg <= -tan_pi_8 )
217 {
218 y1 = y - 1; y2 = y + 1; x1 = x + 1; x2 = x - 1;
219 }
220 else
221 {
222 assert( fabs(tg) > tan_3pi_8 );
223 x1 = x2 = x; y1 = y + 1; y2 = y - 1;
224 }
225
226 if( (unsigned)y1 < (unsigned)height && (unsigned)x1 < (unsigned)width )
227 b = (float)fabs(mag.at<float>(y1, x1));
228
229 if( (unsigned)y2 < (unsigned)height && (unsigned)x2 < (unsigned)width )
230 c = (float)fabs(mag.at<float>(y2, x2));
231
232 if( (a > b || (a == b && ((x1 == x+1 && y1 == y) || (x1 == x && y1 == y+1)))) && a > c )
233 ;
234 else
235 mag.at<float>(y, x) = -a;
236 }
237 }
238
239 dst = Scalar::all(0);
240
241 // hysteresis threshold
242 for( y = 0; y < height; y++ )
243 {
244 for( x = 0; x < width; x++ )
245 if( mag.at<float>(y, x) > highThreshold && !dst.at<uchar>(y, x) )
246 cannyFollow( x, y, lowThreshold, mag, dst );
247 }
248 }
249
250
prepare_to_validation(int)251 void CV_CannyTest::prepare_to_validation( int )
252 {
253 Mat src = test_mat[INPUT][0], dst = test_mat[REF_OUTPUT][0];
254 test_Canny( src, dst, threshold1, threshold2, aperture_size, use_true_gradient );
255 }
256
257
validate_test_results(int test_case_idx)258 int CV_CannyTest::validate_test_results( int test_case_idx )
259 {
260 int code = cvtest::TS::OK, nz0;
261 prepare_to_validation(test_case_idx);
262
263 double err = cvtest::norm(test_mat[OUTPUT][0], test_mat[REF_OUTPUT][0], CV_L1);
264 if( err == 0 )
265 return code;
266
267 if( err != cvRound(err) || cvRound(err)%255 != 0 )
268 {
269 ts->printf( cvtest::TS::LOG, "Some of the pixels, produced by Canny, are not 0's or 255's; the difference is %g\n", err );
270 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
271 return code;
272 }
273
274 nz0 = cvRound(cvtest::norm(test_mat[REF_OUTPUT][0], CV_L1)/255);
275 err = (err/255/MAX(nz0,100))*100;
276 if( err > 1 )
277 {
278 ts->printf( cvtest::TS::LOG, "Too high percentage of non-matching edge pixels = %g%%\n", err);
279 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
280 }
281
282 return code;
283 }
284
TEST(Imgproc_Canny,accuracy)285 TEST(Imgproc_Canny, accuracy) { CV_CannyTest test; test.safe_run(); }
286
287 /* End of file. */
288