实验-四-图像分割与边缘检测

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验四图像分割与边缘检测

一.实验目的及要求

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|

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

相关文档
最新文档