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 icvCannyGetSize_t icvCannyGetSize_p = 0;
45 icvCanny_16s8u_C1R_t icvCanny_16s8u_C1R_p = 0;
48 cvCanny( const void* srcarr, void* dstarr,
49 double low_thresh, double high_thresh, int aperture_size )
51 CvMat *dx = 0, *dy = 0;
53 uchar **stack_top, **stack_bottom = 0;
55 CV_FUNCNAME( "cvCanny" );
59 CvMat srcstub, *src = (CvMat*)srcarr;
60 CvMat dststub, *dst = (CvMat*)dstarr;
62 int flags = aperture_size;
70 CV_CALL( src = cvGetMat( src, &srcstub ));
71 CV_CALL( dst = cvGetMat( dst, &dststub ));
73 if( CV_MAT_TYPE( src->type ) != CV_8UC1 ||
74 CV_MAT_TYPE( dst->type ) != CV_8UC1 )
75 CV_ERROR( CV_StsUnsupportedFormat, "" );
77 if( !CV_ARE_SIZES_EQ( src, dst ))
78 CV_ERROR( CV_StsUnmatchedSizes, "" );
80 if( low_thresh > high_thresh )
83 CV_SWAP( low_thresh, high_thresh, t );
86 aperture_size &= INT_MAX;
87 if( (aperture_size & 1) == 0 || aperture_size < 3 || aperture_size > 7 )
88 CV_ERROR( CV_StsBadFlag, "" );
90 size = cvGetMatSize( src );
92 dx = cvCreateMat( size.height, size.width, CV_16SC1 );
93 dy = cvCreateMat( size.height, size.width, CV_16SC1 );
94 cvSobel( src, dx, 1, 0, aperture_size );
95 cvSobel( src, dy, 0, 1, aperture_size );
97 if( icvCannyGetSize_p && icvCanny_16s8u_C1R_p && !(flags & CV_CANNY_L2_GRADIENT) )
100 IPPI_CALL( icvCannyGetSize_p( size, &buf_size ));
101 CV_CALL( buffer = cvAlloc( buf_size ));
102 IPPI_CALL( icvCanny_16s8u_C1R_p( (short*)dx->data.ptr, dx->step,
103 (short*)dy->data.ptr, dy->step,
104 dst->data.ptr, dst->step,
105 size, (float)low_thresh,
106 (float)high_thresh, buffer ));
110 if( flags & CV_CANNY_L2_GRADIENT )
113 ul.f = (float)low_thresh;
114 uh.f = (float)high_thresh;
121 low = cvFloor( low_thresh );
122 high = cvFloor( high_thresh );
125 CV_CALL( buffer = cvAlloc( (size.width+2)*(size.height+2) +
126 (size.width+2)*3*sizeof(int)) );
128 mag_buf[0] = (int*)buffer;
129 mag_buf[1] = mag_buf[0] + size.width + 2;
130 mag_buf[2] = mag_buf[1] + size.width + 2;
131 map = (uchar*)(mag_buf[2] + size.width + 2);
132 mapstep = size.width + 2;
134 maxsize = MAX( 1 << 10, size.width*size.height/10 );
135 CV_CALL( stack_top = stack_bottom = (uchar**)cvAlloc( maxsize*sizeof(stack_top[0]) ));
137 memset( mag_buf[0], 0, (size.width+2)*sizeof(int) );
138 memset( map, 1, mapstep );
139 memset( map + mapstep*(size.height + 1), 1, mapstep );
153 #define CANNY_PUSH(d) *(d) = (uchar)2, *stack_top++ = (d)
154 #define CANNY_POP(d) (d) = *--stack_top
156 mag_row = cvMat( 1, size.width, CV_32F );
158 // calculate magnitude and angle of gradient, perform non-maxima supression.
159 // fill the map with one of the following values:
160 // 0 - the pixel might belong to an edge
161 // 1 - the pixel can not belong to an edge
162 // 2 - the pixel does belong to an edge
163 for( i = 0; i <= size.height; i++ )
165 int* _mag = mag_buf[(i > 0) + 1] + 1;
166 float* _magf = (float*)_mag;
167 const short* _dx = (short*)(dx->data.ptr + dx->step*i);
168 const short* _dy = (short*)(dy->data.ptr + dy->step*i);
171 int magstep1, magstep2;
174 if( i < size.height )
176 _mag[-1] = _mag[size.width] = 0;
178 if( !(flags & CV_CANNY_L2_GRADIENT) )
179 for( j = 0; j < size.width; j++ )
180 _mag[j] = abs(_dx[j]) + abs(_dy[j]);
181 else if( icvFilterSobelVert_8u16s_C1R_p != 0 ) // check for IPP
183 // use vectorized sqrt
184 mag_row.data.fl = _magf;
185 for( j = 0; j < size.width; j++ )
187 x = _dx[j]; y = _dy[j];
188 _magf[j] = (float)((double)x*x + (double)y*y);
190 cvPow( &mag_row, &mag_row, 0.5 );
194 for( j = 0; j < size.width; j++ )
196 x = _dx[j]; y = _dy[j];
197 _magf[j] = (float)sqrt((double)x*x + (double)y*y);
202 memset( _mag-1, 0, (size.width + 2)*sizeof(int) );
204 // at the very beginning we do not have a complete ring
205 // buffer of 3 magnitude rows for non-maxima suppression
209 _map = map + mapstep*i + 1;
210 _map[-1] = _map[size.width] = 1;
212 _mag = mag_buf[1] + 1; // take the central row
213 _dx = (short*)(dx->data.ptr + dx->step*(i-1));
214 _dy = (short*)(dy->data.ptr + dy->step*(i-1));
216 magstep1 = (int)(mag_buf[2] - mag_buf[1]);
217 magstep2 = (int)(mag_buf[0] - mag_buf[1]);
219 if( (stack_top - stack_bottom) + size.width > maxsize )
221 uchar** new_stack_bottom;
222 maxsize = MAX( maxsize * 3/2, maxsize + size.width );
223 CV_CALL( new_stack_bottom = (uchar**)cvAlloc( maxsize * sizeof(stack_top[0])) );
224 memcpy( new_stack_bottom, stack_bottom, (stack_top - stack_bottom)*sizeof(stack_top[0]) );
225 stack_top = new_stack_bottom + (stack_top - stack_bottom);
226 cvFree( &stack_bottom );
227 stack_bottom = new_stack_bottom;
230 for( j = 0; j < size.width; j++ )
232 #define CANNY_SHIFT 15
233 #define TG22 (int)(0.4142135623730950488016887242097*(1<<CANNY_SHIFT) + 0.5)
244 int tg22x = x * TG22;
245 int tg67x = tg22x + ((x + x) << CANNY_SHIFT);
251 if( m > _mag[j-1] && m >= _mag[j+1] )
253 if( m > high && !prev_flag && _map[j-mapstep] != 2 )
255 CANNY_PUSH( _map + j );
265 if( m > _mag[j+magstep2] && m >= _mag[j+magstep1] )
267 if( m > high && !prev_flag && _map[j-mapstep] != 2 )
269 CANNY_PUSH( _map + j );
280 if( m > _mag[j+magstep2-s] && m > _mag[j+magstep1+s] )
282 if( m > high && !prev_flag && _map[j-mapstep] != 2 )
284 CANNY_PUSH( _map + j );
297 // scroll the ring buffer
299 mag_buf[0] = mag_buf[1];
300 mag_buf[1] = mag_buf[2];
304 // now track the edges (hysteresis thresholding)
305 while( stack_top > stack_bottom )
308 if( (stack_top - stack_bottom) + 8 > maxsize )
310 uchar** new_stack_bottom;
311 maxsize = MAX( maxsize * 3/2, maxsize + 8 );
312 CV_CALL( new_stack_bottom = (uchar**)cvAlloc( maxsize * sizeof(stack_top[0])) );
313 memcpy( new_stack_bottom, stack_bottom, (stack_top - stack_bottom)*sizeof(stack_top[0]) );
314 stack_top = new_stack_bottom + (stack_top - stack_bottom);
315 cvFree( &stack_bottom );
316 stack_bottom = new_stack_bottom;
326 CANNY_PUSH( m - mapstep - 1 );
328 CANNY_PUSH( m - mapstep );
330 CANNY_PUSH( m - mapstep + 1 );
332 CANNY_PUSH( m + mapstep - 1 );
334 CANNY_PUSH( m + mapstep );
336 CANNY_PUSH( m + mapstep + 1 );
339 // the final pass, form the final image
340 for( i = 0; i < size.height; i++ )
342 const uchar* _map = map + mapstep*(i+1) + 1;
343 uchar* _dst = dst->data.ptr + dst->step*i;
345 for( j = 0; j < size.width; j++ )
346 _dst[j] = (uchar)-(_map[j] >> 1);
354 cvFree( &stack_bottom );