实验五 图像分割
一、实验目的与要求
1. 了解图像分割的意义和常用方法
2. 掌握常用的图像分割方法
二、实验相关知识
图像分割就是把图像分成互不重叠的区域并提取出感兴趣目标的技术,是由图像处理到图像分析的 关键步骤。
和本实验有关的常用Matlab函数:
edge:检测灰度或二值图像的边缘,返回一个二值图像,1像素是检测到的边缘,0像素是非边缘
用法:BW=edge(I,‘sobel’,thresh,direction); %I为检测对象; 边缘检测算子可用sobel、roberts、prewitt、zerocross、log、canny; thresh指定阈值,检测时忽略所有小于阈值的边缘,默认自动选择阈值; direction指定方向,可选项有horizontal、vertical或both, 在指定的方向上用算子进行边缘检测
三、实验内容
-
分别用Roberts、Prewitt、Sobel三种边缘检测算子,对图像wire.bmp进行水平、垂直及各个方向的边界检测,并将检测结果转化为白底黑线条的方式显示出来;
-
源代码
I = imread('wire.png'); subplot(4,3,2); imshow(I); title('wire'); %Roberts -- horizontal Roberts = edge(I,'roberts','horizontal'); Roberts = oneTozero(Roberts); subplot(4,3,4); imshow(Roberts); title('Roberts-horizontal'); % Roberts -- vertical Roberts = edge(I,'roberts','vertical'); Roberts = oneTozero(Roberts); subplot(4,3,5); imshow(Roberts); title('Roberts-vertical'); %Roberts -- both Roberts = edge(I,'roberts','both'); Roberts = oneTozero(Roberts); subplot(4,3,6); imshow(Roberts); title('Roberts-both'); %Prewitt -- horizontal Prewitt = edge(I,'prewitt','horizontal'); Prewitt = oneTozero(Prewitt); subplot(4,3,7); imshow(Prewitt); title('Prewitt-horizontal'); %Prewitt -- vertical Prewitt = edge(I,'prewitt','vertical'); Prewitt = oneTozero(Prewitt); subplot(4,3,8); imshow(Prewitt); title('Prewitt-vertical'); %Prewitt -- both Prewitt = edge(I,'prewitt','both'); Prewitt = oneTozero(Prewitt); subplot(4,3,9); imshow(Prewitt); title('Prewitt-both'); %Sobel -- horizontal Sobel = edge(I,'sobel','horizontal'); Sobel = oneTozero(Sobel); subplot(4,3,10); imshow(Sobel); title('Sobel-horizontal'); %Sobel -- vertical Sobel = edge(I,'sobel','vertical'); Sobel = oneTozero(Sobel); subplot(4,3,11); imshow(Sobel); title('Sobel-vertical'); %Sobel -- both Sobel = edge(I,'sobel','both'); Sobel = oneTozero(Sobel); subplot(4,3,12); imshow(Sobel); title('Sobel-both');
-
结果截图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ObNco0gJ-1624205253990)(https://i.loli.net/2021/06/14/enLAWaKdrIG7xJQ.png)]
-
-
使用手动阈值分割法对bottle图像进行分割,显示分割结果,并分析其优缺点。
subplot(2,2,); img = imread('bottle.jpg'); imshow(img); title('原图'); subplot(2,2,[3,4]); imhist(img); [m,n] = size(img); thresh = 80; for i = 1:m for j = 1:n if(img(i,j) > thresh) img(i,j) = 255; else img(i,j) = 0; end end end subplot(2,2,2); imshow(img); title('分割图像');
- 手动阈值分割法:利用直方图进行分析,并根据直方图的波峰和波谷之间的关系,找到一个大概的阈值。准确性较高,但是只适用于存在一个目标和一个背景的,且两者对比明显的图像,且直方图必须是双峰的。限制条件较多。
-
对下图A进行处理,得到类似图B的样子.
图A 图B
-
源代码
I = imread('imageA.jpg'); I = rgb2gray(I); hist = imhist(I); threshold = 111; [m,n] = size(I); I1 = zeros(m,n); for i = 1:m for j = 1:n if I(i,j) > threshold I1(i,j) = 1; else I1(i,j) = 0; end end end I1 = edge(I1,'canny'); I1 = oneTozero(I1); imshow(I1);
-
实验截图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IUYeJMC0-1624205253993)(https://i.loli.net/2021/06/14/NjGgJpQObTc6t4W.png)]
end
I1 = edge(I1,‘canny’);
I1 = oneTozero(I1);
imshow(I1);
```
-
实验截图
[外链图片转存中…(img-IUYeJMC0-1624205253993)]