找回密码
 立即注册
查看: 487|回复: 0

MATLAB图像分割和形态学处理

[复制链接]
发表于 2021-12-13 14:33 | 显示全部楼层 |阅读模式
MATLAB图像分割和形态学处理

一、实验目的

  • 掌握图像分割的基本方法。
  • 掌握形态学处理的基本方法。
  • 学会使用MATLAB编程实现上述方法。
二、实验任务
(1)编程实现基于阈值的图像分割方法和边缘检测方法。
(2)编程实现膨胀、腐蚀方法。
(3)编程实现开运算和闭运算的方法。
(4)编程实现提取骨架和细化的方法。
三、实验配套的主要仪器设备及台(套)数
教师示范用投影仪一台
微型计算机每个学生一台
四、报告要求       
记录每一步的实验过程。
五、实验记录
5.1阈值图像分割方法1——点检测
5.1.1程序
clc
clear
data = imread('lianzipoint.jpg');
w = [-1 -1 -1; -1 8 -1; -1 -1 -1]
g =abs(imfilter(double(data),w));
t =max(g(:));
g1=(g>=t);
[m n]=find(g1)
figure
imshow(data)
hold on
plot(n,m,'ro')
g1=(g>=t-200);
[m n]=find(g1)
figure
imshow(data)
hold on
plot(n,m,'ro')
g1=(g>=t-800);
[m n]=find(g1)
figure
imshow(data)
hold on
plot(n,m,'ro')
5.1.2 效果







分析:随着阈值的减小,所检测出的点越来越多
5.2 阈值分割方法2——线检测
5.2.1程序
clc
clear
data = imread('xian.jpg');
subplot(221),imshow(data);
title('检测指定方向线的原始图像');
w = [2 -1 -1; -1 2 -1; -1 -1 2];
g =abs(imfilter(double(data),w));
subplot(222),imshow(g,[])
title('使用-45度检测器处理后的图像');
gtop = g(1:40,1:40);
gtop = pixeldup(gtop,4);%piceldup函数是将图片放大相应倍数
subplot(223),imshow(gtop,[])
title('-45度检测后左上角放大图');
gbot = g(end-40:end,end-40:end);
gbot = pixeldup(gbot,4);
subplot(224),imshow(gbot,[])
title('-45度检测后右下角后放大图');

5.2.2 处理效果



分析:-45度方向上的直线,经过处理后效果明显,其他方向上的线比较模糊。
5.3 阈值分割方法3——边缘检测
5.3.1程序
clc
clear
f=imread('bianyuan.jpg');
f= rgb2gray(f);
subplot(321),imshow(f);
title('sobel检测的原始图像');
[gv,t]=edge(f,'sobel','vertical');
%斜线因为具有垂直分量,所以也能够被检测出来
subplot(322),imshow(gv);
title('sobel垂直方向检测后图像');
gv=edge(f,'sobel',0.15,'vertical');
subplot(323),imshow(gv);
title('sobel垂直检测0.15阈值后图像');
gboth=edge(f,'sobel',0.15);
subplot(324),imshow(gboth);
title('sobel水平垂直方向阈值0.15后图像');
w45=[-2 -1  0 86      -1  0  1 87      0   1  2];
g45=imfilter(double(f),w45,'replicate');
T=0.3*max(abs(g45(:)));
g45=g45>=T;
subplot(325),imshow(g45);
title('sobel正45度方向上检测图');
w_45=[0 -1 -2 95        1  0 -1 96        2  1 0];
g_45=imfilter(double(f),w_45,'replicate');
T=0.3*max(abs(g_45(:)));
g_45=g_45>=T;
subplot(326),imshow(g_45);
title('sobel负45度方向上检测图');
5.3.2 效果



5.3.2 边缘检测器的比较
5.3.2.1 程序
clc
clear
f=imread('dalouj.jpg');
imshow(f)
[g_sobel_default , ts] = edge(f,'sobel');
imshow(g_sobel_default);
title('g sobel default');
[g_log_default,tlog]=edge(f,'log');
figure
imshow(g_log_default);
title('g log default');
[g_canny_default,tc]=edge(f,'canny');
figure,imshow(g_canny_default);
title('g canny default');
g_sobel_best=edge(f,'sobel',0.25);
figure,imshow(g_sobel_best);
title('g sobel best');
g_log_best=edge(f,'log',0.003,2.25);
figure,imshow(g_log_best);
title('g log best');
g_canny_best=edge(f,'canny',[0.04 0.10],1.5);
figure,imshow(g_canny_best);
title('g canny best');
5.3.2.1 效果













