3.细化与骨架抽取
clear all, close all BW = ~ imread('logo.tif'); BW1 = bwmorph(BW,'thin',Inf); BW2 = bwmorph(BW,'skel',Inf);
subplot(1,3,1), imshow(BW), title(' Original Image '); subplot(1,3,2), imshow(BW1), title(' Thinned Image '); subplot(1,3,3), imshow(BW2), title(' Image skeleton'); %查看bwmorph函数使用说明 help bwmorph
%-----------------------------------------------------------
(二)用MATLAB数学形态学函数编程实现
1.编程实现作业9.17的形态学滤波任务(图片可从Gonzalez网站下载)。 2. 编程实现作业9.34的形态学纹理分割任务(图片可从Gonzalez网站下载)。
FigP0917(noisy_rectangle).tif FigP0934(blobs_in_circular_arrangement).tif
(参看PPT相关内容)
三、实验设备
1.PIII以上微机; 2.MATLAB6.5;
四、预习与思考
1.预习实验内容,阅读教材熟悉实验原理; 2. 查阅资料,熟悉实验中涉及的有关函数。
3.利用课余时间,采用MATLAB函数编程实现实验内容(二)。
19
五、实验报告要求
1.简述试验的目的和试验原理;
2.叙述各段程序功能,改变有关函数的参数,分析比较实验结果; 3.打印出所编写的实验程序。 4.写出本实验的心得体会及意见。
实验四 图像分割
一.实验目的及要求
1.利用MATLAB研究图像分割与边缘检测的常用算法原理; 2.掌握MATLAB图像域值分割与边缘检测函数的使用方法;
3.了解边缘检测的算法和用途,比较Sobel、Prewitt、Canny等算子边缘检测的差异。
二、实验内容
(一)研究以下程序,分析程序功能;输入执行各命令行,认真观察命令执行的结果。熟悉程序中所使用函数的调用方法,改变有关参数,观察试验结果。
1.灰度图像的单阈值分割
clear all, close all; I = imread('rice.png');
%选择阈值T1=120/255对图像二值化 %注意,调用im2bw时,阈值在[0,1]之间取值 T1=120/255;
Ibw1 = im2bw(I,T1); %采用Otsu方法计算最优阈值T2对图像二值化 T2 = graythresh(I); Ibw2 = im2bw(I,T2); figure(1);
subplot(2,2,1), imshow(I), title(' Original Image '); subplot(2,2,2), imhist(I);
subplot(2,2,3), imshow(Ibw1), title('Segemented result using threshold = 120');
subplot(2,2,4), imshow(Ibw2); title(strcat('Segemented result using threshold = ',int2str(T2*255))); %采用顶帽变换(Top-hat)校正图像的不均匀光照
%enhance an image to correct for non-uniform illumination using Top-hat transform background = imopen(I,strel('disk',15)); I2 = I - background;
20
%利用灰度变换增强校正后的图像 I3 = imadjust(I2);
%采用Otsu方法计算最优阈值T3对图像二值化 T3 = graythresh(I3); Ibw3 = im2bw(I3,T3); figure(2);
subplot(2,3,1), imshow(I), title(' Original Image ');
subplot(2,3,2), imshow(background), title(' background Image ');
subplot(2,3,3), imshow(I2), title(' non-uniform illumination corrected Image '); subplot(2,3,4), imshow(I3), title(' Image After Intensity Adjustment ');
subplot(2,3,5), imshow(Ibw3); title(strcat('Segemented result using threshold = ',int2str(T3*255)));
2.彩色图像分割(蓝幕技术的背景去除)
close all; clear all;
I = imread('Fig0815(a)_Bridewedding.jpeg'); Id = double(I);
% 交互获取样本,移动鼠标单击左键在图像蓝色背景上选点形成封闭多边形, % 然后单击鼠标右键,弹出菜单,选择“Create Mask” mask = roipoly(I); figure,imshow(mask);
%得到彩色图像的RGB三个颜色分量 redplane = I(:, :, 1); greenplane = I(:, :, 2); blueplane= I(:, :, 3);
%得到所有样本像素的RGB三个颜色分量 xR = redplane(mask); xG = greenplane(mask); xB = blueplane(mask);
% Concatenate R, G ,B component arrays along column dimension form sample data matrix X = cat(2, xR, xG, xB); X = double(X);
%得到样本像素的数量K [K, n] = size(X); % el
%计算样本像素的均值向量M和协方差矩阵C
% Compute an unbiased estimate of mean. M is a row vector here. M = sum(X, 1)/K;
21
% Subtract the mean from each row of X. X = X - repmat(M,K,1);
% Compute an unbiased estimate of covariance matrix C.
% Note that the product is X'*X because the vectors are rows of X. C = (X'*X)/(K - 1);
% Determine the threshold using covariance matrix C. % TE is used for Euclidean distance d = diag(C); sd = sqrt(d); TE = 3 * max(sd); %避免阈值过小 if TE < 10 TE = 10; end
%采用上述参数,采用欧氏距离对彩色图像分割,“扣取”前景目标 %获取图像的高、宽 [Height, Width, Q] = size(I); %初始化分割结果变量
%二值图像,背景像素值=0,前景像素=1 gmask = false(Height, Width);
%彩色图像,背景像素RGB各分量值=127,前景像素与源输入图像同 gcolor = uint8(127 * ones(Height, Width,3)); sqTE = TE * TE; %
for x = 1: Height for y = 1: Width
sqdist = (Id(x,y,1) - M(1))^2 + (Id(x,y,2) - M(2))^2 + (Id(x,y,3) - M(3))^2; if (sqdist > sqTE) %该像素为前景 gmask(x, y) = 1;
gcolor(x, y,:) = I(x,y,:); end end end %
22
%显示处理结果 figure(1);
subplot(1,3,1), imshow(I), title(' Original Image ');
subplot(1,3,2), imshow(gcolor), title('Segmented color image'); subplot(1,3,3), imshow(gmask), title('Segmented binary image'); %----------------------------------------------------------------------
2.边缘检测
clear all, close all; I = imread('rice.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');
%----------------------------------------------------------------------
% 在完成上述试验后,查看函数edge()使用说明。 help edge
% 仔细阅读函数edge()使用说明后,研究IPT提供的边缘检测演示程序。edgedemo
(二)图像分割中运动的运用(运动目标检测)
% Approximate Median Filter background model for moving object segmentation. %采用近似中值滤波背景模型参考图像实现运动目标分割 clear all; close all;
% Construct a videoreader class to read a avi file, first the 'car_parking.avi' , % then the ?highwayII_raw.avi'.
videoObj = VideoReader('car_parking.avi'); numFrames =videoObj.NumberOfFrames;
%Get the speed of the AVI movie in frames per second (fps)
23