当前位置:首页 > 代码 > 正文

sobel算子源代码(sobel算子经典例题)

admin 发布:2022-12-19 21:29 176


今天给各位分享sobel算子源代码的知识,其中也会对sobel算子经典例题进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求matlab图像边缘检测的三种算子的源代码

edit edge看edge.m的源代码阿

然后把这三个的提取出来,改改不就行了

matlab有现成的函数edge

比如roberts梯度

I=imread('你的图片');

[g,t]=edge(I,'roberts',0.1);

sobel算子

[g,t]=edge(I,'sobel',0.1);

laplacian算子

[g,t]=edge(I,'log',0.1);

在C或C++环境下,分别利用sobel算子,robert算子,编程输出边缘图像。选错课了..完全不懂 求高手代码..

俺就给你写个sobel的,你把sobel模板换成robert模板就OK了。

本来sobel找阈值还有个小算法,不过一般不要求的,俺就用黄金分割点乘以255替代了。

sobel卷积代码如下:

void CSobelDlg::CreateSobolImage(void)

{

static const int sizeOfSobelMask = 9;

static int sobelMaskHor[sizeOfSobelMask] =

{

-1, -2, -1,

 0,  0,  0,

 1,  2,  1

};

static int SobelMaskVer[sizeOfSobelMask] =

{

1, 0, -1,

2, 0, -2,

1, 0, 1

};

int numOfBytes = m_bmpInfo.bmWidthBytes * m_bmpInfo.bmHeight;

unsigned char* pbuf1 = new unsigned char[numOfBytes];

unsigned char* pbuf2 = new unsigned char[numOfBytes];

m_bmpOrg.GetBitmapBits(numOfBytes, pbuf1);

unsigned char averageColor = 0;

for(int row = 0; row  m_bmpInfo.bmHeight; ++ row)

{

for(int col = 0; col  m_bmpInfo.bmWidth; ++col)

{

averageColor = ( pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] +

pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 1] +

pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 2] ) / 3;

pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] = averageColor;

}

}

unsigned char ts = 0, tv = 0, tmp = 0, dst = 0, idx = 0;

for(int row = 1; row  m_bmpInfo.bmHeight - 1; ++ row)

{

for(int col = 1; col  m_bmpInfo.bmWidth - 1; ++col)

{

idx = ts = tv = 0;

for(int r = row - 1; r = row + 1; ++r)

{

for(int c = col - 1; c = col + 1; ++c)

{

tmp = pbuf1[r * m_bmpInfo.bmWidthBytes + c * m_bmpInfo.bmBitsPixel / 8];

ts += (sobelMaskHor[idx] * tmp );

tv += (SobelMaskVer[idx] * tmp );

++idx;

}

}

dst = (unsigned char)sqrt( (float)(ts * ts + tv * tv) );

if(dst  (unsigned char)(0.6180339887 * 255.0) ) {

pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] = 0;

pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 1] = 255;

pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 2] = 0;

pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 3] = 255;

}

else {

pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] = 0;

pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 1] = 0;

pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 2] = 255;

pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 3] = 255;

}

}

}

m_bmpSobol.CreateBitmap(m_bmpInfo.bmWidth, m_bmpInfo.bmHeight, 1, 32, pbuf2);

delete []pbuf1;

delete []pbuf2;

}

再给你一张本程序运行的效果图。

哪位大哥给个 图像边缘检测 C++程序代码

将常用的检测实现公式列出如下:

Roberts算子:G[i,i]=|f[i,j]-f[i+1,j+1]|+|f[i+1,j]-f[i,j+1]|;

Sobe算子:G[i,i]=|f[i-1,j+1]+2f[i,j+1]+f[i+1,j+1]-f[i-1,j-1]-2f[i,j-1]-f[i+1,j-1]|

+|f[i-1,j-1]+2f[i-1,j]+f[i-1,j+1]-f[i+1,j-1]-2f[i+1,j]-f[i+1,j+1]|;

拉普拉斯算子:G[I,j]=|f[i+1,j]+f[i-1,j]+f(i,j+1)+f[i,j-1]-4f[i,j]|;

