【数字图像处理】基于Matlab的图像基本操作

2022/4/24 23:12:46

本文主要是介绍【数字图像处理】基于Matlab的图像基本操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.找到一副具有多形状特征的图像;

2.选取合适的阈值,得到二值化图像;

%% 基础实验部分
Img=imread('Test_bw.jpg');%多形状特征的图像
Img_Gray=rgb2gray(Img);
figure;imhist(Img_Gray);
level=125/255;%选取合适阈值(波谷)
%level=graythresh(Img_Gray);%使用 Otsu 方法计算全局图像阈值
I=im2bw(Img,level);%#ok<IM2BW> %得到二值化图像(im2bw是指导书中要求,建议使用imbinarize)
figure,subplot(221.5),imshow(Img),title('原图');
subplot(223),imshow(Img_Gray),title('灰度图');
subplot(224),imshow(I),title('二值化');

图片来自网络,侵删

3.分别设置不同的结构元素(圆形,方形,两种线形),大小根据自己的图像进行设置;

%% 创建结构元素
Strel_Disk=strel('disk',10);%结构元素为半径10的圆
Strel_Rectangle=strel('rectangle',[3 4]);%结构元素为大小为[3 4]的矩形
Strel_Square=strel('square',5);%结构元素为边长5的方形
Strel_Line=strel('line',10,45);%结构元素为长10角度45的线

4.对得到的二值图像,利用不同的结构元素进行腐蚀运算,并分析得到结果;

%% 腐蚀运算
Erode_Disk=imerode(I,Strel_Disk);
Erode_Rectangle=imerode(I,Strel_Rectangle);
Erode_Square=imerode(I,Strel_Square);
Erode_Line=imerode(I,Strel_Line);
figure,subplot(231),imshow(Img),title('原图');
subplot(232),imshow(I),title('二值化图');
subplot(233),imshow(Erode_Disk),title('圆型腐蚀');
subplot(234),imshow(Erode_Rectangle),title('矩形腐蚀');
subplot(235),imshow(Erode_Square),title('方形腐蚀');
subplot(236),imshow(Erode_Line),title('线性腐蚀');

5.对得到的二值图像,利用不同的结构元素进行膨胀运算,并分析得到结果;

%% 膨胀运算
Dilate_Disk=imdilate(I,Strel_Disk);
Dilate_Rectangle=imdilate(I,Strel_Rectangle);
Dilate_Square=imdilate(I,Strel_Square);
Dilare_Line=imdilate(I,Strel_Line);
figure,subplot(231),imshow(Img),title('原图');
subplot(232),imshow(I),title('二值化图');
subplot(233),imshow(Dilate_Disk),title('圆型膨胀');
subplot(234),imshow(Dilate_Rectangle),title('矩形膨胀');
subplot(235),imshow(Dilate_Square),title('方形膨胀');
subplot(236),imshow(Dilare_Line),title('线性膨胀');

6.对得到的二值图像,利用不同的结构元素进行开运算,并分析得到结果;

%% 开运算
Open_Disk=imopen(I,Strel_Disk);
Open_Rectangle=imopen(I,Strel_Rectangle);
Open_Square=imopen(I,Strel_Square);
Open_Line=imopen(I,Strel_Line);
figure,subplot(231),imshow(Img),title('原图');
subplot(232),imshow(I),title('二值化图');
subplot(233),imshow(Open_Disk),title('圆型开运算');
subplot(234),imshow(Open_Rectangle),title('矩形开运算');
subplot(235),imshow(Open_Square),title('方形开运算');
subplot(236),imshow(Open_Line),title('线性开运算');

7.对得到的二值图像,利用不同的结构元素进行闭运算,并分析得到结果;

%% 闭运算
Close_Disk=imclose(I,Strel_Disk);
Close_Rectangle=imclose(I,Strel_Rectangle);
Close_Square=imclose(I,Strel_Square);
Close_Line=imclose(I,Strel_Line);
figure,subplot(231),imshow(Img),title('原图');
subplot(232),imshow(I),title('二值化图');
subplot(233),imshow(Close_Disk),title('圆型闭运算');
subplot(234),imshow(Close_Rectangle),title('矩形闭运算');
subplot(235),imshow(Close_Square),title('方形闭运算');
subplot(236),imshow(Close_Line),title('线性闭运算');

8.每幅图像均需有含义明确的图题。



这篇关于【数字图像处理】基于Matlab的图像基本操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程