sobel算子实现边缘检测及其c++实现及与matlab效果对比

来源:互联网 发布:java编程规范 华为 编辑:程序博客网 时间:2024/05/22 07:52

这里增加了对边缘像素的补齐。sobel梯度分割抗噪性好,但是无法做到自动阈值,是其一大遗憾,matlab却解决的很好。


//默认对8位位图进行处理void Sobel(unsigned char *pIn, int width, int height, unsigned char *pOut){//每行像素所占字节数,输出图像与输入图像相同int lineByte=(width+3)/4*4;//申请输出图像缓冲区pOut=new unsigned char[lineByte*height];//循环变量,图像的坐标int i,j;//中间变量int x, y, t;//Sobel算子for(i=1;i<height-1;i++){for(j=1;j<width-1;j++){//x方向梯度x= *(pIn+(i-1)*lineByte+j+1) + 2 * *(pIn+i*lineByte+j+1) + *(pIn+(i+1)*lineByte+j+1) - *(pIn+(i-1)*lineByte+j-1) - 2 * *(pIn+i*lineByte+j-1) - *(pIn+(i+1)*lineByte+j-1);//y方向梯度y= *(pIn+(i-1)*lineByte+j-1)+ 2 * *(pIn+(i-1)*lineByte+j)+ *(pIn+(i-1)*lineByte+j+1)- *(pIn+(i+1)*lineByte+j-1)- 2 * *(pIn+(i+1)*lineByte+j)- *(pIn+(i+1)*lineByte+j+1);t=abs(x)+abs(y)+0.5;if (t>100){*(pOut+i*lineByte+j)=255;}else{*(pOut+i*lineByte+j)=0;}}}for(j=0;j<width;j++){*(pOut+(height-1)*lineByte+j)=0;//补齐最后一行*(pOut+j)=0;//补齐第一行} for(i=0;i<height;i++) {*(pOut+i*lineByte)=0;//补齐第一列*(pOut+i*lineByte+width-1)=0;//补齐最后一列} }}

image=imread('C:\\Users\\Liu\\\Desktop\\lenna.bmp');Info=imfinfo('C:\\Users\\Liu\\\Desktop\\lenna.bmp');  %读图像信息,并判断是否是灰度图if Info.BitDepth>8image=rgb2gray(image);endBW=edge(image,'sobel');imshow(BW)

甚至对比opencv,matlab的效果也略胜一筹,接下来希望深入matlab底层,用c++实现matlab的sobel算子。


0 0