其中G[i,j]表示处理后(i,j)点的灰度值,f[i,j]表示处理前该点的灰度值。

不过用robert算法实现的没有找到具体的源代码,下面有一个Sobe算子实现的:

在视图类中定义的响应菜单命令的边缘检测Sobel算子实现灰度图像边缘检测的函数:

void CDibView::OnMENUSobel()

//灰度图像数据的获得参见天极网9.10日发表的拙作//VC数字图像处理一文

{

HANDLE data1handle;

LPBITMAPINFOHEADER lpBi;

CDibDoc *pDoc=GetDocument();

HDIB hdib;

unsigned char *hData;

unsigned char *data;

hdib=pDoc-m_hDIB;

BeginWaitCursor();

lpBi=(LPBITMAPINFOHEADER)GlobalLock((HGLOBAL)hdib);

hData= lpbi +* (LPDWORD)lpbi + 256*sizeof(RGBQUAD);

//得到指向位图像素值的指针

pDoc-SetModifiedFlag(TRUE);//设修改标志为"TRUE"

data1handle=GlobalAlloc(GMEM_SHARE,WIDTHBYTES(lpBi-biWidth*8)*lpBi-biHeight);

//申请存放处理后的像素值的缓冲区

data=(unsigned char*)GlobalLock((HGLOBAL)data1handle);

AfxGetApp()-BeginWaitCursor();

int i,j,buf,buf1,buf2;

for( j=0; jbiHeight; j++)//以下循环求(x,y)位置的灰度值

for( i=0; ibiWidth; i++)

{

if(((i-1)=0)((i+1)biWidth)((j-1)=0)((j+1)biHeight))

{//对于图像四周边界处的向素点不处理

buf1=(int)*(hData+(i+1)*WIDTHBYTES(lpBi-biWidth*8)+(j-1))

+2*(int)*(hData+(i+1)*WIDTHBYTES(lpBi-biWidth*8)+(j))

+(int)(int)*(hData+(i+1)*WIDTHBYTES(lpBi-biWidth*8)+(j+1));

buf1=buf1-(int)(int)*(hData+(i-1)*WIDTHBYTES(lpBi-biWidth*8)+(j-1))

-2*(int)(int)*(hData+(i-1)*WIDTHBYTES(lpBi-biWidth*8)+(j))

-(int)(int)*(hData+(i-1)*WIDTHBYTES(lpBi-biWidth*8)+(j+1));

//x方向加权微分

buf2=(int)(int)*(hData+(i-1)*WIDTHBYTES(lpBi-biWidth*8)+(j+1))

+2*(int)(int)*(hData+(i)*WIDTHBYTES(lpBi-biWidth*8)+(j+1))

+(int)(int)*(hData+(i+1)*WIDTHBYTES(lpBi-biWidth*8)+(j+1));

buf2=buf2-(int)(int)*(hData+(i-1)*WIDTHBYTES(lpBi-biWidth*8)+(j-1))

-2*(int)(int)*(hData+(i)*WIDTHBYTES(lpBi-biWidth*8)+(j-1))

-(int)(int)*(hData+(i+1)*WIDTHBYTES(lpBi-biWidth*8)+(j-1));

//y方向加权微分

buf=abs(buf1)+abs(buf2);//求梯度

if(buf255) buf=255;

if(buf0)

else *(data+i*lpBi-biWidth+j)=(BYTE)0;

}

for( j=0; jbiHeight; j++)

for( i=0; ibiWidth; i++)

*(hData+i*WIDTHBYTES(lpBi-biWidth*8)+j)=*(data+i*WIDTHBYTES(lpBi-biWidth*8)+j);

//处理后的数据写回原缓冲区

AfxGetApp()-EndWaitCursor();

GlobalUnlock((HGLOBAL)hdib);

GlobalUnlock(data1handle);

GlobalFree(date1handle);

EndWaitCursor();

Invalidate(TRUE);

}

具体的分析给你一个链接吧:

Sobel算子的C代码

/* Sobel templatea00 a01 a02a10 a11 a12a20 a21 a22*/unsigned char a00, a01, a02;unsigned char a10, a11, a12;unsigned char a20, a21, a22;void MySobel(IplImage* gray, IplImage* gradient){CvScalar color ;for (int i=1; igray-height-1; ++i){for (int j=1; jgray-width-1; ++j){a00 = cvGet2D(gray, i-1, j-1).val[0];a01 = cvGet2D(gray, i-1, j).val[0];a02 = cvGet2D(gray, i-1, j+1).val[0];a10 = cvGet2D(gray, i, j-1).val[0];a11 = cvGet2D(gray, i, j).val[0];a12 = cvGet2D(gray, i, j+1).val[0];a20 = cvGet2D(gray, i+1, j-1).val[0];a21 = cvGet2D(gray, i+1, j).val[0];a22 = cvGet2D(gray, i+1, j+1).val[0];// x方向上的近似导数double ux = a20 * (1) + a21 * (2) + a22 * (1)+ (a00 * (-1) + a01 * (-2) + a02 * (-1));// y方向上的近似导数double uy = a02 * (1) + a12 * (2) + a22 * (1)+ a00 * (-1) + a10 * (-2) + a20 * (-1);color.val[0] = sqrt(ux*ux + uy*uy);cvSet2D(gradient, i, j, color);}}}//注释:该程序需要在安装Opencv软件下运行。Matlabps=imread('D:\14.jpg'); %读取图像subplot(1,3,1)imshow(ps);title('原图像');ps=rgb2gray(ps);[m,n]=size(ps); %用Sobel微分算子进行边缘检测pa = edge(ps,'sobel');subplot(1,3,2);imshow(pa);title('Sobel边缘检测得到的图像');

求soble算子和prewitt算子源代码,用C语言编的!用于数字图像处理!

自己以前图像处理的时候写的,用的是C++, 不过处理流程一样的,可以参考一下

//Soble

void CBmp::RhSobel()

{

double temp[9];

DWORD m_Y=m_pInfo-bmiHeader.biHeight;

DWORD m_X=WIDTH((m_pInfo-bmiHeader.biWidth)*(m_pInfo-bmiHeader.biBitCount));

BYTE *m_B=(BYTE *) new char[m_Y*m_X];

for(int d=0;dm_nPixels;d++)

{

m_B[d]=m_pPixels[d];

}

if((m_pInfo-bmiHeader.biBitCount)==24)

for(int i=1;im_Y-1;i++)

for(int j=3;j(m_X-2);j+=3)

{

for(int n=0;n9;n+=3)

{

temp[n]=(m_B[(i-1+n/3)*m_X+j-3]+m_B[(i-1+n/3)*m_X+j-2]+m_B[(i-1+n/3)*m_X+j-1])/3;

temp[n+1]=(m_B[(i-1+n/3)*m_X+j]+m_B[(i-1+n/3)*m_X+j+1]+m_B[(i-1+n/3)*m_X+j+2])/3;

temp[n+2]=(m_B[(i-1+n/3)*m_X+j+3]+m_B[(i-1+n/3)*m_X+j+4]+m_B[(i-1+n/3)*m_X+j+5])/3;

}

m_pPixels[i*m_X+j]=m_pPixels[i*m_X+j+1]=m_pPixels[i*m_X+j+2]=//

(BYTE((abs(temp[2]+2*temp[5]+temp[8]-//

temp[0]-2*temp[3]-temp[6])+

abs(temp[0]+2*temp[1]+temp[2]-//

temp[6]-2*temp[7]-temp[8]))));

}

else

for(int i=1;i(m_Y-1);i++)

{

for(int j=1;j(m_X-1);j++)

{

m_pPixels[i*m_X+j]=(abs(m_B[(i-1)*m_X+j+1]+(2*m_B[(i)*m_X+j+1])+m_B[(i+1)*m_X+j+1]-//

m_B[(i-1)*m_X+j-1]-(2*m_B[(i)*m_X+j-1])-m_B[(i+1)*m_X+j-1])+

abs(m_B[(i-1)*m_X+j-1]+(2*m_B[(i-1)*m_X+j])+m_B[(i-1)*m_X+j+1]-//

m_B[(i+1)*m_X+j-1]-(2*m_B[(i+1)*m_X+j])-m_B[(i+1)*m_X+j+1]));

}

}

}

//Prewitt

void CBmp::ByPrewitt()

{

double temp1,temp2;

DWORD m_Y=m_pInfo-bmiHeader.biHeight;

DWORD m_X=WIDTH((m_pInfo-bmiHeader.biWidth)*(m_pInfo-bmiHeader.biBitCount));

BYTE *m_B=(BYTE *) new char[m_Y*m_X];

for(int d=0;dm_nPixels;d++)

{

m_B[d]=m_pPixels[d];

}

if(m_pInfo-bmiHeader.biBitCount==8)

for(int i=1;i(m_Y-1);i++)

{

for(int j=1;j(m_X-1);j++)

{

temp1=abs(m_B[(i-1)*m_X+j+1]-m_B[(i-1)*m_X+j-1]+m_B[i*m_X+j+1]-//

m_B[i*m_X+j-1]+m_B[(i+1)*m_X+j+1]-m_B[(i+1)*m_X+j-1]);

temp2=abs(m_B[(i-1)*m_X+j-1]+m_B[(i-1)*m_X+j]+m_B[(i-1)*m_X+j+1]-//

m_B[(i+1)*m_X+j-1]-m_B[(i+1)*m_X+j]-m_B[(i+1)*m_X+j+1]);

m_pPixels[i*m_X+j]=(temp1temp2?temp1:temp2);

}

}

else

{

Huidu();

for(int i=1;i(m_Y-1);i++)

{

for(int j=3;j(m_X-5);j+=3)

{

temp1=abs(m_B[(i-1)*m_X+j+3]-m_B[(i-1)*m_X+j-3]+m_B[i*m_X+j+3]-//

m_B[i*m_X+j-3]+m_B[(i+1)*m_X+j+3]-m_B[(i+1)*m_X+j-3]);

temp2=abs(m_B[(i-1)*m_X+j-3]+m_B[(i-1)*m_X+j]+m_B[(i-1)*m_X+j+3]-//

m_B[(i+1)*m_X+j-3]-m_B[(i+1)*m_X+j]-m_B[(i+1)*m_X+j+3]);

m_pPixels[i*m_X+j]=m_pPixels[i*m_X+j+1]=m_pPixels[i*m_X+j+2]=(temp1temp2?temp1:temp2);

}

}

}

}

急求 sobel算子检测边缘 Matlab程序代码

f=imread('peppers.png'); % 读入图像

f=rgb2gray(f); % 灰度转换

f=im2double(f); % 数据类型转换

% 使用垂直Sobel算子,自动选择阈值

[VSFAT Threshold]=edge(f,'sobel','vertical'); % 边缘探测

figure, imshow(f),title('Original Image'), % 显示原始图像

figure,imshow(VSFAT),title('Sobel Filter - Automatic Threshold'); % 显示边缘探测图像

%使用水平和垂直Sobel算子,自动选择阈值

SFST=edge(f,'sobel',Threshold);

figure,imshow(SFST),title('Sobel Filter (Horizontal and Vertical)'); % 显示边缘探测图像

%使用指定45度角Sobel算子滤波器,指定阈值

s45=[-2 -1 0;-1 0 1;0 1 2];

SFST45=imfilter(f,s45,'replicate');

SFST45=SFST45=Threshold;

figure,imshow(SFST45),title('Sobel Filter (45 Degree)'); % 显示边缘探测图像

%使用指定-45度角Sobel算子滤波器,指定阈值

sm45=[0 1 2;-1 0 1;-2 -1 0];

SFSTM45=imfilter(f,sm45,'replicate');

SFSTM45=SFSTM45=Threshold;

figure,imshow(SFSTM45),title('Sobel Filter (-45 Degree)'); % 显示边缘探测图像

关于sobel算子源代码和sobel算子经典例题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/25108.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载