matlab图像处理的几个实例.docx
《matlab图像处理的几个实例.docx》由会员分享,可在线阅读,更多相关《matlab图像处理的几个实例.docx(8页珍藏版)》请在冰豆网上搜索。
![matlab图像处理的几个实例.docx](https://file1.bdocx.com/fileroot1/2023-4/21/0e67f2b6-84de-4ae1-ba41-9bc341a5f2f7/0e67f2b6-84de-4ae1-ba41-9bc341a5f2f71.gif)
matlab图像处理的几个实例
Matlab图像处理的几个实例(初学者用)
1.图像的基本信息及其加减乘除
clear,clc;
P=imread('yjx.jpg');
whosP
Q=imread('dt.jpg');
P=im2double(P);
Q=im2double(Q);
gg1=im2bw(P,0.3);
gg2=im2bw(P,0.5);
gg3=im2bw(P,0.8);
K=imadd(gg1,gg2);
L=imsubtract(gg2,gg3);
cf=immultiply(P,Q);
sf=imdivide(Q,P);
subplot(421),imshow(P),title('郁金香原图');
subplot(422),imshow(gg1),title('0.3');
subplot(423),imshow(gg2),title('0.5');
subplot(424),imshow(gg3),title('0.8');
subplot(425),imshow(K),title('0.3+0.5');
subplot(426),imshow(L),title('0.5-0.3');
subplot(427),imshow(cf),title('P*Q');
subplot(428),imshow(sf),title('P/Q');
2.图像缩放
clear,clc;
I=imread('dt.jpg');
A=imresize(I,0.1,'nearest');
B=imresize(I,0.4,'bilinear');
C=imresize(I,0.7,'bicubic');
D=imresize(I,[100,200]);
F=imresize(I,[400,100]);
figure
subplot(321),imshow(I),title('原图');
subplot(322),imshow(A),title('最邻近插值');
subplot(323),imshow(B),title('双线性插值');
subplot(324),imshow(C),title('二次立方插值');
subplot(325),imshow(D),title('水平缩放与垂直缩放比例为2:
1');
subplot(326),imshow(F),title('水平缩放与垂直缩放比例为1:
4');
灰度变换、直方图变换
clear,clc;
fg=imread('fg.jpg');
zl=imread('zl.jpg');
hfg=rgb2gray(fg);
fg1=double(hfg);
out1=255*(fg1/255).^0.7;
out1(find(out1>255))=255;
fg1=uint8(fg1);
out1=uint8(out1);
img=rgb2gray(zl);
[harm,x]=imhist(img);
J=histeq(hfg,harm);
figure
subplot(421),imshow(fg1),title('复古灰度图');
subplot(422),imhist(fg1),title('复古灰度图的直方图');
subplot(423),imshow(out1),title('对复古灰度图像进行幂次变换');
subplot(424),imhist(out1),title('幂次变换图像的直方图');
subplot(425),imshow(img),title('朱莉');
subplot(426),imhist(img),title('朱莉图像对应的直方图');
subplot(427),imshow(J),title('直方图变换后的复古图');
subplot(428),imhist(J),title('直方图变换后的复古图对应的直方图');
傅里叶变换、频域滤波
1.傅里叶变换
clear,clc;
rgb=imread('zl.jpg');
rgb=imresize(rgb,0.7,'bilinear');
rgb=im2double(rgb);
fR=rgb(:
:
1);
fG=rgb(:
:
2);
fB=rgb(:
:
3);
flyfR=fft2(fR);
flyfG=fft2(fG);
flyfB=fft2(fB);
Frgb(:
:
1)=flyfR;
Frgb(:
:
2)=flyfG;
Frgb(:
:
3)=flyfB;
tzR=fftshift(flyfR);
tzG=fftshift(flyfG);
tzB=fftshift(flyfB);
tzF(:
:
1)=tzR;
tzF(:
:
2)=tzG;
tzF(:
:
3)=tzB;
iflyfR=ifft2(flyfR);
iflyfG=ifft2(flyfG);
iflyfB=ifft2(flyfB);
out(:
:
1)=iflyfR;
out(:
:
2)=iflyfG;
out(:
:
3)=iflyfB;
figure
subplot(221),imshow(rgb),title('原图');
subplot(222),imshow(Frgb),title('图像频谱');
subplot(223),imshow(tzF),title('调整中心后的图像频谱');
subplot(224),imshow(out),title('逆变换得到的原图');
2.频域滤波
clear,clc;
I=rgb2gray(imread('ml.jpg'));
J=imnoise(I,'gaussian',0.1);
Jzz1=medfilt2(J,[33]);
Jzz2=medfilt2(J,[1010]);
XJ=imnoise(I,'salt&pepper');
f=im2double(XJ);
g=fft2(f);
g=fftshift(g);
[M,N]=size(g);
nn=2;
d0=50;
m=fix(M/2);n=fix(M/2);
fori=1:
M
forj=1:
N
d=sqrt((i-m)^2+(j-n)^2);
h1=1/(1+0.414*(d/d0)^(2*nn));
result1(i,j)=h1*g(i,j);
end
end
result1=ifftshift(result1);
J2=ifft2(result1);
J3=im2uint8(real(J2));
figure
subplot(231),imshow(I),title('原图的灰度图像');
subplot(232),imshow(J),title('加高斯噪声');
subplot(233),imshow(Jzz1),title('模板3*3中值滤波后的图像');
subplot(234),imshow(Jzz2),title('模板10*10中值滤波后的图像');
subplot(235),imshow(J3),title('低通滤波图');
彩色图像处理
clear,clc;
rgb=imread('yjx.jpg');
fR=rgb(:
:
1);
fG=rgb(:
:
2);
fB=rgb(:
:
3);
R=rgb;
R(:
:
[23])=0;
G=rgb;
G(:
:
[13])=0;
B=rgb;
B(:
:
[12])=0;
yiq=rgb2ntsc(rgb);
fY=yiq(:
:
1);
fI=yiq(:
:
2);
fQ=yiq(:
:
3);
fR=histeq(fR,256);
fG=histeq(fG,256);
fB=histeq(fB,256);
RGB=cat(3,fR,fG,fB);
figure
subplot(341),imshow(rgb),title('原图');
subplot(342),imshow(R),title('图像的红色分量');
subplot(343),imshow(G),title('图像的绿色分量');
subplot(344),imshow(B),title('图像的蓝色分量');
subplot(345),imshow(yiq),title('NTSC彩色空间');
subplot(346),imshow(fY),title('亮度');
subplot(347),imshow(fI),title('色调');
subplot(348),imshow(fQ),title('饱和度');
subplot(349),imshow(RGB),title('rgb均衡化后的彩色图像');