FPS = videoObj.FrameRate;
% Read the first frame in the video sequence as the initial value newframe = read(videoObj, 1); fmed = double(newframe);
% Get the height, width, and number of color components of the frame [height, width, numColor] = size(newframe); % Assign a value to the threshold Threh = 20; beta = 0.6;
fg = false(height, width);
%创建方形结构元素,用于对分割结果形态学滤波 se = strel('square',3);
% To avoid consuming too much memories, read only a one frame each time. for n = 1:numFrames
newframe = read(videoObj, n);
% Calculate the differrence image between the new frame and fmed Idiff = double(newframe) - fmed; % Update the median of each pixel value pixInc = find(Idiff > 0);
fmed(pixInc) = fmed(pixInc) + beta; pixDec = find(Idiff < 0);
fmed(pixDec) = fmed(pixDec) - beta;
% Motion segment, detection moving object by threholding Idiff fg = abs(Idiff) >Threh;
if ( numColor == 3) % color image fg = fg(:, :, 1) | fg(:, :, 2) | fg(:, :, 3); end
%对分割结果进行形态学滤波 fg2 = imopen(fg,se);
fg2 = imclose(fg2,se); figure(1);
subplot(1,3,1), imshow(newframe);
title(strcat('Current Image, No. ', int2str(n))); subplot(1,3,2), imshow(fg);
title('Segmented result using Approximate Median Filter');
24
subplot(1,3,3), imshow(fg2);
title('Segmented result using morphological filter'); end
%----------------------------------------------------------------------
Current Image Foreground Image(moving object) filtered by morphological opening operation
(三)采用MATLAB编程实现全局阈值算法,对图像'rice.png'进行二值化分割 算法步骤:
⑴选取一个的初始估计值T;
⑵用T分割图像。这样便会生成两组像素集合:G1由所有灰度值大于 T 的像素组成,而G2由所有灰度值小于或等于 T 的像素组成。
⑶对G1和G2中所有像素计算平均灰度值?1和?2。 ⑷计算新的阈值: T =(?1+?2)/2
⑸重复步骤(2)到(4),直到逐次迭代所得到的 T 值之差小于一个事先定义的参数To,即,如果 |Tn – Tn-1| 三、实验设备 1.PIII以上微机; 2.MATLAB6.5; 四、预习与思考 1.预习实验内容,阅读教材熟悉实验原理; 2.查阅资料,熟悉实验中涉及的有关MATLAB函数; 2.利用课余时间,采用MATLAB底层函数编程实现实验内容(二)中的自动全局阈值算法。 五、实验报告要求 25 1.简述试验的目的和试验原理; 2.叙述各段程序功能,改变有关函数的参数,分析比较实验结果; 3.打印出所编写的实验程序。 4.写出本实验的心得体会及意见。 实验五 图像的几何变换 一.实验目的及要求 掌握图像几何变换的基本原理,熟练掌握数字图像的缩放、旋转、平移、镜像和转置的基本原理及其MATLAB编程实现方法。 二、实验内容 (一)研究以下程序,分析程序功能;输入执行各命令行,认真观察命令执行的结果。熟悉程序中所使用函数的调用方法,改变有关参数,观察试验结果。 1. 图像缩放 clear all, close all I = imread('cameraman.tif'); Scale = 1.35; % 将图像放大1.35倍 J1 = imresize(I, Scale, 'nearest'); % using the nearest neighbor interpolation J2 = imresize(I, Scale, 'bilinear'); % using the bilinear interpolation imshow(I), title('Original Image'); figure, imshow(J1), title('Resized Image-- using the nearest neighbor interpolation '); figure, imshow(J2), title('Resized Image-- using the bilinear interpolation '); % 查看imresize使用帮助 help imresize %-------------------------------------------------------------------- 说明: ?注意观察不同插值方法的图像表现; ?改变图像缩放因子Scale,重做上述实验。 2. 图像旋转 clear all, close all I = imread('cameraman.tif'); Theta = 45; % 将图像逆时针旋转45?。 26 J1 = imrotate(I, Theta, 'nearest'); % using the nearest neighbor interpolation Theta = -45; % 将图像顺时针旋转45?。 J2 = imrotate(I, Theta, 'bilinear', 'crop'); % using bilinear interpolation and crops the output image imshow(I), title('Original Image'); figure, imshow(J1), title('Rotated Image-- using the nearest neighbor interpolation '); figure, imshow(J2), title(' Rotated Image-- using the bilinear interpolation '); % 查看imrotate使用帮助 help imrotate %--------------------------------------------------------------------- 说明: ?注意观察不同插值方法和输出图像后处理方法的图像表现; ?改变旋转角度大小和方向,重做上述实验。 3.图像水平镜象 clear all, close all I = imread('cameraman.tif'); I1 = flipdim(I,2); I2 = flipdim(I,1); figure(1), subplot(1,2,1), imshow(I); subplot(1,2,2), imshow(I1); figure(2), subplot(2,1,1), imshow(I); subplot(2,1,2), imshow(I2); %------------------------------------------------------------------ (二)用MATLAB编程实现以下图像几何变换(参考自编讲义相关章节) 1.图像扭曲变换 2.球面变换 三、实验设备 1.PIII以上微机; 2.MATLAB6.5; 四、预习与思考 1.预习实验内容,阅读教材熟悉实验原理; 2.查阅资料,熟悉实验中涉及的有关MATLAB函数; 3.利用课余时间,采用MATLAB底层函数编程实现实验内容(二)中的图像平移、图像转置等几何变换。 27 五、实验报告要求 1.简述试验的目的和试验原理; 2.叙述各段程序功能,改变有关函数的参数,分析比较实验结果; 3.打印出所编写的实验程序。 4.写出本实验的心得体会及意见。 实验六 数字图像处理应用 一.实验目的及要求 1.利用MATLAB提供的图像处理函数实现图像中物体属性的测量; 2.训练综合运用MATLAB图像处理函数的能力; 3.了解数字图像处理基本应用。 二、实验内容 以大米粒特性测量为例,综合应用课程中图像分割、形态学滤波、图像增强、图像特征提取等图像处理方法,实现大米粒特性自动测量。 实验过程简述: 1. 读取和显示图像 2. 估计图像背景 3. 获取背景均匀的图像 4. 图像增强 5. 图像二值化分割 6. 区域标记及为彩色处理 7. 测量图像中的区域特性(面积、质心等) 8.统计大米粒的特性分布规律。 (一)研究以下程序,分析程序功能;输入执行各命令行,认真观察命令执行的结果。熟悉程序中所使用函数的调用方法,改变有关参数,观察试验结果。 % Read and Display an Image clear, close all,close all; I = imread('rice.png'); figure, imshow(I) % Use Morphological Opening to Estimate the Background background = imopen(I,strel('disk',15)); figure, imshow(background); %Display the Background Approximation as a Surface figure, surf(double(background(1:8:end,1:8:end))),zlim([0 255]); set(gca,'ydir','reverse'); 28