实验四图像分割与边缘检测
一.实验目的及要求
1.利用MATLAB研究图像分割与边缘检测的常用算法原理;
2.掌握MATLAB图像域值分割与边缘检测函数的使用方法;
3.了解边缘检测的算法和用途,比较Sobel、Prewitt、Canny等算子边缘检测的差异。
二、实验内容
(一)研究以下程序,分析程序功能;输入执行各命令行,认真观察命令执行的结果。
熟悉程序中所使用函数的调用方法,改变有关参数,观察试验结果。
1.图像阈值分割
clear all, close all;
I = imread('cameraman.tif');
figure (1),imshow(I)
figure(2); imhist(I)
T=120/255;
Ibw1 = im2bw(I,T);
figure(3);
subplot(1,2,1), imshow(Ibw1);
T=graythresh(I);
L = uint8(T*255)
Ibw2 = im2bw(I,T);
subplot(1,2,2), imshow(Ibw2);
help im2bw;
help graythresh;
clear all, close all;
I = imread('cameraman.tif'); figure (1),imshow(I)
figure(2); imhist(I)
T=240/255;
Ibw1 = im2bw(I,T);
figure(3);
subplot(1,2,1), imshow(Ibw1); T=graythresh(I);
L = uint8(T*255)
Ibw2 = im2bw(I,T);
subplot(1,2,2), imshow(Ibw2); help im2bw;
help graythresh;
clear all, close all;
I = imread('cameraman.tif'); figure (1),imshow(I)
figure(2); imhist(I)
T=120/255;
Ibw1 = im2bw(I,T);
figure(3);
subplot(1,2,1), imshow(Ibw1); T=graythresh(I);
L = uint8(T*255)
Ibw2 = im2bw(I,T);
subplot(1,2,2), imshow(Ibw2); help im2bw;
help graythresh;
2.边缘检测
clear all, close all;
I = imread('moon.tif');
BW1 = edge(I,'sobel');
BW2 = edge(I,'canny');
BW3 = edge(I,'prewitt');
BW4 = edge(I,'roberts');
BW5 = edge(I,'log');
figure(1), imshow(I), title('Original Image'); figure(2), imshow(BW1), title('sobel'); figure(3), imshow(BW2), title('canny'); figure(4), imshow(BW3), title('prewitt'); figure(5), imshow(BW4), title('roberts'); figure(6), imshow(BW5), title('log');
help edge
edgedemo
(二)利用MATLAB熟悉并验证其它图像分割方法灰度阈值分割:
I=imread('C:\Users\Administrator\Desktop\rice.jpg');
I=rgb2gray(I);
I2=im2bw(I);
figure,imshow(I2);
I2=im2bw(I,140/255);
figure,imshow(I2)
区域分割法:
I=imread('eight.tif'); imshow(I)
c=[222 272 300 270 221 194]; r=[21
21
75
121 121
75];
BW=roipoly(I,c,r);
figure,imshow(BW)
H=fspecial('unsharp');
J1=roifilt2(H,I,BW);
figure,imshow(J1)
J2=roifill(I,c,r);
figure,imshow(J2)
分水岭分割法:
f=imread('C:\Users\Administrator\Desktop\cell.jpg'); imshow(f);
g=im2bw(f, graythresh(f));
figure,imshow(g);
gc=~g;
D=bwdist(gc);
L=watershed(-D);
w=L==0;
g2=g&~w;
figure,imshow(g2)
(三)采用MATLAB编程实现自动全局阈值算法,对图像'rice.tif'进行二值化分割
算法步骤:
1)选取一个的初始估计值T;
2)用T分割图像。
这样便会生成两组像素集合:G1由所有灰度值大于T 的像素组成,而G2由所有灰度值小于或等于T 的像素组成。
3)对G1和G2中所有像素计算平均灰度值μ1和μ2。
4)计算新的阈值:T =(μ1+μ2)/2
5)重复步骤(2)到(4),直到逐次迭代所得到的T 值之差小于一个事先定义的参数T o,即,如果|T n– T n-1|<T o ,则停止。
clc;clear all;
I=imread('C:\Users\Administrator\Desktop\rice.gif');
I=double(I)/255;
k1=(max(max(I))+min(min(I)))/2;
[rows cols]=size(I);
count1=0;
count2=0;
for i=1:rows
for j=1:cols
if I(i,j)<k1
count1=count1+1;
G1(count1).I=I(i,j);
else
count2=count2+1;
G2(count2).I=I(i,j);
end
end
end
k2=(mean(mean([G1.I]))+mean(mean([G2.I])))/2; while(abs(k2-k1)>(5/255))
k1=k2;
count1=0;
count2=0;
for i=1:rows
for j=1:cols
if I(i,j)<k1
count1=count1+1;
G1(count1).I=I(i,j);
else
count2=count2+1;
G2(count2).I=I(i,j);
end
end
end
k2=(mean(mean([G1.I]))+mean(mean([G2.I])))/2;
end
figure(1);imshow(I);
figure(2);II=im2bw(I,k2);imshow(II);
三、实验设备
1.计算机;
2.MATLAB6.5;
四、实验总结
图像分割与边缘检测一直以来都是贯穿整本书的,当然也是重难点,在分割的时候,方法之多,步骤之繁琐也是可见一斑,所以在这次实验当中我只是用了区域分割和分水岭分割法,简单的处理了一下,当看到图像的变化的时候,觉得只有自己动手做才能真切的看到一份辛勤,一份收获!。