经典的边缘检测方法是对原始图像中像素的某小邻域来构造边缘检测算子。常用的边缘检测方法有Roberts算子、Sobe l算子、Prew itt算子、K irsch算子、Laplacian算子、LOG 算子、C anny算子等。
一、基于一阶微分的边缘检测方法
1、最简单的梯度算子是Roberts算子, 它是一个2 * 2模板, 利用局部差分检测比较陡峭的边缘, 但对于噪声较敏感, 经常会出现孤立点 。
//roberts算子求图像梯度提取边缘,输入源图像,输出梯度图,此方法不常用
void roberts(IplImage *src,IplImage *dst)
{
//为roberts图像申请空间,因为要利用源图像指针中的imageData,因此使用复制方式
dst=cvCloneImage(src);
int x,y,i,w,h;
int temp,temp1;
uchar* ptr=(uchar*) (dst->imageData );
int ptr1[4]={0};
int indexx[4]={0,1,1,0};
int indexy[4]={0,0,1,1};
w=dst->width;
h=dst->height;
for(y=0;y<h-1;y++)
for(x=0;x<w-1;x++)
{
for(i=0;i<4;i++) //取每个2*2矩阵元素的指针 0 | 1
{ // 3 | 2
ptr1[i]= *(ptr+(y+indexy[i])*dst->widthStep+x+indexx[i]);
}
temp=abs(ptr1[0]-ptr1[2]); //计算2*2矩阵中0和2位置的差,取绝对值temp
temp1=abs(ptr1[1]-ptr1[3]); //计算2*2矩阵中1和3位置的差,取绝对值temp1
temp=(temp>temp1?temp:temp1); //若temp1>temp,则以temp1的值替换temp
temp= (int)sqrt(float(temp*temp)+float(temp1*temp1)); //输出值
/* if (temp>100)
temp=255;
else temp=0; */
*(ptr+y*dst->widthStep+x)=temp; //将输出值存放于dst像素的对应位置
}
double min_val = 0,max_val = 0;//取图并显示像中的最大最小像素值
cvMinMaxLoc