canny.cpp
Go to the documentation of this file.
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 <opencv2/imgproc.hpp>
43 #include <opencv2/core.hpp>
44 
45 namespace tuw {
46 CV_IMPL void tuwCanny( const void* srcarr, void* dstarr, void* gradientarr, void* directionarr, void* dxarr, void* dyarr,
47  double low_thresh, double high_thresh,
48  int aperture_size )
49 {
50  cv::AutoBuffer<char> buffer;
51  std::vector<uchar*> stack;
52  uchar **stack_top = 0, **stack_bottom = 0;
53 
54  CvMat srcstub, *src = cvGetMat( srcarr, &srcstub );
55  CvMat dststub, *dst = cvGetMat( dstarr, &dststub );
56  CvMat dstgrad, *gra = cvGetMat( gradientarr, &dstgrad ); // Added by Markus Bader
57  CvMat dstdir, *dir = cvGetMat( directionarr, &dstdir ); // Added by Markus Bader
58  CvMat *dx = cvGetMat( dxarr, &dstgrad ); // Added by Markus Bader
59  CvMat *dy = cvGetMat( dyarr, &dstdir ); // Added by Markus Bader
60  CvSize size;
61  int flags = aperture_size;
62  int low, high;
63  int* mag_buf[3];
64  uchar* map;
65  int mapstep, maxsize;
66  int i, j;
67  CvMat mag_row;
68 
69  if( CV_MAT_TYPE( gra->type ) != CV_16UC1 || // Added by Markus Bader
70  CV_MAT_TYPE( dir->type ) != CV_16UC1 ) // Added by Markus Bader
71  CV_Error( CV_StsUnsupportedFormat, "gradientarr or directionarr are not CV_16UC1" ); // Added by Markus Bader
72  if( CV_MAT_TYPE( dx->type ) != CV_16SC1 || // Added by Markus Bader
73  CV_MAT_TYPE( dy->type ) != CV_16SC1 ) // Added by Markus Bader
74  CV_Error( CV_StsUnsupportedFormat, "sobel_dx or sobel_dy are not CV_16SC1" ); // Added by Markus Bader
75  if( CV_MAT_TYPE( src->type ) != CV_8UC1 ||
76  CV_MAT_TYPE( dst->type ) != CV_8UC1 )
77  CV_Error( CV_StsUnsupportedFormat, "" );
78 
79  if( !CV_ARE_SIZES_EQ( src, dst ))
80  CV_Error( CV_StsUnmatchedSizes, "" );
81 
82  if( low_thresh > high_thresh )
83  {
84  double t;
85  CV_SWAP( low_thresh, high_thresh, t );
86  }
87 
88  aperture_size &= INT_MAX;
89  if( (aperture_size & 1) == 0 || aperture_size < 3 || aperture_size > 7 )
90  CV_Error( CV_StsBadFlag, "" );
91 
92  size.width = src->cols;
93  size.height = src->rows;
94 
95  // dx = cvCreateMat( size.height, size.width, CV_16SC1 ); // Removed by Markus Bader
96  // dy = cvCreateMat( size.height, size.width, CV_16SC1 ); // Removed by Markus Bader
97  cvSobel( src, dx, 1, 0, aperture_size );
98  cvSobel( src, dy, 0, 1, aperture_size );
99 
100  /*if( icvCannyGetSize_p && icvCanny_16s8u_C1R_p && !(flags & CV_CANNY_L2_GRADIENT) )
101  {
102  int buf_size= 0;
103  IPPI_CALL( icvCannyGetSize_p( size, &buf_size ));
104  CV_CALL( buffer = cvAlloc( buf_size ));
105  IPPI_CALL( icvCanny_16s8u_C1R_p( (short*)dx->data.ptr, dx->step,
106  (short*)dy->data.ptr, dy->step,
107  dst->data.ptr, dst->step,
108  size, (float)low_thresh,
109  (float)high_thresh, buffer ));
110  EXIT;
111  }*/
112 
113  if( flags & CV_CANNY_L2_GRADIENT )
114  {
115  Cv32suf ul, uh;
116  ul.f = (float)low_thresh;
117  uh.f = (float)high_thresh;
118 
119  low = ul.i;
120  high = uh.i;
121  }
122  else
123  {
124  low = cvFloor( low_thresh );
125  high = cvFloor( high_thresh );
126  }
127 
128  buffer.allocate( (size.width+2)*(size.height+2) + (size.width+2)*3*sizeof(int) );
129 
130  mag_buf[0] = (int*)(char*)buffer;
131  mag_buf[1] = mag_buf[0] + size.width + 2;
132  mag_buf[2] = mag_buf[1] + size.width + 2;
133  map = (uchar*)(mag_buf[2] + size.width + 2);
134  mapstep = size.width + 2;
135 
136  maxsize = MAX( 1 << 10, size.width*size.height/10 );
137  stack.resize( maxsize );
138  stack_top = stack_bottom = &stack[0];
139 
140  memset( mag_buf[0], 0, (size.width+2)*sizeof(int) );
141  memset( map, 1, mapstep );
142  memset( map + mapstep*(size.height + 1), 1, mapstep );
143 
144  /* sector numbers
145  (Top-Left Origin)
146 
147  1 2 3
148  * * *
149  * * *
150  0*******0
151  * * *
152  * * *
153  3 2 1
154  */
155 
156  #define CANNY_PUSH(d) *(d) = (uchar)2, *stack_top++ = (d)
157  #define CANNY_POP(d) (d) = *--stack_top
158 
159  mag_row = cvMat( 1, size.width, CV_32F );
160 
161  // calculate magnitude and angle of gradient, perform non-maxima supression.
162  // fill the map with one of the following values:
163  // 0 - the pixel might belong to an edge
164  // 1 - the pixel can not belong to an edge
165  // 2 - the pixel does belong to an edge
166  for( i = 0; i <= size.height; i++ )
167  {
168  int* _mag = mag_buf[(i > 0) + 1] + 1;
169  float* _magf = (float*)_mag;
170  const short* _dx = (short*)(dx->data.ptr + dx->step*i);
171  const short* _dy = (short*)(dy->data.ptr + dy->step*i);
172  uchar* _map;
173  int x, y;
174  int magstep1, magstep2;
175  int prev_flag = 0;
176 
177  if( i < size.height )
178  {
179  _mag[-1] = _mag[size.width] = 0;
180 
181  if( !(flags & CV_CANNY_L2_GRADIENT) )
182  for( j = 0; j < size.width; j++ )
183  _mag[j] = abs(_dx[j]) + abs(_dy[j]);
184  /*else if( icvFilterSobelVert_8u16s_C1R_p != 0 ) // check for IPP
185  {
186  // use vectorized sqrt
187  mag_row.data.fl = _magf;
188  for( j = 0; j < size.width; j++ )
189  {
190  x = _dx[j]; y = _dy[j];
191  _magf[j] = (float)((double)x*x + (double)y*y);
192  }
193  cvPow( &mag_row, &mag_row, 0.5 );
194  }*/
195  else
196  {
197  for( j = 0; j < size.width; j++ )
198  {
199  x = _dx[j]; y = _dy[j];
200  _magf[j] = (float)std::sqrt((double)x*x + (double)y*y);
201  }
202  }
203  }
204  else
205  memset( _mag-1, 0, (size.width + 2)*sizeof(int) );
206 
207  // at the very beginning we do not have a complete ring
208  // buffer of 3 magnitude rows for non-maxima suppression
209  if( i == 0 )
210  continue;
211 
212  _map = map + mapstep*i + 1;
213  _map[-1] = _map[size.width] = 1;
214 
215  _mag = mag_buf[1] + 1; // take the central row
216  _dx = (short*)(dx->data.ptr + dx->step*(i-1));
217  _dy = (short*)(dy->data.ptr + dy->step*(i-1));
218 
219  magstep1 = (int)(mag_buf[2] - mag_buf[1]);
220  magstep2 = (int)(mag_buf[0] - mag_buf[1]);
221 
222  if( (stack_top - stack_bottom) + size.width > maxsize )
223  {
224  int sz = (int)(stack_top - stack_bottom);
225  maxsize = MAX( maxsize * 3/2, maxsize + 8 );
226  stack.resize(maxsize);
227  stack_bottom = &stack[0];
228  stack_top = stack_bottom + sz;
229  }
230 
231  for( j = 0; j < size.width; j++ )
232  {
233  #define CANNY_SHIFT 15
234  #define TG22 (int)(0.4142135623730950488016887242097*(1<<CANNY_SHIFT) + 0.5)
235 
236  x = _dx[j];
237  y = _dy[j];
238  int s = x ^ y;
239  int m = _mag[j];
240 
241  x = abs(x);
242  y = abs(y);
243  if( m > low )
244  {
245  int tg22x = x * TG22;
246  int tg67x = tg22x + ((x + x) << CANNY_SHIFT);
247 
248  y <<= CANNY_SHIFT;
249 
250  if( y < tg22x )
251  {
252  if( m > _mag[j-1] && m >= _mag[j+1] )
253  {
254  if( m > high && !prev_flag && _map[j-mapstep] != 2 )
255  {
256  CANNY_PUSH( _map + j );
257  prev_flag = 1;
258  }
259  else
260  _map[j] = (uchar)0;
261  continue;
262  }
263  }
264  else if( y > tg67x )
265  {
266  if( m > _mag[j+magstep2] && m >= _mag[j+magstep1] )
267  {
268  if( m > high && !prev_flag && _map[j-mapstep] != 2 )
269  {
270  CANNY_PUSH( _map + j );
271  prev_flag = 1;
272  }
273  else
274  _map[j] = (uchar)0;
275  continue;
276  }
277  }
278  else
279  {
280  s = s < 0 ? -1 : 1;
281  if( m > _mag[j+magstep2-s] && m > _mag[j+magstep1+s] )
282  {
283  if( m > high && !prev_flag && _map[j-mapstep] != 2 )
284  {
285  CANNY_PUSH( _map + j );
286  prev_flag = 1;
287  }
288  else
289  _map[j] = (uchar)0;
290  continue;
291  }
292  }
293  }
294  prev_flag = 0;
295  _map[j] = (uchar)1;
296  }
297 
298  // scroll the ring buffer
299  _mag = mag_buf[0];
300  mag_buf[0] = mag_buf[1];
301  mag_buf[1] = mag_buf[2];
302  mag_buf[2] = _mag;
303  }
304 
305  // now track the edges (hysteresis thresholding)
306  while( stack_top > stack_bottom )
307  {
308  uchar* m;
309  if( (stack_top - stack_bottom) + 8 > maxsize )
310  {
311  int sz = (int)(stack_top - stack_bottom);
312  maxsize = MAX( maxsize * 3/2, maxsize + 8 );
313  stack.resize(maxsize);
314  stack_bottom = &stack[0];
315  stack_top = stack_bottom + sz;
316  }
317 
318  CANNY_POP(m);
319 
320  if( !m[-1] )
321  CANNY_PUSH( m - 1 );
322  if( !m[1] )
323  CANNY_PUSH( m + 1 );
324  if( !m[-mapstep-1] )
325  CANNY_PUSH( m - mapstep - 1 );
326  if( !m[-mapstep] )
327  CANNY_PUSH( m - mapstep );
328  if( !m[-mapstep+1] )
329  CANNY_PUSH( m - mapstep + 1 );
330  if( !m[mapstep-1] )
331  CANNY_PUSH( m + mapstep - 1 );
332  if( !m[mapstep] )
333  CANNY_PUSH( m + mapstep );
334  if( !m[mapstep+1] )
335  CANNY_PUSH( m + mapstep + 1 );
336  }
337 
338  // the final pass, form the final image
339  for( i = 0; i < size.height; i++ )
340  {
341  const uchar* _map = map + mapstep*(i+1) + 1;
342  uchar* _dst = dst->data.ptr + dst->step*i;
343 
344 
345  unsigned short* _dir = (unsigned short*)(dir->data.ptr + dir->step*i); // Added by Markus Bader
346  unsigned short* _gra = (unsigned short*)(gra->data.ptr + gra->step*i); // Added by Markus Bader
347  const short* _dx = (short*)(dx->data.ptr + dx->step*i); // Added by Markus Bader
348  const short* _dy = (short*)(dy->data.ptr + dy->step*i); // Added by Markus Bader
349 
350  for( j = 0; j < size.width; j++ ){
351  _dst[j] = (uchar)-(_map[j] >> 1);
352  if(_dst[j]) { // Added by Markus Bader
353  _dir[j] = (unsigned short) cvFastArctan(_dy[j], _dx[j]); // Added by Markus Bader
354  _gra[j] = cvSqrt(_dx[j]*_dx[j] + _dy[j]*_dy[j]); // Added by Markus Bader
355  } else { // Added by Markus Bader
356  _dir[j] = 0; // Added by Markus Bader
357  _gra[j] = 0; // Added by Markus Bader
358  } // Added by Markus Bader
359  }
360  }
361 }
362 
363 void Canny( const cv::Mat& image, cv::Mat& edges, cv::Mat& gradient, cv::Mat& direction, cv::Mat& sobel_dx, cv::Mat& sobel_dy,
364  double threshold1, double threshold2,
365  int apertureSize, bool L2gradient )
366 {
367  cv::Mat src = image;
368  edges.create(src.size(), CV_8U);
369  gradient.create(src.size(), CV_16UC1);
370  direction.create(src.size(), CV_16UC1);
371  sobel_dx.create(src.size(), CV_16SC1);
372  sobel_dy.create(src.size(), CV_16SC1);
373  CvMat _src = src, _dst = edges, _gradientarr = gradient, _directionarr = direction, dxarr = sobel_dx, dyarr = sobel_dy;
374  tuwCanny( &_src, &_dst, &_gradientarr, &_directionarr, &dxarr, &dyarr, threshold1, threshold2,
375  apertureSize + (L2gradient ? CV_CANNY_L2_GRADIENT : 0));
376 }
377 };
378 
379 /* End of file. */
#define CANNY_SHIFT
void tuwCanny(const void *srcarr, void *edgearr, void *gradientarr, void *directionarr, void *dxarr, void *dyarr, double low_thresh, double threshold2, int aperture_size CV_DEFAULT(3))
XmlRpcServer s
TFSIMD_FORCE_INLINE const tfScalar & y() const
#define TG22
void Canny(const cv::Mat &image, cv::Mat &edges, cv::Mat &gradient, cv::Mat &direction, cv::Mat &sobel_dx, cv::Mat &sobel_dy, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false)
Definition: canny.cpp:363
TFSIMD_FORCE_INLINE const tfScalar & x() const
#define CANNY_POP(d)
#define CANNY_PUSH(d)


tuw_ellipses
Author(s): Markus Bader
autogenerated on Mon Jun 10 2019 15:42:10