75f0b1a4e427965d4d459abdfe86b48fa6507b58
[opencv] / tests / cv / src / acanny.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 "cvtest.h"
43
44 class CV_CannyTest : public CvArrTest
45 {
46 public:
47     CV_CannyTest();
48
49 protected:
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 );
53     void run_func();
54     void prepare_to_validation( int );
55
56     int aperture_size, use_true_gradient;
57     double threshold1, threshold2;
58 };
59
60
61 CV_CannyTest::CV_CannyTest()
62     : CvArrTest( "canny", "cvCanny, cvSobel", "" )
63 {
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;
70
71     support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
72     default_timing_param_names = 0;
73 }
74
75
76 void CV_CannyTest::get_test_array_types_and_sizes( int test_case_idx,
77                                                 CvSize** sizes, int** types )
78 {
79     CvRNG* rng = ts->get_rng();
80     double thresh_range;
81
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;
84
85     aperture_size = cvTsRandInt(rng) % 2 ? 5 : 3;
86     thresh_range = aperture_size == 3 ? 300 : 1000;
87     
88     threshold1 = cvTsRandReal(rng)*thresh_range;
89     threshold2 = cvTsRandReal(rng)*thresh_range*0.3;
90
91     if( cvTsRandInt(rng) % 2 )
92         CV_SWAP( threshold1, threshold2, thresh_range );
93
94     use_true_gradient = cvTsRandInt(rng) % 2;
95 }
96
97
98 int CV_CannyTest::prepare_test_case( int test_case_idx )
99 {
100     int code = CvArrTest::prepare_test_case( test_case_idx );
101     if( code > 0 )
102     {
103         CvMat* src = &test_mat[INPUT][0];
104         cvSmooth( src, src, CV_GAUSSIAN, 11, 11, 5, 5 );
105     }
106
107     return code;
108 }
109
110
111 double CV_CannyTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
112 {
113     return 0;
114 }
115
116
117 void CV_CannyTest::run_func()
118 {
119     cvCanny( test_array[INPUT][0], test_array[OUTPUT][0], threshold1, threshold2,
120             aperture_size + (use_true_gradient ? CV_CANNY_L2_GRADIENT : 0));
121 }
122
123
124 static void
125 icvTsCannyFollow( int x, int y, float lowThreshold, const CvMat* mag, CvMat* dst )
126 {
127     static const int ofs[][2] = {{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
128     int i;
129
130     dst->data.ptr[dst->step*y + x] = (uchar)255;
131
132     for( i = 0; i < 8; i++ )
133     {
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 );
141     }
142 }
143
144
145 static void
146 icvTsCanny( const CvMat* src, CvMat* dst,
147             double threshold1, double threshold2,
148             int aperture_size, int use_true_gradient )
149 {
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);
161
162     int x, y, width = src->cols, height = src->rows;
163
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 );
170
171     /* estimate magnitude and angle */
172     for( y = 0; y < height; y++ )
173     {
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);
177         
178         for( x = 0; x < width; x++ )
179         {
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]));
183             _mag[x] = mval;
184         }
185     }
186
187     /* nonmaxima suppression */
188     for( y = 0; y < height; y++ )
189     {
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);
193         
194         for( x = 0; x < width; x++ )
195         {
196             int y1 = 0, y2 = 0, x1 = 0, x2 = 0;
197             double tg;
198             float a = _mag[x], b = 0, c = 0;
199
200             if( a <= lowThreshold )
201                 continue;
202
203             if( _dx[x] )
204                 tg = (double)_dy[x]/_dx[x];
205             else
206                 tg = DBL_MAX*CV_SIGN(_dy[x]);
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((double)mag->data.fl[y1*width+x1]);
228
229             if( (unsigned)y2 < (unsigned)height && (unsigned)x2 < (unsigned)width )
230                 c = (float)fabs((double)mag->data.fl[y2*width+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[x] = -a;
236         }
237     }
238
239     cvTsZero( dst );
240
241     /* hysteresis threshold */
242     for( y = 0; y < height; y++ )
243     {
244         const float* _mag = (float*)(mag->data.ptr + mag->step*y);
245         uchar* _dst = dst->data.ptr + dst->step*y;
246
247         for( x = 0; x < width; x++ )
248             if( _mag[x] > highThreshold && !_dst[x] )
249                 icvTsCannyFollow( x, y, lowThreshold, mag, dst );
250     }
251
252     cvReleaseMat( &_src );
253     cvReleaseMat( &dx );
254     cvReleaseMat( &dy );
255     cvReleaseMat( &kernel );
256     cvReleaseMat( &mag );
257 }
258
259
260 void CV_CannyTest::prepare_to_validation( int )
261 {
262     icvTsCanny( &test_mat[INPUT][0], &test_mat[REF_OUTPUT][0],
263                 threshold1, threshold2, aperture_size, use_true_gradient );
264 }
265
266 CV_CannyTest canny_test;
267
268 /* End of file. */