/*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 "_cv.h"
icvCannyGetSize_t icvCannyGetSize_p = 0;
icvCanny_16s8u_C1R_t icvCanny_16s8u_C1R_p = 0;
//参数的意义
//const void* srcarr :输入图像
//void* dstarr :输出图像
//double low_thresh :阈值下限
//double high_thresh :阈值上限
//int aperture_size :算子内核的大小
CV_IMPL void
cvCanny( const void* srcarr, void* dstarr,
double low_thresh, double high_thresh, int aperture_size )
{
static const int sec_tab[] = { 1, 3, 0, 0, 2, 2, 2, 2 };
CvMat *dx = 0, *dy = 0;
void *buffer = 0;
uchar **stack_top, **stack_bottom = 0;
CV_FUNCNAME( "cvCanny" );
__BEGIN__;
CvMat srcstub, *src = (CvMat*)srcarr;
CvMat dststub, *dst = (CvMat*)dstarr;
CvSize size;
int flags = aperture_size;
int low, high;
int* mag_buf[3];
uchar* map;
int mapstep, maxsize;
int i, j;
CvMat mag_row;
CV_CALL( src = cvGetMat( src, &srcstub ));
CV_CALL( dst = cvGetMat( dst, &dststub ));
if( CV_MAT_TYPE( src->type ) != CV_8UC1 ||
CV_MAT_TYPE( dst->type ) != CV_8UC1 )
CV_ERROR( CV_StsUnsupportedFormat, "" );
if( !CV_ARE_SIZES_EQ( src, dst ))
CV_ERROR( CV_StsUnmatchedSizes, "" );
if( low_thresh > high_thresh )
{
double t;
CV_SWAP( low_thresh, high_thresh, t );
}
aperture_size &= INT_MAX;
if( (aperture_size & 1) == 0 || aperture_size < 3 || aperture_size > 7 )//必须为3,5,7中的一个?
CV_ERROR( CV_StsBadFlag, "" );
//计算x与y方向的梯度分量
size = cvGetMatSize( src );
dx = cvCreateMat( size.height, size.width, CV_16SC1 );
dy = cvCreateMat( size.height, size.width, CV_16SC1 );
cvSobel( src, dx, 1, 0, aperture_size );
cvSobel( src, dy, 0, 1, aperture_size );
if( icvCannyGetSize_p && icvCanny_16s8u_C1R_p && !(flags & CV_CANNY_L2_GRADIENT) )
{
int buf_size= 0;
IPPI_CALL( icvCannyGetSize_p( size, &buf_size ));
CV_CALL( buffer = cvAlloc( buf_size ));
IPPI_CALL( icvCanny_16s8u_C1R_p( (short*)dx->data.ptr, dx->step,
(short*)dy->data.ptr, dy->step,
dst->data.ptr, dst->step,
size, (float)low_thresh,
(float)high_thresh, buffer ));
EXIT;
}
if( flags & CV_CANNY_L2_GRADIENT )
{
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 );
}
//为
CV_CALL( buffer = cvAlloc( (size.width+2)*(size.height+2) +
(size.width+2)*3*sizeof(int)) );
mag_buf[0] = (int*)buffer;
mag_buf[1] = mag_buf[0] + size.width + 2;
mag_buf[2] = mag_buf[1] + size.width + 2;
map = (uchar*)(mag_buf[2] + size.width + 2);
mapstep = size.width + 2;
maxsize = MAX( 1 << 10, size.width*size.height/10 );
CV_CALL( stack_top = stack_bottom = (uchar**)cvAlloc( maxsize*sizeof(stack_top[0]) ));
memset( mag_buf[0], 0, (size.width+2)*sizeof(int) );
memset( map, 1, mapstep );
memset( map + mapstep*(size.height + 1), 1, mapstep );
/* 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
mag_row = cvMat( 1, size.width, CV_32F );
//计算梯度的大小和角度,进行非极大值抑制
//用下面的值对图像进行填充
// 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++ )
{
int* _mag = mag_buf[(i > 0) + 1] + 1;//i=0时,初始化_mag=mag_buf[1] + 1;i>0时,初始化_mag=mag_buf[2] + 1
float* _magf = (float*)_mag;
const short* _dx = (short*)(dx->data.ptr + dx->step*i);//分梯度的指针
const short* _dy = (short*)(dy->data.ptr + dy->step*i);
uchar* _map; //为边缘点图,记录是否为边缘点的标记0,1,2
int x, y;
int magstep1, magstep2;
int prev_flag = 0;
if( i < size.height )
{
_mag[-1] = _mag[size.width] = 0; //每一行第一个像素点和第size.width+1个像素点处_mag数组值设置为0
//根据情况选择不同的边缘强度计算方法,存储于数组_mag或者_magf中
//数组_mag与_magf地址是一样的
if( !(flags & CV_CANNY_L2_GRADIENT) ) //当设定的核算子的大小为3,5,7时都执行此运算
for( j = 0; j < size.width; j++ )
_mag[j] = abs(_dx[j]) + abs(_dy[j]);
else if( icvFilterSobelVert_8u16s_C1R_p != 0 ) // check for IPP
{
// use vectorized sqrt
mag_row.data.fl = _magf;
for( j = 0; j < size.width; j++ )
{
x = _dx[j]; y = _dy[j];
_magf[j] = (float)((double)x*x + (double)y*y);
}
cvPow( &mag_row, &mag_row, 0.5 );
}
else
{
for( j = 0; j < size.width; j++ )
{
x = _dx[j]; y = _dy[j];
_magf[j] = (float)sqrt((double)x*x + (double)y*y);
}
}
}
else
memset( _mag-1, 0, (size.width + 2)*sizeof(int) );
//一开始没有完整的邻域
// at the very beginning we do not have a complete ring
// buffer of 3 magnitude rows for non-maxima suppression
if( i == 0 )
continue;
_map = map + mapstep*i + 1;
_map[-1] = _map[size.width] = 1;//每一行的首个和倒数第二个都为零
_mag = mag_buf[1] + 1; // take the central row
_dx = (short*)(dx->data.ptr + dx->step*(i-1));
_dy = (short*)(dy->data.ptr + dy->step*(i-1));
magstep1 = (int)(mag_buf[2] - mag_buf[1]);
magstep2 = (int)(mag_buf[0] - mag_buf[1]);
if( (stack_top - stack_bottom) + size.width > maxsize )//如果先前申请的栈空间不够用,则扩大栈空间
{
uchar** new_stack_bottom;
maxsize = MAX( maxsize * 3/2, maxsize + size.width );
CV_CALL( new_stack_bottom = (uchar**)cvAlloc( maxsize * sizeof(stack_top[0])) );
memcpy( new_stack_bottom, stack_bottom, (stack_top - stack_bottom)*sizeof(stack_top[0]) );
stack_top = new_stack_bottom + (stack_top - stack_bottom);
cvFree( &stack_bottom );
stack_bottom = new_stack_bottom;
}
//局部极大值抑制
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];
x = abs(x);
y = abs(y);
if( m > low )
{
int tg22x = x * TG22;
int tg67x = tg22x + ((x + x) << CANNY_SHIFT);
y <<= CANNY_SHIFT;
if( y < tg22x )//dy算子小时,看dx的大小,所以看是否是同行三个的极大值
{
if( m > _mag[j-1] && m >= _mag[j+1] )
{
if( m > high && !prev_flag && _map[j-mapstep] != 2 )
{
CANNY_PUSH( _map + j );//_map[j]=2;确认为边缘点
prev_flag = 1;
}
else
_map[j] = (uchar)0;//不是边缘点
continue;
}
}
else if( y > tg67x )//当dy算子大时,则dy对的贡献大,所以看是否是同列邻域三个的极大值
{
if( m > _mag[j+magstep2] && m >= _mag[j+magstep1] )
{
if( m > high && !prev_flag && _map[j-mapstep] != 2 )
{
CANNY_PUSH( _map + j );
prev_flag = 1;
}
else
_map[j] = (uchar)0;
continue;
}
}
else //dy算子不大不小,比较是否是斜对角三个的极大值
{
s = s < 0 ? -1 : 1;
if( m > _mag[j+magstep2-s] && m > _mag[j+magstep1+s] )
{
if( m > high && !prev_flag && _map[j-mapstep] != 2 )
{
CANNY_PUSH( _map + j );
prev_flag = 1;
}
else
_map[j] = (uchar)0;
continue;
}
}
}
prev_flag = 0;
_map[j] = (uchar)1;//可能为边缘点
}
// scroll the ring buffer
_mag = mag_buf[0];
mag_buf[0] = mag_buf[1];
mag_buf[1] = mag_buf[2];
mag_buf[2] = _mag;
}
// now track the edges (hysteresis thresholding)
//遍历边缘图,根据边缘图上的标注确定最终的边缘点
//边缘点的八邻域内等级为0的点确认为边缘点
while( stack_top > stack_bottom )
{
uchar* m;
if( (stack_top - stack_bottom) + 8 > maxsize )//如果栈空间不够用,扩展栈空间
{
uchar** new_stack_bottom;
maxsize = MAX( maxsize * 3/2, maxsize + 8 );
CV_CALL( new_stack_bottom = (uchar**)cvAlloc( maxsize * sizeof(stack_top[0])) );
memcpy( new_stack_bottom, stack_bottom, (stack_top - stack_bottom)*sizeof(stack_top[0]) );
stack_top = new_stack_bottom + (stack_top - stack_bottom);
cvFree( &stack_bottom );
stack_bottom = new_stack_bottom;
}
CANNY_POP(m);//栈中边缘点(标记为2)的地址赋给m (自顶向下)
//遍历八邻域的点,等级为0的点均确认为边缘点
if( !m[-1] )//左边的点等级为0(可能的边缘点),则确认其为边缘点
CANNY_PUSH( m - 1 );
if( !m[1] ) //同上
CANNY_PUSH( m + 1 );
if( !m[-mapstep-1] )
CANNY_PUSH( m - mapstep - 1 );
if( !m[-mapstep] )
CANNY_PUSH( m - mapstep );
if( !m[-mapstep+1] )
CANNY_PUSH( m - mapstep + 1 );
if( !m[mapstep-1] )
CANNY_PUSH( m + mapstep - 1 );
if( !m[mapstep] )
CANNY_PUSH( m + mapstep );
if( !m[mapstep+1] )
CANNY_PUSH( m + mapstep + 1 );
}
// the final pass, form the final image
//将结果输出到输出文件中
for( i = 0; i < size.height; i++ )
{
const uchar* _map = map + mapstep*(i+1) + 1;
uchar* _dst = dst->data.ptr + dst->step*i;
for( j = 0; j < size.width; j++ )
_dst[j] = (uchar)-(_map[j] >> 1);
}
__END__;
cvReleaseMat( &dx );
cvReleaseMat( &dy );
cvFree( &buffer );
cvFree( &stack_bottom );
}
/* End of file. */
OpenCV中Canny边缘检测源程序的注释
最新推荐文章于 2024-04-10 22:31:22 发布