matlab 4种梯度算子(原创)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

clear all;

close all;

P=input('input picture data:');

subplot(231);imshow(P);

title('原图像');

d=size(P);

if(d(3)>1)

P=rgb2gray(P);

end

subplot(232);imshow(P);

title('灰度图像');

P=double(P);

% Roberts Operator

tou=input('input threshold tou=');

bh=input('input a constant intensity nearer to white bh=');

bl=input('input a constant intensity nearer to black bl=');

P1=P;

for i=1:d(1)-1

for j=1:d(2)-1

RobNum=abs(P(i+1,j+1)-P(i,j))+abs(P(i,j+1)-P(i+1,j));

if RobNum>=tou

P1(i,j)=bh;

else

P1(i,j)=bl;

end

end

end

P1=uint8(P1);

subplot(233);imshow(P1);

title('Roberts Operator');

%Prewitt Operator

P2=P;

for i=2:d(1)-1

for j=2:d(2)-1

PreNum=1/6*(abs(P(i-1,j-1)+P(i-1,j)+P(i-1,j+1)-P(i+1,j-1)-P(i+1,j)-P(i+1,j+1))+abs(P(i-1,j-1)+P(i ,j-1)+P(i+1,j-1)-P(i-1,j+1)-P(i,j+1)-P(i+1,j+1)));

if PreNum>=tou

P2(i,j)=bh;

else

P2(i,j)=bl;

end

end

end

P2=uint8(P2);

subplot(234);imshow(P2);

title('Prewitt Operator');

%Sobel Operator

P3=P;

for i=2:d(1)-1

for j=2:d(2)-1

SobNum=1/8*sqrt((P(i-1,j-1)+2*P(i-1,j)+P(i-1,j+1)-P(i+1,j-1)-2*P(i+1,j)-P(i+1,j+1)).^2+(P(i-1,j-1)+2*P(i,j-1)+P(i+1,j-1)-P(i-1,j+1)-2*P(i,j+1)-P(i+1,j+1)).^2);

if SobNum>=tou

P3(i,j)=bh;

else

P3(i,j)=bl;

end

end

end

P3=uint8(P3);

subplot(235);imshow(P3);

title('Sobel Operator');

%Laplacian Operator

P4=P;

for i=2:d(1)-1

for j=2:d(2)-1

P4(i,j)=P(i+1,j)-2*P(i,j)+P(i-1,j)+P(i,j+1)-2*P(i,j)+P(i,j-1);

end

end

P4=uint8(P4);

subplot(236);imshow(P4);

title('Laplacian Operator');

相关文档
最新文档