图像分割技术的matlab实现
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
f=rgb2gray(f); % 将彩色图像转换为灰度图像
f=im2double(f); % 转换为双精度,便于后面的计算figure, imshow(f),title('Original Image'),
PF=edge(f,'prewitt'); % 边缘探测,算子为prewitt figure,imshow(PF),title('Prewitt Filter');
RF=edge(f,'roberts'); % 边缘探测,算子为roberts figure,imshow(RF),title('Roberts Filter');
LF=edge(f,'log'); % 边缘探测,算子为log
figure,imshow(LF),title('Laplacian of Gaussian (LoG) Filter');
CF=edge(f,'canny'); % 边缘探测,算子为canny figure,imshow(CF),title('Canny Filter');
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)'); % 显示边缘探测图像
I = imread('circuit.tif');
rotI = imrotate(I,33,'crop'); % 图像旋转,该函数具体用法在本书13.3.3有介绍。fig1 = imshow(rotI); % 显示处理后的图像。
BW = edge(rotI,'canny'); % 图像边界探测
figure, % 新建窗口
imshow(BW); % 显示图像
[H,theta,rho] = hough(BW);
figure, imshow(imadjust(mat2gray(H)),[],'XData',theta,'YData',rho,...% 设置显示属性 'InitialMagnification','fit');
xlabel('\theta (degrees)'), ylabel('\rho'); % 设置坐标轴标题
axis on, axis normal, hold on; % 设置轴属性
colormap(hot) % 设置色彩索引图
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = theta(P(:,2));
y = rho(P(:,1));
plot(x,y,'s','color','black');
f=imread('peppers.png'); % 读入图像
f=rgb2gray(f); % 转换为灰度图像
f=im2double(f); % 数据类型转换
% 全局阈值
T=0.5*(min(f(:))+max(f(:)));
done=false;
while ~done
g=f>=T;
Tn=0.5*(mean(f(g))+mean(f(~g)));
done=abs(T-Tn)<0.1;
T=Tn;
end
display('Threshold(T) - Iterative'); % 显示文字
T
r=im2bw(f,T); % 图像黑白转换
figure,imshow(f),title('Original Image'); % 显示原始图像figure,imshow(r); % 显示处理后的图像
title('Global Thresholding - Iterative Method'); % 图像标题Th=graythresh(f); % 阈值
display('Threshold(T) - Otsu''s Method'); % 显示文字Th
s=im2bw(f,Th); % 图像黑白转换
figure,imshow(s); % 显示处理后的图像
title('Global Thresholding - Otsu''s Method'); % 图像标题se=strel('disk',10);
ft=imtophat(f,se);
Thr=graythresh(ft); % 阈值
display('Threshold(T) - Local Thresholding'); % 显示文字Thr
lt=im2bw(ft,Thr); % 图像黑白转换figure,imshow(lt); % 显示处理后的图像
title('Local Thresholding'); % 图像标题