2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
void cv::Canny( InputArray _src, OutputArray _dst,
|
|
|
|
double low_thresh, double high_thresh,
|
|
|
|
int aperture_size, bool L2gradient )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
CV_Assert( src.depth() == CV_8U );
|
|
|
|
|
|
|
|
_dst.create(src.size(), CV_8U);
|
|
|
|
Mat dst = _dst.getMat();
|
2011-11-11 22:45:53 +08:00
|
|
|
#ifdef HAVE_TEGRA_OPTIMIZATION
|
2012-02-16 05:10:11 +08:00
|
|
|
if (tegra::canny(src, dst, low_thresh, high_thresh, aperture_size, L2gradient))
|
2011-11-11 22:45:53 +08:00
|
|
|
return;
|
|
|
|
#endif
|
2012-02-16 05:10:11 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( low_thresh > high_thresh )
|
2012-02-16 05:10:11 +08:00
|
|
|
std::swap(low_thresh, high_thresh);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
if( (aperture_size & 1) == 0 || (aperture_size != -1 && (aperture_size < 3 || aperture_size > 7)) )
|
2010-05-12 01:44:00 +08:00
|
|
|
CV_Error( CV_StsBadFlag, "" );
|
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
Mat dx, dy;
|
|
|
|
Sobel(src, dx, CV_16S, 1, 0, aperture_size, 1, 0, BORDER_REFLECT_101);
|
|
|
|
Sobel(src, dy, CV_16S, 0, 1, aperture_size, 1, 0, BORDER_REFLECT_101);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
int low, high;
|
|
|
|
if( L2gradient )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
Cv32suf ul, uh;
|
|
|
|
ul.f = (float)low_thresh;
|
|
|
|
uh.f = (float)high_thresh;
|
|
|
|
|
|
|
|
low = ul.i;
|
|
|
|
high = uh.i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
low = cvFloor( low_thresh );
|
|
|
|
high = cvFloor( high_thresh );
|
|
|
|
}
|
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
Size size = src.size();
|
|
|
|
int i, j, k, mstep = size.width + 2, cn = src.channels();
|
|
|
|
|
|
|
|
Mat mask(size.height + 2, mstep, CV_8U);
|
|
|
|
memset( mask.ptr<uchar>(0), 1, mstep );
|
|
|
|
memset( mask.ptr<uchar>(size.height+1), 1, mstep );
|
|
|
|
|
|
|
|
Mat mag(6+cn, mstep, CV_32S);
|
|
|
|
mag = Scalar::all(0);
|
|
|
|
int* mag_buf[3] = { mag.ptr<int>(0), mag.ptr<int>(1), mag.ptr<int>(2) };
|
|
|
|
short* dxybuf[3] = { (short*)mag.ptr<int>(3), (short*)mag.ptr<int>(4), (short*)mag.ptr<int>(5) };
|
|
|
|
int* mbuf = mag.ptr<int>(6);
|
|
|
|
|
|
|
|
int maxsize = MAX( 1 << 10, size.width*size.height/10 );
|
|
|
|
std::vector<uchar*> stack( maxsize );
|
|
|
|
uchar **stack_top, **stack_bottom;
|
2010-05-12 01:44:00 +08:00
|
|
|
stack_top = stack_bottom = &stack[0];
|
|
|
|
|
|
|
|
/* sector numbers
|
|
|
|
(Top-Left Origin)
|
|
|
|
|
|
|
|
1 2 3
|
|
|
|
* * *
|
|
|
|
* * *
|
|
|
|
0*******0
|
|
|
|
* * *
|
|
|
|
* * *
|
|
|
|
3 2 1
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CANNY_PUSH(d) *(d) = (uchar)2, *stack_top++ = (d)
|
|
|
|
#define CANNY_POP(d) (d) = *--stack_top
|
|
|
|
|
|
|
|
// calculate magnitude and angle of gradient, perform non-maxima supression.
|
|
|
|
// fill the map with one of the following values:
|
|
|
|
// 0 - the pixel might belong to an edge
|
|
|
|
// 1 - the pixel can not belong to an edge
|
|
|
|
// 2 - the pixel does belong to an edge
|
|
|
|
for( i = 0; i <= size.height; i++ )
|
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
int *_mag = mag_buf[(i > 0) + 1] + 1;
|
2010-05-12 01:44:00 +08:00
|
|
|
float* _magf = (float*)_mag;
|
2012-02-16 05:10:11 +08:00
|
|
|
const short *_dx, *_dy;
|
|
|
|
short *_ddx, *_ddy;
|
2010-05-12 01:44:00 +08:00
|
|
|
uchar* _map;
|
|
|
|
int x, y;
|
2010-11-16 04:53:33 +08:00
|
|
|
ptrdiff_t magstep1, magstep2;
|
2010-05-12 01:44:00 +08:00
|
|
|
int prev_flag = 0;
|
|
|
|
|
|
|
|
if( i < size.height )
|
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
_dx = dx.ptr<short>(i);
|
|
|
|
_dy = dy.ptr<short>(i);
|
|
|
|
_ddx = dxybuf[(i > 0) + 1];
|
|
|
|
_ddy = _ddx + size.width;
|
|
|
|
|
|
|
|
if( cn > 1 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
_mag = mbuf;
|
|
|
|
_magf = (float*)_mag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !L2gradient )
|
|
|
|
for( j = 0; j < size.width*cn; j++ )
|
|
|
|
_mag[j] = std::abs(_dx[j]) + std::abs(_dy[j]);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( j = 0; j < size.width*cn; j++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
x = _dx[j]; y = _dy[j];
|
2012-02-16 05:10:11 +08:00
|
|
|
_magf[j] = sqrtf((float)x*x + (float)y*y);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-02-16 05:10:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if( cn > 1 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
_mag = mag_buf[(i > 0) + 1] + 1;
|
2010-05-12 01:44:00 +08:00
|
|
|
for( j = 0; j < size.width; j++ )
|
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
_mag[j] = mbuf[(j+1)*cn];
|
|
|
|
_ddx[j] = _dx[j*cn]; _ddy[j] = _dy[j*cn];
|
|
|
|
}
|
|
|
|
|
|
|
|
for( k = 1; k < cn; k++ )
|
|
|
|
{
|
|
|
|
for( j = 0; j < size.width; j++ )
|
|
|
|
if( mbuf[(j+1)*cn + k] > _mag[j] )
|
|
|
|
{
|
|
|
|
_mag[j] = mbuf[(j+1)*cn + k];
|
|
|
|
_ddx[j] = _dx[j*cn + k];
|
|
|
|
_ddy[j] = _dy[j*cn + k];
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-16 05:10:11 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for( j = 0; j < size.width; j++ )
|
|
|
|
_ddx[j] = _dx[j]; _ddy[j] = _dy[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
_mag[-1] = _mag[size.width] = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
2012-02-16 05:10:11 +08:00
|
|
|
memset( _mag-1, 0, (size.width + 2)*sizeof(_mag[0]) );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// at the very beginning we do not have a complete ring
|
|
|
|
// buffer of 3 magnitude rows for non-maxima suppression
|
|
|
|
if( i == 0 )
|
|
|
|
continue;
|
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
_map = &mask.at<uchar>(i, 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
_map[-1] = _map[size.width] = 1;
|
|
|
|
|
|
|
|
_mag = mag_buf[1] + 1; // take the central row
|
2012-02-16 05:10:11 +08:00
|
|
|
_dx = dxybuf[1];
|
|
|
|
_dy = _dx + size.width;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-16 04:53:33 +08:00
|
|
|
magstep1 = mag_buf[2] - mag_buf[1];
|
|
|
|
magstep2 = mag_buf[0] - mag_buf[1];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( (stack_top - stack_bottom) + size.width > maxsize )
|
|
|
|
{
|
|
|
|
int sz = (int)(stack_top - stack_bottom);
|
2012-02-16 05:10:11 +08:00
|
|
|
maxsize = MAX( maxsize * 3/2, maxsize + size.width );
|
2010-05-12 01:44:00 +08:00
|
|
|
stack.resize(maxsize);
|
|
|
|
stack_bottom = &stack[0];
|
|
|
|
stack_top = stack_bottom + sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( j = 0; j < size.width; j++ )
|
|
|
|
{
|
|
|
|
#define CANNY_SHIFT 15
|
|
|
|
#define TG22 (int)(0.4142135623730950488016887242097*(1<<CANNY_SHIFT) + 0.5)
|
|
|
|
|
|
|
|
x = _dx[j];
|
|
|
|
y = _dy[j];
|
|
|
|
int s = x ^ y;
|
|
|
|
int m = _mag[j];
|
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
x = std::abs(x);
|
|
|
|
y = std::abs(y);
|
2010-05-12 01:44:00 +08:00
|
|
|
if( m > low )
|
|
|
|
{
|
|
|
|
int tg22x = x * TG22;
|
|
|
|
int tg67x = tg22x + ((x + x) << CANNY_SHIFT);
|
|
|
|
|
|
|
|
y <<= CANNY_SHIFT;
|
|
|
|
|
|
|
|
if( y < tg22x )
|
|
|
|
{
|
|
|
|
if( m > _mag[j-1] && m >= _mag[j+1] )
|
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
if( m > high && !prev_flag && _map[j-mstep] != 2 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
CANNY_PUSH( _map + j );
|
|
|
|
prev_flag = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_map[j] = (uchar)0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( y > tg67x )
|
|
|
|
{
|
|
|
|
if( m > _mag[j+magstep2] && m >= _mag[j+magstep1] )
|
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
if( m > high && !prev_flag && _map[j-mstep] != 2 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
CANNY_PUSH( _map + j );
|
|
|
|
prev_flag = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_map[j] = (uchar)0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s = s < 0 ? -1 : 1;
|
|
|
|
if( m > _mag[j+magstep2-s] && m > _mag[j+magstep1+s] )
|
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
if( m > high && !prev_flag && _map[j-mstep] != 2 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
CANNY_PUSH( _map + j );
|
|
|
|
prev_flag = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_map[j] = (uchar)0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev_flag = 0;
|
|
|
|
_map[j] = (uchar)1;
|
|
|
|
}
|
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
// scroll the ring buffers
|
2010-05-12 01:44:00 +08:00
|
|
|
_mag = mag_buf[0];
|
|
|
|
mag_buf[0] = mag_buf[1];
|
|
|
|
mag_buf[1] = mag_buf[2];
|
|
|
|
mag_buf[2] = _mag;
|
2012-02-16 05:10:11 +08:00
|
|
|
|
|
|
|
_ddx = dxybuf[0];
|
|
|
|
dxybuf[0] = dxybuf[1];
|
|
|
|
dxybuf[1] = dxybuf[2];
|
|
|
|
dxybuf[2] = _ddx;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// now track the edges (hysteresis thresholding)
|
|
|
|
while( stack_top > stack_bottom )
|
|
|
|
{
|
|
|
|
uchar* m;
|
|
|
|
if( (stack_top - stack_bottom) + 8 > maxsize )
|
|
|
|
{
|
|
|
|
int sz = (int)(stack_top - stack_bottom);
|
|
|
|
maxsize = MAX( maxsize * 3/2, maxsize + 8 );
|
|
|
|
stack.resize(maxsize);
|
|
|
|
stack_bottom = &stack[0];
|
|
|
|
stack_top = stack_bottom + sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
CANNY_POP(m);
|
|
|
|
|
|
|
|
if( !m[-1] )
|
|
|
|
CANNY_PUSH( m - 1 );
|
|
|
|
if( !m[1] )
|
|
|
|
CANNY_PUSH( m + 1 );
|
2012-02-16 05:10:11 +08:00
|
|
|
if( !m[-mstep-1] )
|
|
|
|
CANNY_PUSH( m - mstep - 1 );
|
|
|
|
if( !m[-mstep] )
|
|
|
|
CANNY_PUSH( m - mstep );
|
|
|
|
if( !m[-mstep+1] )
|
|
|
|
CANNY_PUSH( m - mstep + 1 );
|
|
|
|
if( !m[mstep-1] )
|
|
|
|
CANNY_PUSH( m + mstep - 1 );
|
|
|
|
if( !m[mstep] )
|
|
|
|
CANNY_PUSH( m + mstep );
|
|
|
|
if( !m[mstep+1] )
|
|
|
|
CANNY_PUSH( m + mstep + 1 );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// the final pass, form the final image
|
|
|
|
for( i = 0; i < size.height; i++ )
|
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
const uchar* _map = mask.ptr<uchar>(i+1) + 1;
|
|
|
|
uchar* _dst = dst.ptr<uchar>(i);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
for( j = 0; j < size.width; j++ )
|
|
|
|
_dst[j] = (uchar)-(_map[j] >> 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-16 05:10:11 +08:00
|
|
|
void cvCanny( const CvArr* image, CvArr* edges, double threshold1,
|
|
|
|
double threshold2, int aperture_size )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-02-16 05:10:11 +08:00
|
|
|
cv::Mat src = cv::cvarrToMat(image), dst = cv::cvarrToMat(edges);
|
|
|
|
CV_Assert( src.size == dst.size && src.depth() == CV_8U && dst.type() == CV_8U );
|
|
|
|
|
|
|
|
cv::Canny(src, dst, threshold1, threshold2, aperture_size & 255,
|
|
|
|
(aperture_size & CV_CANNY_L2_GRADIENT) != 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file. */
|