5.2 编程实现膨胀和腐蚀
5.2.1 膨胀
5.2.1.1程序
clc  
clear
A1=imread('beitie.jpg');
A1 =im2bw(A1);
B=[1 1 1 1
   1 1 1 1
   1 1 1 1
   1 1 1 1];
A2=imdilate(A1,B);%图像A1被结构元素B膨胀
A3=imdilate(A2,B);
A4=imdilate(A3,B);
figure,imshow(A1);
title('imdilate膨胀原始图像');
figure,imshow(A2);
title('使用B后1次膨胀后的图像');
figure,imshow(A3);
title('使用B后2次膨胀后的图像');
figure,imshow(A4);
title('使用B后3次膨胀后的图像');

   5.2.1.2 效果









5.2.2 腐蚀
5.2.2.1 程序
clc
clear
A1=imread('xian.jpg');
A1 =im2bw(A1);
figure,imshow(A1);
title('原始图像');
se1=strel('disk',2);
A2=imerode(A1,se1);
figure,imshow(A2);
title('用disk(2)腐蚀后的图像');
se2=strel('disk',4);
A3=imerode(A1,se2);
figure,imshow(A3);
title('用disk(4)腐蚀后的图像');
se3=strel('disk',6);
A4=imerode(A1,se3);
figure,imshow(A4);
title('用disk(6)腐蚀后的图像');
5.2.2.2  效果



5.3编程实现开运算和闭运算
5.3.1开运算
5.3.1.1程序
clc
clear
f=imread('beitie.jpg');
f =im2bw(f);
%se=strel('square',5');%方型结构元素
se=strel('disk',5');%圆盘型结构元素
imshow(f);%原图像
title('运算原始图像')
fo=imopen(f,se);
figure
imshow(fo);
title('直接开运算');
5.3.1.2 效果





5.3.2 闭运算
5.3.2.1 程序
clc
clear
f=imread('beitie.jpg');
f =im2bw(f);
figure
imshow(f);%原图像
     title('原始图像');

se=strel('disk',5');%圆盘型结构元素
fc=imclose(f,se);%直接闭运算
figure,imshow(fc);
title('直接闭运算');
foc=imclose(fc,se);%先开后闭运算
figure,imshow(foc);
title('先开后闭运算');
fco=imopen(fc,se);%先闭后开运算
figure,imshow(fco);
title('先闭后开运算');
5.3.2.2 效果








5.4 编程实现提取骨架和细化
5.4.1 提取骨架
5.4.1.1 程序
clc
clear
f=imread('guge.jpg');
f =im2bw(f);
figure,imshow(f);
title('骨架提取原图');
g1=bwmorph(f,'skel',1);
figure,imshow(g1);
title('骨架提取1次');
g2=bwmorph(f,'skel',5);
figure,imshow(g2);
title('骨架提取5次');
g3=bwmorph(f,'skel',20);
figure,imshow(g3);
title('骨架提取20次');
g4=bwmorph(f,'skel',Inf);
figure,imshow(g4);
title('骨架提取无穷大次');
for k=1:5
     g3=g3&~endpoints(g3);
end
figure,imshow(g3);
title('骨架提取并消除毛刺');
5.4.1.2效果













5.4.2 细化
5.4.2.1 程序
f=imread('zhiwen.jpg');
f =im2bw(f);
f=~f;
figure,imshow(f);
title('指纹细化原图');
g1=bwmorph(f,'thin',1);
figure,imshow(g1);
title('指纹细化1次');
g2=bwmorph(f,'thin',5);
figure,imshow(g2);
title('指纹细化5次');
g3=bwmorph(f,'thin',Inf);
figure,imshow(g3);
title('指纹细化无穷大次');
5.4.2.2 效果      







本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2025-5-14 22:07 , Processed in 0.135894 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表