1、实验一内容(一)(1)彩色图像变灰度图像A=imread(1.jpg);B=rgb2gray(A);figuresubplot(1,2,1),imshow(A)title(原图)subplot(1,2,2),imshow(B)title(原图灰度图像)(2)彩色图像变索引图像A=imread(1.jpg);figuresubplot(1,2,1),imshow(A)title(原图)X,map=rgb2ind(A,128);subplot(1,2,2),imshow(X,map)title(原图索引图像)(3)彩色图像变二值图像A=imread(1.jpg);figuresubplot(1,2
2、,1),imshow(A)title(原图) C=im2bw(A,0.2);subplot(1,2,2),imshow(C)title(原图二值图像)(4)灰度图像变索引图像(一)A=imread(1.jpg);figureB=rgb2gray(A);subplot(1,2,1),imshow(B)title(灰度图像)C=grayslice(B,39);subplot(1,2,2),imshow(C)title(灰度变索引图像)(5)灰度图像变索引图像(二)A=imread(1.jpg);figureB=rgb2gray(A);subplot(1,2,1),imshow(B)title(灰度
3、图像)X,map=gray2ind(B,63);subplot(1,2,2),imshow(X,map)title(灰度变索引图像)(6)灰度图像变彩色图像A=imread(1.jpg);figureB=rgb2gray(A);subplot(1,2,1),imshow(B)title(灰度图像)C=gray2rgb(B,map);subplot(1,2,2),imshow(C)title(灰度变彩色图像)内容(二)(1)灰度平均值A=imread(1.jpg);figureB=rgb2gray(A);subplot(1,2,1),imshow(B)title(灰度图像)B=double(B)
4、;m,n=size(B);sumg=0.0;for i=1:m;for j=1:n;sumg=sumg+B(i,j);endendavg=sumg/(m*n) % 均值maxg=max(max(B) % 区域最大灰度ming=min(min(B) % 区域最小灰度(2)彩色平均值A=imread(1.jpg);figureimshow(A)title(彩色图像)A=double(A);m,n=size(A);sumg=0.0;for i=1:m;for j=1:n;sumg=sumg+A(i,j);endendavg=sumg/(m*n)squre=m*nmaxg=max(max(A)ming
5、=min(min(A)内容(三)采样量化实验二 图像变换傅里叶变换、反变换、I=imread(19.jpg);A=rgb2gray(I);x1=fft2(A);x2=fftshift(x1);x3=ifft(x1)/10;figure,subplot(1,4,1);imshow(A)title(原图);subplot(1,4,2);imshow(x1)title(频谱图);subplot(1,4,3);imshow(log(abs(x2)+1),0 10);title(直流分量移至频谱图中心);subplot(1,4,4);imshow(x3,0 10)title(傅里叶反变换);DCT变换、
6、反变换X=imread(19.jpg);I=rgb2gray(X);subplot(1,3,1);imshow(I);title(原图);subplot(1,3,2);J=dct2(I);imshow(log(abs(J),0 20);title(二维离散余弦变换);subplot(1,3,3);K=idct2(J)/20;imshow(K,0 20);title(二维离散反余弦变换);利用DCT变换压缩图像I=imread(19.jpg);A=rgb2gray(I);B=DCT2(A);B(abs(B)0.1)=0;C=idct2(B)/255;figure,subplot(1,3,1);i
7、mshow(A);title(原图);subplot(1,3,2);imshow(B);title(二维离散余弦变换频谱图);subplot(1,3,3);imshow(C); title(压缩后图像);实验三 图像增强(一)灰度图像增强(1)线性变换法clc;clear all;I=imread(19.jpg);A=rgb2gray(I);colormap;imshow(A);%设置图像倒数参数j=imadjust(A,0 1,1 0,1.5);figure;subimage(j) (2)灰度图像的非线性变换(之对数)I=imread(19.jpg);colormapimshow(I)J=d
8、ouble(I);J=45*log(J+1);I=uint8(J);figure,subimage(J) (二)直方图校正直方图均衡I=imread(19.jpg);B=rgb2gray(I);imshow(B,40 255);figure,imhist(B) title(直方图)J=imadjust(B,0.15 0.9,0 1);figure,imhist(B,64)title(均衡直方图)滤波I=imread(19.jpg);figure,B=rgb2gray(I);C=imnoise(B,salt D=imfilter(B,fspecial(average,3);E=medfilt2(
9、B);subplot(1,3,2)imshow(D)title(均值滤波)subplot(1,3,3)imshow(D)title(中值滤波)subplot(1,3,1)imshow(C)title(加入椒盐噪声图像)锐化处理I=imread(19.jpg);A=rgb2gray(I);figure,subplot(2,3,1),imshow(A);title(原图);hs=fspecial(sobel);S=imfilter(A,hs);hp=fspecial(prewitt);P=imfilter(A,hs);A=double(A);%双精度型H=0 1 0,1 -4 1,0 1 0;%拉
10、普拉斯算子J=conv2(A,H,same);K=A-J;subplot(2,3,2),imshow(K);title(拉普拉斯锐化图像);B=edge(A,roberts,0.1);subplot(2,3,3),imshow(B);title(罗伯特锐化图像);subplot(2,3,4),imshow(S);title(sobel算子锐化图像);subplot(2,3,5),imshow(P);title(prewitt算子锐化图像);实验四放缩A=imread(19.jpg);imshow(A);title(原图)B=imresize(A,2)figure,imshow(B);title
11、(二倍图)C=imresize(A,0.5)figureimshow(C)title(二分之一图)旋转A=imread(19.jpg);figuresubplot(1,4,1),imshow(A);title(原图像)B=imrotate(A,30,nearest);subplot(1,4,2),imshow(uint8(B);title(旋转30度图像)C=imrotate(A,45,nearest);subplot(1,4,3),imshow(uint8(C);title(旋转45度图像)D=imrotate(A,60,nearest);subplot(1,4,4),imshow(uint
12、8(D);title(旋转60度图像)镜像A1=imread(19.jpg);A1=double(A1);Figure,subplot(1,4,1),imshow(uint8(A1);H=size(A1);title(原像)A2(1:H(1),1:H(2),1:H(3)=A1(H(1):-1:1,1:H(2),1:H(3);%垂直镜像subplot(1,4,2),imshow(uint8(A2);title(垂直镜像)A3(1:H(1),1:H(2),1:H(3)=A1(1:H(1),H(2):-1:1,1:H(3);%水平镜像subplot(1,4,3),imshow(uint8(A3);t
13、itle(水平镜像)A4(1:H(1),1:H(2),1:H(3)=A1(H(1):-1:1,H(2):-1:1,1:H(3);%对角镜像subplot(1,4,4),imshow(uint8(A4);title(对角镜像)剪切A1=imread(19.jpg);A2=imcrop(A1,75 68 100 110);figuresubplot(1,2,1),imshow(A1);title(原像)subplot(1,2,2),imshow(A2);title(剪切后像)实验五阈值分割A=imread(19.jpg);figuresubplot(1,4,1),imshow(A);title(原
14、图像)B=im2bw(A,91/255);subplot(1,4,2),imshow(B);title(阈值91的图像)C=im2bw(A,71/255);subplot(1,4,3),imshow(C);title(阈值71的图像)D=im2bw(A,140/255);subplot(1,4,4),imshow(D);title(阈值140的图像)边缘检测I=imread(19.jpg);A=rgb2gray(I);figuresubplot(1,4,1),imshow(A);title(原图像)B=edge(A,sobel,0.1);%edge边缘检测函数subplot(1,4,2),imshow(B);title(sobel算子检测)C=edge(A,roberts,0.1);%0.1为门限subplot(1,4,3),imshow(C);title(roberts算子检测)D=edge(A,prewitt,0.1);subplot(1,4,4),imshow(D);title(prewitt算子检测)