figure, imshow(bw); title('Converted binary image');
%查看图像像素信息,在图像上移动鼠标,注意左下角的信息 impixelinfo;
%----------------------------------------------------------------------------
⑸视频文件的读取及图像帧的抽取
%采用参考图像实现运动目标分割
% This m-file implements the background subtraction using reference image % for moving object segmentation. clear all; close all;
% Construct a videoreader class to read a avi file, first the 'traffic.avi' , % then the ?highwayII_raw.avi'. videoObj = VideoReader('traffic.avi'); numFrames =videoObj.NumberOfFrames;
% Read the first frame in the video sequence as the reference background image newframe = read(videoObj, 1); Iref = 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;
fg = zeros(height, width);
% To avoid consuming too much memories, read only a one frame each time. for n = 1:numFrames
newframe = read(videoObj, n);
% Calculate the absolute differrence image between the new frame % and the referrence frame Iref. Idiff = abs(double(newframe) - Iref);
% motion segment, detection moving object by threholding Idiff fg = Idiff >Threh;
if ( numColor == 3) % color image fg = fg(:, :, 1) | fg(:, :, 2) | fg(:, :, 3); end figure(1);
subplot(1,2,1), imshow(newframe);
title(strcat('Current Image, No. ', int2str(n)));
4
subplot(1,2,2), imshow(fg);
title('Segmented result using reference image'); end
%------------------------------------------------------------
2. 图像灰度变换
⑴ 使用工具箱函数 imadjust 的直接灰度变换
clear all; close all
I = imread('cameraman.tif'); J = imadjust(I,[0 0.2],[0.5 1]); imshow(I) figure, imshow(J)
[X,map] = imread('forest.tif'); figure,imshow(X,map) I2 = ind2gray(X,map); J2 = imadjust(I2,[],[],0.5); figure,imshow(I2); figure, imshow(J2); J3 = imadjust(I2,[],[],1.5); figure, imshow(J3);
help imadjust % Display the imadjust() function information. %------------------------------------------------------------
注意:imadjust() 功能: 调整图像灰度值或颜色映像表,也可实现伽马校正。 语法: J = imadjust(I,[low_in high_in],[low_out high_out],gamma) newmap = imadjust(map,[low_in high_in],[low_out high_out],gamma) RGB2 = imadjust(RGB1,...) ⑵直方图均衡化
% Clear the MATLAB workspace of any variables and close open figure windows. clear all; close all;
% Reads the sample images , and stores it in an array named I, and display the image I = imread('Fig0310(b)(washed_out_pollen_image).tif'); imshow(I);
% Create a histogram of the image and display it in a new figure window figure, imhist(I);
% Histogram equalization. [I2,T] = histeq(I);
% Display the new equalized image, I2, in a new figure window. figure, imshow(I2) ;
% Create a histogram of the equalized image I2. figure, imhist(I2);
5
% plot the transformation curve. figure,plot((0:255)/255,T);
% Write the newly adjusted image I2 to a disk file named ?pollen_image2.tif?. imwrite (I2, 'pollen_image2.tif');
% Check the contents of the newly written file imfinfo('pollen_image2.tif')
%------------------------------------------------------------
3.空间域滤波
⑴平滑滤波(模糊、去噪)
clear all; close all; I = imread('eight.tif'); h1 = ones(3,3) / 9; h2 = ones(5,5) / 25; I1 = imfilter(I,h1); I2 = imfilter(I,h2);
figure(1), imshow(I), title('Original Image');
figure(2), imshow(I1), title('Filtered Image With 3*3 ') figure(3), imshow(I2), title('Filtered Image With 5*5 ') % 加入Gaussian 噪声
J1 = imnoise(I,'gaussian',0,0.005); % 加入椒盐噪声
J2 = imnoise(I,'salt & pepper',0.02); % 对J1、J2进行平均值平滑滤波 K1 = imfilter(J1,fspecial('average',3)); K2 = imfilter(J2,fspecial('average',3)); figure(4);
subplot(2,2,1), imshow(J1) , title('gaussian'); subplot(2,2,2), imshow(J2), title('salt & pepper '); subplot(2,2,3), imshow(K1), title('average '); subplot(2,2,4), imshow(K2); % 对J1、J2进行中值滤波 K3 = medfilt2(J1,[3 3]); K4 = medfilt2(J2,[3 3]); figure(5);
subplot(2,2,1), imshow(J1) , title('gaussian');
6
subplot(2,2,2), imshow(J2), title('salt & pepper '); subplot(2,2,3), imshow(K3), title(' Median filtering '); subplot(2,2,4), imshow(K4)
%------------------------------------------------------------
⑵锐化滤波
clear all; close all
f = imread('Fig0338(a)(blurry_moon).tif'); w4 = fspecial('laplacian',0); w8 = [1, 1, 1;1, -8, 1;1, 1, 1]; f=im2double(f);
g4=f-imfilter(f,w4,'replicate'); g8=f-imfilter(f,w8,'replicate'); g4=im2uint8(g4); g8=im2uint8(g8); imshow(f); figure,imshow(g4); figure,imshow(g8);
%------------------------------------------------------------
㈡ 采用MATLAB底层函数编程实现 (参看自编建议或PPT中的相关内容) 1.灰度变换之动态范围扩展
假定原图像f(x, y)的灰度范围为[a, b],希望变换后图像 g(x, y)的灰度范围扩展至[c, d],则线性变换可表示为:
g(x,y)?d?c[f(x,y)?a]?c b?a 用MATLAB底层函数编程实现上述变换函数。观察图像? pout.tif?的灰度直方图,选择合适的参数[a, b]、[c, d]对图像‘pout.tif’进行灰度变换,以获得满意的视觉效果。 2.非锐化掩蔽和高升滤波
从原图像中减去其非锐化(平滑过的)图像的过程称为非锐化掩蔽,其基本步骤为: ⑴对原图像进行平滑滤波得到模糊图像f(x,y);
⑵从原图像中减去模糊图像,产生的差值图像称为模板gmask(x,y); ⑶将模板加到原图像上,得到锐化后的图像g(x,y)。 即,
7
gmask(x,y)?f(x,y) - f(x,y)
g(x,y)?f(x,y)?k?gmask(x,y);k?1
用MATLAB函数编程实现上述功能。
3. 对图片中人脸区域做模糊处理
人脸是准确鉴定一个人的身份,推断出一个人的种族、地域,地位等信息的重要依据。科学界从图像处理、计算机视觉等多个学科对人脸进行研究。人脸识别在满足人工智能应用和保护信息安全方面都有重要的意义,是当今信息化时代必须解决的问题。电视采访、街景地图等应用中,为保护当事人或行人的隐私权,需要将视频图像中当事人的人脸区域作模糊或马赛克处理。
找一幅含正面人脸的图片,使用函数impixelinfo找出图片中人脸区域的范围(左上角、右下角的行列坐标),然后对图像中人脸区域模糊或马赛克处理。
三、实验设备
1.微机
2.MATLAB2014或以上版本
四、预习与思考
1.预习实验内容,阅读教材熟悉实验原理;
2.查阅资料,熟悉MATLAB的操作环境和基本功能。熟悉实验中涉及的有关函数。 3.利用课余时间,用MATLAB底层函数编程实现实验内容(二)中的灰度线性变换。 4.你能否给出实现样例程序功能的其它方法? 5.简述高斯噪声和椒盐噪声的特点。
6.结合实验内容,定性评价平均滤波器/中值滤波器对高斯噪声和椒盐噪声的去噪效果? 7.结合实验内容,定性评价滤波窗口对去噪效果的影响?
五、实验报告要求
1.简述实验目的和实验原理;
2.叙述各段程序功能,介绍关键函数的使用方法; 3.改变有关参数,用数据和图片分析比较实验结果; 4.写出本实验的心得体会。
8