学习DIP第44天
转载请标明本文出处:http://blog.csdn.net/tonyshengtan,欢迎大家转载,发现博客被某些论坛转载后,图像无法正常显示,无法正常表达本人观点,对此表示很不满意。有些网站转载了我的博文,很开心的是自己写的东西被更多人看到了,但不开心的是这段话被去掉了,也没标明转载来源,虽然这并没有版权保护,但感觉还是不太好,出于尊重文章作者的劳动,转载请标明出处!!!!
文章代码已托管,欢迎共同开发:https://github.com/Tony-Tan/DIPpro
用两个小模板分别卷积的另一个好处是减少计算量,对于使用大小为nxn的模板,卷积计算量为O(n*n*width*height)而分开成小模板卷积计算量是O(2*n*width*height)也就是O(n*width*height)减少了一项,当n相对较大的时候,计算量明显减少。
帕斯卡三角的计算是通过组合公式给出,具体不在这里描述,所以Sobel算子的模板计算方法我们就有了大概的了解。
opencv文档中给出了关于sobel算子的下面信息:
和上面描述的方法类似,更直观,可以用来理解sobel的模板结构,不同的差分方向带来的问题就是边缘方向的确定,由于算子属于一阶微分,也就是梯度算子之一,所以梯度方向信息也显得很重要,比如后面要说的canny就是用到了梯度方向的信息,所以,在确定方向时要注意算子的差分方向。
对于阶梯型边缘,计算过程及结果如下,红色为模板中心:
可以看到,相比于Robert算子,Sobel得到的边界候选位置相对较宽,而且包括全部的内边界和外边界。并且差分被放大了,也就是说,用Sobel算子处理后的图片有可能超过原图像灰度级别,对于这个问题,处理方法是将平滑分算子(分解后的平滑部分,例如【1,2,1】)归一化,得到的差值仍在原始灰度级范围内。
double Sobel(double *src,double *dst,double *edgedriction,int width,int height,int sobel_size){
//double SobelMask_x[3]={-1,-2,-1,0,0,0,1,2,1};
double *dst_x=(double *)malloc(sizeof(double)*width*height);
double *dst_y=(double *)malloc(sizeof(double)*width*height);
if(sobel_size==3){
double SobelMask1[3]={0.25,0.5,0.25};
double SobelMask2[3]={1,0,-1};
RealConvolution(src, dst_x, SobelMask1, width, height, 1, 3);
RealConvolution(dst_x, dst_x, SobelMask2, width, height, 3, 1);
RealConvolution(src, dst_y, SobelMask2, width, height, 1, 3);
RealConvolution(dst_y, dst_y, SobelMask1, width, height, 3, 1);
}else if(sobel_size==5){
double SobelMask1[5]={0.0625,0.25,0.375,0.25,0.0625};
double SobelMask2[5]={1/3.0,2/3.0,0,-2/3.0,-1/3.0};
RealConvolution(src, dst_x, SobelMask1, width, height, 1, 5);
RealConvolution(dst_x, dst_x, SobelMask2, width, height, 5, 1);
RealConvolution(src, dst_y, SobelMask2, width, height, 1, 5);
RealConvolution(dst_y, dst_y, SobelMask1, width, height, 5, 1);
}else if(sobel_size==7){
double SobelMask1[7]={0.015625,0.09375,0.234375,0.3125,0.234375,0.09375,0.015625};
double SobelMask2[7]={0.1,0.4,0.5,0,-0.5,-0.4,-0.1};
RealConvolution(src, dst_x, SobelMask1, width, height, 1, 7);
RealConvolution(dst_x, dst_x, SobelMask2, width, height, 7, 1);
RealConvolution(src, dst_y, SobelMask2, width, height, 1, 7);
RealConvolution(dst_y, dst_y, SobelMask1, width, height, 7, 1);
}
if(edgedriction!=NULL)
//getEdgeDirection(dst_x, dst_y, edgedriction, width, height);
getEdgeAngle(dst_x, dst_y, edgedriction, width, height);
for(int j=0;j<height;j++)
for(int i=0;i<width;i++){
dst[j*width+i]=abs(dst_x[j*width+i])+abs(dst_y[j*width+i]);
}
free(dst_x);
free(dst_y);
return findMatrixMax(dst,width,height);
}
原文地址:http://blog.csdn.net/tonyshengtan/article/details/43698711