数字图像处理实验报告
计算机科学与软件学院
软件C103 XXX XXXXXX
实验一
一.熟悉MATLAB软件的开发环境、基本操作以及图像处理工具箱,为编写图像处理程序奠定基础。
1.矩阵:
A=[1 2 3;4 5 6];
2.读取图像,并进行显示 I=imread(‘xy.jpg’); Imshow(I);
二.说明以下程序的显示结果为何是一幅几乎全黑的图像。
clear; close all; myi=zeros(20,20); myi(2:2:18,2:2:18)=1; myi=uint8(myi);
figure, imshow(myi,'notruesize');
首先1中的clear是清除当前内存中的变量,close all 是关闭所有窗口(程序运行产生的,不包括命令窗,editor窗和帮助窗)
2中初始化了一个20X20的矩阵,每个元素的值都是0.并且这个矩阵的名字是myi。 3.从矩阵的第2行开始,每次走2行,到第18行,从矩阵的第2列开始,每次走2列,到第18列,让这些地方的元素值为1.
4.uint8这个函数是把数值矩阵转换为图像矩阵,就是说最大值255表示白色,最小值0表示黑色。但是转换后矩阵的值和原来的一样。也就是说在3中矩阵的值变到4后没变,所以因为这些值要么仍是0要么仍是1,这样显示以后会看起来都是黑色。
5.imshow里的noturesize参数就是设定图片去适应窗口,窗口大小不动,自动调节图片大小。所以显示出来的图片跟实际尺寸可能不一致。
三.编写程序将图一中orangutan_1.tif图片拉伸一倍后形成orangutan_2.tif所示的图片 A= imread('C:\\Users\\jason\\Desktop\\orangutan_2.tif'); imshow(A); figure
tform = maketform('affine',[1.414 0 0; 0 1 0; 0 0 1]); B = imtransform(A,tform); imshow(B);
四、应用MATLAB(或C)语言编写一幅灰度图像直方图统计程序,并选择一幅图像显示其直方图,将结果与MATLAB图像处理工具箱中提供的灰度直方图函数imhist的处理结果进
行比较。 clear;
i=imread('2.jpg'); x=rgb2gray(i); figure;
subplot(2,3,1),imshow(i),title('原图');
subplot(2,3,3),imhist(x),title('灰度直方图')
五、利用以上编写的程序,估算图一所示图像iris.tif中的瞳孔半径(以像素为单位)。
程序: clear all;
A=imread('yan.jpg'); B=rgb2gray(A); imhist(B); [m n]=size(B); cout=0; for i=1:m for j=1:n
if(B(i,j)<20)
cout=cout+1; end end end
disp(['cout=',num2str(cout)]); r=sqrt(cout/3.14); disp(['r=',num2str(r)]); 结果图:
面积的结果是cout=4429,半径是r=37.5568
一.二维傅里叶变换:程序: clear all;
img=imread('C:\\Users\\jason\\Desktop\\lena512.jpg'); img1=img;
subplot(2,2,1),imshow(img1); title('灰度图像'); F=fft2(img1);
subplot(2,2,2),imshow(F,[]);
title('傅立叶变换后,高频部分在四角'); F=abs(F); F1=fftshift(F);
subplot(2,2,3),imshow(F1,[]); title('把高频变到中间'); F2=1+log(F1);
subplot(2,2,4),imshow(F2,[]); title('指数优化后显示'); 处理结果:
实验二
二.直方图均衡化:
利用Matlab提供的可进行图像直方图修正的函数,自己选择几幅直方图不均匀的图像(如图二pout.tif),对这些图像进行直方图均衡处理,显示处理前后的图像以及它们的灰度直方图,体会直方图均衡化算法的特点 clear all;
A=imread('C:\\Users\\jason\\Desktop\\lena512.jpg'); B=A;
subplot(2,2,1),imshow(B); title('均衡化之前'); subplot(2,2,2),imhist(B);
title('均衡化之前的直方图'); C=histeq(B);
subplot(2,2,3),imshow(C); title('均衡化之后'); subplot(2,2,4),imhist(C);
title('均衡化之后的直方图');
三.灰度修正技术 clear all;
A = imread('C:\\Users\\jason\\Desktop\\pout.tif'); RGB1=A;
C=fspecial('gaussian');
subplot(2,4,1),imshow(RGB1); title('原始图像');
subplot(2,4,2),imhist(RGB1); title('原始图像的灰度直方图');
RGB2 = imadjust(RGB1,[.2 .3 0; .6 .7 1],[],0.3); subplot(2,4,3),imshow(RGB2); title('参数为0.3,灰度修正后'); subplot(2,4,4),imhist(RGB2);
title('参数为0.3灰度修正后的灰度直方图');
RGB2 = imadjust(RGB1,[.2 .3 0; .6 .7 1],[],1); subplot(2,4,5),imshow(RGB2); title('参数为1,灰度修正后'); subplot(2,4,6),imhist(RGB2);
title('参数为1灰度修正后的灰度直方图');
RGB2 = imadjust(RGB1,[.2 .3 0; .6 .7 1],[],3); subplot(2,4,7),imshow(RGB2); title('参数为3,灰度修正后'); subplot(2,4,8),imhist(RGB2);
title('参数为3,灰度修正后的灰度直方图');
四.图像平滑(去噪)
i=imread('C:\\Users\\jason\\Desktop\\lena512.jpg'); p=imnoise(i,'gaussian',0.1);
f1=[0 1 0;1 0 1;0 1 0]/4; %所用模板(4-邻域) f2=[0 1 0;1 0 1;0 1 0]/8; %所用模板(8-邻域) j=imfilter(p,f1,'corr','replicate'); k=imfilter(p,f2,'corr','replicate'); subplot(2,2,1),imshow(i),title('原图');
subplot(2,2,2),imshow(p),title('噪声污染后图'); subplot(2,2,3),imshow(j),title('4-邻域处理后图'); subplot(2,2,4),imshow(k),title('8-邻域处理后图');
五.边缘检测技术: clear all;
A=imread('C:\\Users\\jason\\Desktop\\orangutan_2.tif'); subplot(2,3,1),imshow(A); title('原始图像'); B=im2bw(A,0.95);
subplot(2,3,2),imshow(B); title('原始二值图像'); C=double(B);
D=edge(C,'sobel');
subplot(2,3,3),imshow(D); title('sobel边缘提取方法'); E=edge(C,'prewitt');
subplot(2,3,4),imshow(E);
title('prewitt边缘提取方法'); F=edge(C,'roberts');
subplot(2,3,5),imshow(F);
title('roberts边缘提取方法'); G=edge(C,'log');
subplot(2,3,6),imshow(G);
title('拉普拉斯边缘提取方法');