matlab计算机视觉与图像处理3

三、图像分割
1.阈值分割

clear all;%边缘
%使用sobel检测器来提取边缘
f=rgb2gray (imread("1.png"));
subplot(2,3,1),imshow(f),title('原图像')
[gt,t]=edge(f,'sobel','vertical');
%使用edge函数对图像f提取垂直边缘检测
subplot(2,3,2),imshow(gt),title('垂直sobel掩模后图像')
gv=edge(f,'sobel',0.15,'vertical');
%國值T=0.15的sobel提取垂直边缘检测
subplot(2,3,3),imshow(gv),title('指定阈值后图像')
gboth=edge(f,'sobel',0.15);%默认both的國值T=0.15的边缘检测
subplot(2,3,4),imshow(gboth),title('指定國值水平边缘和垂直边缘后图像')
W45=[-2 -1 0;-1 0 1;0 1 2];%指定掩模(45°)
g45=imfilter(double(f),W45,'replicate');%指定掩模的replicate的淲波器
T=0.3*max(abs(g45(:)));
%指定阈值
g45=g45>=T;
%查找大于工的值
subplot(2,3,5),imshow(g45),title('45° 边缘后图像')
W45=[0 1 2;-1 0 1;-2 -1 0];
%指定掩模(-45°)
g45=imfilter(double(f),W45,'replicate');%指定掩模的replicate的淲波器
T=0.3*max(abs(g45(:)));
%指定阈值
g45=g45>=T;
%査找大于T的値
subplot(2,3,6),imshow(g45),title('-45° 边缘后图像')

matlab计算机视觉与图像处理3_第1张图片
2.区域分割

clc,clear;
img = imread("橙子.png");
imshow(img);
I = im2double(img);
r=I(:,:,1);
g=I(:,:,2);
b=I(:,:,3);
sob1=fspecial('sobel');
sob2=sob1';
Rx = imfilter(r, sob1,'replicate');
Ry = imfilter(r, sob2,'replicate');
Gx = imfilter(g, sob1,'replicate');
Gy = imfilter(g, sob2,'replicate');
Bx = imfilter(b, sob1,'replicate');
By = imfilter(b, sob2,'replicate');
gxx=Rx.^2+Gx.^2+Bx.^2;
gxy=Ry.^2+Gy.^2+By.^2;
gyy=Rx.*Ry+Gx.*Gy+Bx.*By;
theta1=1/2*atan(2*gxy./(gxx-gyy+eps));
F_theta1=(0.5*((gxx+gyy)+(gxx-gyy).*cos(2*theta1)+2*gxy.*sin(2*theta1)));
F_theta1=F_theta1.^0.5;
theta2=theta1+pi/2;
F_theta2=(0.5*((gxx+gyy)+(gxx-gyy).*cos(2*theta2)+2*gxy.*sin(2*theta2)));
F_theta2=F_theta2.^0.5;
I1=max(F_theta1,F_theta2);
I1=abs(I1);
I1 = mat2gray(I1);
imshow(I1)
title('区域');


3.分水岭算法

f=imread("1.png"); %阈值
f=rgb2gray (f); 
subplot (2, 2, 1);
imshow(f); 
title( '(a)原始图像');
subplot (2, 2, 2) ;
f=double (f) ; 
hv=fspecial ('prewitt') ;
hh=hv.'; 
gv=abs (imfilter (f, hv, 'replicate') ) ;
gh=abs (imfilter (f, hh, 'replicate' ) ) ;
g=sqrt (gv.^2+gh.^2) ;
subplot (2,2, 2) ;
L=watershed (g) ; 
wr=L==0;
imshow (wr) ; 
title('(b)分水岭'); 
f(wr)=255; 
subplot (2, 2, 3) ;
imshow (uint8 (f) ) ; 
title('(c)分割结果');
rm=imregionalmin (g) ;
subplot (2, 2, 4) ; 
imshow(rm);
title('(d)局部极小值');

matlab计算机视觉与图像处理3_第2张图片

你可能感兴趣的:(matlab,图像处理,计算机视觉)