clc
clear all
close all
A = imread('tig.jpg') %读入图像
imshow(A)title('原图')
y_mask = [-1 -1 -10 0 01 1 1] %建立Y方向的模板
x_mask = y_mask' %建立X方向的模板
I = im2double(A) %将图像数据转化为双精度
dx = imfilter(I, x_mask) %计算X方向的梯度分量
dy = imfilter(I, y_mask) %计算Y方向的梯度分量
grad = sqrt(dx.*dx + dy.*dy) %计算梯度
grad = mat2gray(grad) %将梯度矩阵转换为灰度图像
level = graythresh(grad) %计算灰度阈值
BW = im2bw(grad,level) %用阈值分割梯度图像
figure, imshow(BW) %显示分割后的图像即边缘图像
title('Prewitt')
自己以前图像处理的时候写的,用的是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=0d<m_nPixelsd++)
{
m_B[d]=m_pPixels[d]
}
if((m_pInfo->bmiHeader.biBitCount)==24)
for(int i=1i<m_Y-1i++)
for(int j=3j<(m_X-2)j+=3)
{
for(int n=0n<9n+=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=1i<(m_Y-1)i++)
{
for(int j=1j<(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=0d<m_nPixelsd++)
{
m_B[d]=m_pPixels[d]
}
if(m_pInfo->bmiHeader.biBitCount==8)
for(int i=1i<(m_Y-1)i++)
{
for(int j=1j<(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]=(temp1>temp2?temp1:temp2)
}
}
else
{
Huidu()
for(int i=1i<(m_Y-1)i++)
{
for(int j=3j<(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]=(temp1>temp2?temp1:temp2)
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)