数字图像处理实验报告.docx
《数字图像处理实验报告.docx》由会员分享,可在线阅读,更多相关《数字图像处理实验报告.docx(19页珍藏版)》请在冰豆网上搜索。
数字图像处理实验报告
实验一图像文件格式
一、实验目的
以BMP图像文件格式为例,了解图像文件格式的基本特征。
二、实验内容
利用matlab读取BMP图像(含二值图像、灰度图像、索引色图像、真彩色图像),分析BMP图像文件数据结构特征,掌握图像文件存储的一般规律。
三、实习步骤:
1、利用以下命令读取图像(提示:
注意修改图像文件路径)
Fopen():
//Openfile,orobtaininformationaboutopenfiles,判断图像文件能否打开
Fread():
//Readdatafrombinaryfile,从二进制文件读取数据
Imfinfo():
//Informationaboutgraphicsfile
Imread():
//Readimagefromgraphicsfile
对于索引色图像,使用[x,map]=imread(filename,fmt)形式读取,其中x为图像矩阵
Figure:
figureCreatefiguregraphicsobject
Imshow():
//Displayimage
对于索引色图像,使用imshow(x,map)形式显示图像
Imhist(x,map),显示索引色图像的直方图
1.1单色位图图像的操作
[fid1,mesage1]=fopen('cameraman-1.bmp','r');
data1=fread(fid1);
fileinfo1=imfinfo('cameraman-1.bmp','bmp');
[img1,map1]=imread('cameraman-1.bmp','bmp');
figure,imshow(img1,map1);
结果图
1.216色位图图像的操作
[fid2,mesage2]=fopen('cameraman-4.bmp','r');
data2=fread(fid2);
fileinfo2=imfinfo('cameraman-4.bmp','bmp');
[img2,map2]=imread('cameraman-4.bmp','bmp');
figure,imshow(img2,map2);
结果图
1.3256色位图图像的操作
[fid3,mesage3]=fopen('cameraman-8.bmp','r');
Data3=fread(fid3);
Fileinfo3=imfinfo('cameraman-8.bmp','bmp');
[img3,map3]=imread('cameraman-8.bmp','bmp');
figure,imshow(img3,map3);
1.4彩色位图图像的操作
1.4.1基于索引色的彩色位图图像的操作
[fid4,mesage4]=fopen('canoe-256.bmp','r');
Data4=fread(fid4);
Fileinfo4=imfinfo('canoe-256.bmp','bmp');
[img4,map4]=imread('canoe-256.bmp','bmp');
figure,imshow(img4,map4);
结果图
[fid5,mesage5]=fopen('forest-256.bmp','r');
Data5=fread(fid5);
Fileinfo5=imfinfo('forest-256.bmp','bmp');
[img5,map5]=imread('forest-256.bmp','bmp');
figure,imshow(img5,map5);
结果图
1.4.2真彩色图像操作
[fid6,mesage6]=fopen('peppers.bmp','r');
Data6=fread(fid6);
Fileinfo6=imfinfo('peppers.bmp','bmp');
[img6,map6]=imread('peppers.bmp','bmp');
figure,imshow(img6,map6);
结果图
2、根据BMP文件存储格式观察分析不同单色、16色位图、256色、真彩色位图图像data,fileinfo,img数据内容,结合以下5方面理解BMP格式存储特征。
2.1位图文件头结构及其各组分内容:
包括bfType、bfSize、bfOffBits。
2.2位图信息头结构及其各组分内容:
分析biSize、biWidth、biHeight、biPlanes、biBitCount、biCompression、biSizeImage等内容。
2.3调色板内容及其组分内容:
调色板的结构、调色板内容、调色板大小,分析单色图像、灰度图像、索引色图像、真彩色图像的调色板。
2.4位图数据内容:
索引色图像和真彩色图像的位图数据内容,图像数据是如何保存到位图数据结构的,单色、16色和256色、真彩色图像存储时文件大小的计算。
2.5图像数据宽度对BMP图像存储的影响
四实习体会与收获
附录:
1:
BMP文件组成
BMP文件由文件头、位图信息头、颜色信息和图像数据四部分组成。
2:
BMP文件头(14字节)
BMP文件头数据结构含有BMP文件的类型、文件大小和位图起始位置等信息。
其结构定义如下:
typedefstructtagBITMAPFILEHEADER
{
WORDbfType;//位图文件的类型,必须为BM(1-2字节)
DWORDbfSize;//位图文件的大小,以字节为单位(3-6字节)
WORDbfReserved1;//位图文件保留字,必须为0(7-8字节)
WORDbfReserved2;//位图文件保留字,必须为0(9-10字节)
DWORDbfOffBits;//位图数据的起始位置,以相对于位图(11-14字节)
//文件头的偏移量表示,以字节为单位
}BITMAPFILEHEADER;
typedefstructtagBITMAPINFOHEADER{
DWORDbiSize;//本结构所占用字节数(15-18字节)
LONGbiWidth;//位图的宽度,以像素为单位(19-22字节)
LONGbiHeight;//位图的高度,以像素为单位(23-26字节)
WORDbiPlanes;//目标设备的级别,必须为1(27-28字节)
WORDbiBitCount;//每个像素所需的位数,必须是1(双色),(29-30字节)
//4(16色),8(256色)16(高彩色)或24(真彩色)之一
DWORDbiCompression;//位图压缩类型,必须是0(不压缩),(31-34字节)
//1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一
DWORDbiSizeImage;//位图的大小,以字节为单位(35-38字节)
LONGbiXPelsPerMeter;//位图水平分辨率,每米像素数(39-42字节)
LONGbiYPelsPerMeter;//位图垂直分辨率,每米像素数(43-46字节)
DWORDbiClrUsed;//位图实际使用的颜色表中的颜色数(47-50字节)
DWORDbiClrImportant;//位图显示过程中重要的颜色数(51-54字节)
}BITMAPINFOHEADER;
typedefstructtagRGBQUAD{
BYTErgbBlue;//蓝色的亮度(值范围为0-255)
BYTErgbGreen;//绿色的亮度(值范围为0-255)
BYTErgbRed;//红色的亮度(值范围为0-255)
BYTErgbReserved;//保留,必须为0
}RGBQUAD;
5:
位图数据
位图数据记录了位图的每一个像素值,记录顺序是在扫描行内是从左到右,扫描行之间是从下到上。
位图的一个像素值所占的字节数:
当biBitCount=1时,8个像素占1个字节;
当biBitCount=4时,2个像素占1个字节;
当biBitCount=8时,1个像素占1个字节;
当biBitCount=24时,1个像素占3个字节;
Windows规定一个扫描行所占的字节数必须是
4的倍数(即以long为单位),不足的以0填充,
biSizeImage=((((bi.biWidth*bi.biBitCount)+31)&~31)/8)*bi.biHeight;
实验二Matlab图像处理
一、实验目的
利用Matlab图像处理工具箱中的函数实现各种图像常规处理,深化课程理论知识认识。
二、实验内容和要求
实习内容:
利用matlab读取BMP图像,实现图像点变换、空间域局部处理、几何变换、彩色合成和彩色变换。
3、实习步骤及结果
1图像的灰度变换
I=imread('cameraman-8.bmp','bmp');
figure;subplot(2,2,1),imshow(I);subplot(2,2,2),imhist(I);
[counts1,x1]=imhist(I);%观察Counts,x的值
subplot(2,2,3),stem(x1,counts1);
J=imadjust(I,[0.10.7],[0.00.9]);
subplot(2,2,4),imshow(J);
figure;subplot(2,3,1),imshow(I);
subplot(2,3,2),imshow(J);
subplot(2,3,4),imhist(I);
subplot(2,3,5),imhist(J);
[counts2,x2]=imhist(J);
subplot(2,3,6),stem(x2,counts2);
结果图
图1
图2
2图像直方图处理
2.1直方图均衡化
I=imread('cameraman-8.bmp','bmp');
J=histeq(I);
figure
(1),subplot(2,2,1),imshow(I),subplot(2,2,2),imshow(J);
figure
(1),subplot(2,2,3),imhist(I),subplot(2,2,4),imhist(J);
结果图
2.2直方图规定化
I1=imread('TM5.bmp','bmp');
I2=imread('TM3.bmp','bmp');
K1=histeq(I1,imhist(I2));
figure;
subplot(3,2,1),imshow(I1);
subplot(3,2,2),imhist(I1);
subplot(3,2,3),imshow(I2);
subplot(3,2,4),imhist(I2);
subplot(3,2,5),imshow(K1);
subplot(3,2,6),imhist(K1);
结果图
3图像空间域平滑
3.1用均值滤波器实现图像空间域的平滑
I=imread('cameraman-8.bmp','bmp');
J=imnoise(I,'gaussian');%添加高斯噪声
K=imnoise(I,'salt&pepper');%添加椒盐噪声
M=imnoise(I,'speckle');%添加乘性噪声
H=ones(3,3)/9;%3*3的均值去噪模板
I1=imfilter(I,H);
J1=imfilter(J,H);
K1=imfilter(K,H);
M1=imfilter(M,H);%去噪
figure;
subplot(2,4,1),imshow(I);title('原图');
subplot(2,4,2),imshow(J);title('高斯噪声图像');
subplot(2,4,3),imshow(K);title('椒盐噪声图像');
subplot(2,4,4),imshow(M);title('乘性噪声图像');
subplot(2,4,6),imshow(J1);title('高斯噪声滤波图像');
subplot(2,4,7),imshow(K1);title('椒盐噪声滤波图像');
subplot(2,4,8),imshow(M1);title('乘性噪声滤波图像');
结果图
3.2用中值滤波器实现图像空间域的平滑
I=imread('cameraman-8.bmp');
I1=imnoise(I,'gaussian');
I2=imnoise(I,'salt&pepper',0.02);
I3=imnoise(I,'speckle');
J=medfilt2(I,[3,3]);%3×3中值滤波模板
J1=medfilt2(I1,[3,3]);
J2=medfilt2(I2,[3,3]);
J3=medfilt2(I3,[3,3]);
figure,subplot(2,4,1),imshow(I);title('原图');
subplot(2,4,2),imshow(I1);title('添加高斯噪声');
subplot(2,4,3),imshow(I2);title('添加椒盐噪声');
subplot(2,4,4),imshow(I3);title('添加乘性噪声');
subplot(2,4,6),imshow(J1);title('高斯噪声3*3中值滤波');
subplot(2,4,7),imshow(J2);title('椒盐噪声3*3中值滤波');
subplot(2,4,8),imshow(J3);title('乘性噪声3*3中值滤波');
K=medfilt2(I,[5,5]);%5×5中值滤波模板
K1=medfilt2(I1,[5,5]);
K2=medfilt2(I2,[5,5]);
K3=medfilt2(I3,[5,5]);
figure;
subplot(2,2,1),imshow(K);title('原图5*5中值滤波');
subplot(2,2,2),imshow(K1);title('高斯噪声5*5中值滤波');
subplot(2,2,3),imshow(K2);title('椒盐噪声5*5中值滤波');
subplot(2,2,4),imshow(K3);title('乘性噪声5*5中值滤波');
结果图
图1
图2
4图像空间域锐化
I=imread('cameraman-4.bmp','bmp')
H=fspecial('sobel')%用sobel算子做模板
J=imfilter(I,H)
figure,subplot(1,2,1),imshow(I),subplot(1,2,2),imshow(J);
结果图
5图像几何变换
5.1缩放
I=imread('cameraman-8.bmp','bmp');J=imresize(I,2);K=imresize(I,0.5);
figure
(1),subplot(),imshow(I),title('原图');
figure
(2),subplot(),imshow(J),title('放大两倍的图');
figure(3),subplot(),imshow(K),title('缩小0.5倍的图');
结果图
5.2旋转
I=imread('cameraman-8.bmp','bmp');
M=imrotate(I,45);
figure;
subplot(1,2,1),imshow(I),subplot(1,2,2),imshow(M);
结果图
6、彩色图像处理
6.1彩色变换
I=imread('peppers.bmp','bmp');
HSV=rgb2hsv(I);
RGB=hsv2rgb(HSV);
figure;
subplot(1,3,1),imshow(I),title('原图'),
subplot(1,3,2),imshow(HSV),title('HSV图像');
subplot(1,3,3),imshow(RGB),title('RGB图像');
结果图
6.2彩色合成
I=imread('peppers.bmp','bmp');
J1=I;
J2=I;
J3=I;
J4=I;
J5=I;
J1(:
:
1)=I(:
:
2),J1(:
:
2)=I(:
:
3),J1(:
:
3)=I(:
:
1);
J2(:
:
1)=I(:
:
1),J2(:
:
2)=I(:
:
3),J2(:
:
3)=I(:
:
2);
J3(:
:
1)=I(:
:
2),J3(:
:
2)=I(:
:
1),J3(:
:
3)=I(:
:
3);
J4(:
:
1)=I(:
:
3),J4(:
:
2)=I(:
:
2),J4(:
:
3)=I(:
:
1);
J5(:
:
1)=I(:
:
3),J5(:
:
2)=I(:
:
1),J5(:
:
3)=I(:
:
2);
figure,subplot(2,3,1),imshow(I),title('原图');
subplot(2,3,2),imshow(J1),title('变换一');
subplot(2,3,3),imshow(J2),title('变换二');
subplot(2,3,4),imshow(J3),title('变换三');
subplot(2,3,5),imshow(J4),title('变换四');
subplot(2,3,6),imshow(J5),title('变换五');
结果图
四实习体会与收获
附录:
函数说明
Imread():
读取图像
Imadjust():
Adjustimageintensityvaluesorcolormap
Imhist():
Displayhistogramofimagedata
stem():
Plotdiscretesequencedata
subplot():
Createaxesintiledpositions
histeq():
Enhancecontrastusinghistogramequalization
imshow():
Displayimage
imnoise():
Addnoisetoimage
ones():
Createarrayofallones
medfilt2():
2-Dmedianfiltering
rgb2hsv():
ConvertRGBcolormaptoHSVcolormap
hsv2rgb():
ConvertHSVcolormaptoRGBcolormap