matlAB图像处理程序与实操II.docx
- 文档编号:14353389
- 上传时间:2023-06-22
- 格式:DOCX
- 页数:90
- 大小:3.98MB
matlAB图像处理程序与实操II.docx
《matlAB图像处理程序与实操II.docx》由会员分享,可在线阅读,更多相关《matlAB图像处理程序与实操II.docx(90页珍藏版)》请在冰点文库上搜索。
matlAB图像处理程序与实操II
%平滑
%分别显示RGB图像的红色分量、绿色分量、蓝色分量
%将RGB图像转换成HSI图像,并显示HSI图像的H分量、S分量、以及I分量
%对RGB图像的红色分量、绿色分量和蓝色分量进行平滑,并显示平滑后的RGB图像
%只对HSI图像的I分量进行平滑,并显示平滑后的RGB图像
%对HSI的H分量、S分量和I分量进行平滑,并显示平滑后的RGB图像
functionpinghua
clearall;
clc;
image_rgb=imread('lenna.bmp');
figure
(1);
subplot(2,2,1);
imshow(image_rgb);
title('原始图像');
image_rgb_r=image_rgb(:
:
1);
subplot(2,2,2);
imshow(image_rgb_r);
title('红色分量');
image_rgb_g=image_rgb(:
:
2);
subplot(2,2,3);
imshow(image_rgb_g);
title('绿色分量');
image_rgb_b=image_rgb(:
:
3);
subplot(2,2,4);
imshow(image_rgb_b);
title('蓝色分量');
image_hsi=rgb2hsi(image_rgb);
figure
(2);
subplot(2,2,1);
imshow(image_hsi);
title('HSI图像');
image_hsi_h=image_hsi(:
:
1);
subplot(2,2,2);
imshow(image_hsi_h);
image_hsi_s=image_hsi(:
:
2);
subplot(2,2,3);
imshow(image_hsi_s);
image_hsi_i=image_hsi(:
:
3);
subplot(2,2,4);
imshow(image_hsi_i);
w=fspecial('average',25);%产生平均滤波器
image_rgb_filtered=imfilter(image_rgb,w,'replicate');%执行滤波
figure(3);
subplot(2,2,1);
imshow(image_rgb_filtered);
title('平滑后的RGB图像');
hsi_I_filtered=imfilter(image_hsi_i,w,'replicate');%只对I分量进行滤波
hsi_image=cat(3,image_hsi_h,image_hsi_s,hsi_I_filtered);%将分量组合成图像
rgb_image=hsi2rgb(hsi_image);
subplot(2,2,2);
imshow(rgb_image);
title('平滑I分量后的RGB图像');
hsi_filtered=imfilter(image_hsi,w,'replicate');%只对I分量进行滤波
rgb_image=hsi2rgb(hsi_filtered);
subplot(2,2,3);
imshow(rgb_image);
title('平滑HSI分量后的RGB图像');
functionhsi=rgb2hsi(rgb)
rgb=im2double(rgb);
r=rgb(:
:
1);
g=rgb(:
:
2);
b=rgb(:
:
3);
num=0.5*((r-g)+(r-b));%公式
den=sqrt((r-g).^2+(r-b).*(g-b));
theta=acos(num./(den+eps));
H=theta;
H(b>g)=2*pi-H(b>g);
H=H/(2*pi);
num=min(min(r,g),b);
den=r+g+b;
den(den==0)=eps;
S=1-3.*num./den;
H(S==0)=0;
I=(r+g+b)/3;
hsi=cat(3,H,S,I);%将H,S,I三个分量组成HSI图像
functionrgb=hsi2rgb(hsi)
H=hsi(:
:
1)*2*pi;%提取HIS图像的H,S,I分量
S=hsi(:
:
2);
I=hsi(:
:
3);
R=zeros(size(hsi,1),size(hsi,2));%转换方程的实现过程
G=zeros(size(hsi,1),size(hsi,2));
B=zeros(size(hsi,1),size(hsi,2));
idx=find((0<=H)&(H<2*pi/3));
B(idx)=I(idx).*(1-S(idx));
R(idx)=I(idx).*(1+S(idx).*cos(H(idx))./cos(pi/3-H(idx)));
G(idx)=3*I(idx)-(R(idx)+B(idx));
idx=find((2*pi/3<=H)&(H<4*pi/3));
R(idx)=I(idx).*(1-S(idx));
G(idx)=I(idx).*(1+S(idx).*cos(H(idx)-2*pi/3)./cos(pi-H(idx)));
B(idx)=3*I(idx)-(R(idx)+G(idx));
idx=find((4*pi/3<=H)&(H<=2*pi));
G(idx)=I(idx).*(1-S(idx));
B(idx)=I(idx).*(1+S(idx).*cos(H(idx)-4*pi/3)./cos(5*pi/3-H(idx)));
R(idx)=3*I(idx)-(G(idx)+B(idx));
rgb=cat(3,R,G,B);
rgb=max(min(rgb,1),0);%将R,G,B三个分量组成RGB图像
%彩色图像的锐化
clearall;
clc;
image_rgb=imread('saturn.png');
lapmatrix=[111;1-81;111];
rgb_filtered=imfilter(image_rgb,lapmatrix,'replicate');
subplot(2,2,1);
imshow(image_rgb);
title('原图像');
subplot(2,2,2);
imshow(rgb_filtered);
title('图像边缘锐化');
rgb_sharp=double(image_rgb)-double(rgb_filtered);
subplot(2,2,3);
imshow(uint8(rgb_sharp));
title('锐化后的图像');
%灰度分层法的伪彩色处理
clearall;
clc;
image_rgb=imread('lenna.bmp');
image_rgb_1=rgb2gray(image_rgb);
subplot(1,2,1);
imshow(image_rgb);
title('原图像');
image_rgb_2=grayslice(image_rgb_1,16);
subplot(1,2,2);
imshow(image_rgb_2,hot(16));
title('处理后的图像');
%灰度变换法的伪彩色处理
clearall;
clc;
image=imread('cameraman.tif');
subplot(1,2,1);
imshow(image);
title('原始图像');
image=double(image);
[M,N]=size(image);
L=256;
fori=1:
M
forj=1:
N
ifimage(i,j)<=L/4%像素值在0~64时的处理方法
R(i,j)=0;
G(i,j)=L;
B(i,j)=L;
elseifimage(i,j)<=L/2%像素值在64~128时的处理方法
R(i,j)=0;
G(i,j)=L;
B(i,j)=-4*image(i,j)+2*L;
elseifimage(i,j)<=3*L/4%像素值在128~192时的处理方法
R(i,j)=4*image(i,j)-2*L;
G(i,j)=L;
B(i,j)=0;
else%像素值在192~256时的处理方法
R(i,j)=L;
G(i,j)=-4*image(i,j)+4*L;
B(i,j)=0;
end
end
end
fori=1:
M
forj=1:
N
image_out(i,j,1)=R(i,j);%红绿蓝分量分别赋值
image_out(i,j,2)=G(i,j);
image_out(i,j,3)=B(i,j);
end
end
image_out=image_out/256;
subplot(1,2,2);
imshow(image_out);
title('处理后的图像')
%频域伪彩色
clearall;
clc;
image=imread('cameraman.tif');
subplot(1,2,1);
imshow(image);
title('原始图像');
image=double(image);
[M,N]=size(image);
F=fft2(image);%二维傅里叶变换
fftshift(F);
REDcut=100;
GREENcut=200;
BLUEcenter=150;
BLUEwidth=100;
BLUEu0=10;
BLUEv0=10;
foru=1:
M
forv=1:
N
D(u,v)=sqrt(u^2+v^2);
REDH(u,v)=1/(1+(sqrt
(2)-1)*(D(u,v)/REDcut)^2);
GREENH(u,v)=1/(1+(sqrt
(2)-1)*(GREENcut/D(u,v))^2);
BLUED(u,v)=sqrt((u-BLUEu0)^2+(v-BLUEv0)^2);
BLUEH(u,v)=1-1/(1+BLUED(u,v)*BLUEwidth/((BLUED(u,v))^2-(BLUEcenter)^2)^2);
end
end
RED=REDH.*F;
REDcolor=ifft2(RED);
GREEN=GREENH.*F;
GREENcolor=ifft2(GREEN);
BLUE=BLUEH.*F;
BLUEcolor=ifft2(BLUE);
REDcolor=real(REDcolor)/256;
GREENcolor=real(GREENcolor)/256;
BLUEcolor=real(BLUEcolor)/256;
fori=1:
M
forj=1:
N
image_out(i,j,1)=REDcolor(i,j);
image_out(i,j,2)=GREENcolor(i,j);
image_out(i,j,3)=BLUEcolor(i,j);
end
end
image_out=abs(image_out);
subplot(1,2,2);
imshow(image_out);
title('处理后的图像');
%假彩色处理
clearall;
clc;
image_rgb=imread('lenna.bmp');
subplot(1,2,1);
imshow(image_rgb);
title('原始图像');
image_rgb_new(:
:
1)=image_rgb(:
:
3);
image_rgb_new(:
:
2)=image_rgb(:
:
3);
image_rgb_new(:
:
3)=image_rgb(:
:
2);
subplot(1,2,2);
imshow(image_rgb_new);
title('处理后的图像');
6图像压缩
6.1哈夫曼编码
%图像压缩——哈夫曼编码
clc;
clearall;
A=[
44444440
45555540
45666540
45676540
45555540
44444440
44444440
];
[m,n]=size(A);
p1=1;
s=m*n;
fork=1:
m
forL=1:
n
f=0;
forb=1:
p1-1
if(c(b,1)==A(k,L))%将该矩阵中所有不同的数据统计在c的第一列中
f=1;
break;
end
end
if(f==0)
c(p1,1)=A(k,L);
p1=p1+1;
end
end
end
forg=1:
p1-1%将相同的数据出现的概率统计在数组p中
p(g)=0;
c(g,2)=0;
fork=1:
m
forL=1:
n
if(c(g,1)==A(k,L))
p(g)=p(g)+1;
end
end
end
p(g)=p(g)/s;
end
p11=p;
%以下这段程序是为了实现哈夫曼编程的前三个步骤,并将每次所得到的最小的和次小的概率分别存放在数组tree的第一列和第二列中
pn=0;
p0=1;
while
(1)
if(pn>=1.0)%当概率之和大于或等于1时,就跳出循环
break;
else
[pm,p2]=min(p(1:
p1-1));%求得最小概率
p(p2)=1.1;
[pm2,p3]=min(p(1:
p1-1));%求得次小的概率
p(p3)=1.1;
pn=pm+pm2;%求出两个最小概率之和
p(p1)=pn;%将求和后的新概率放在数组p中
tree(p0,1)=p2;
tree(p0,2)=p3;
p0=p0+1;
p1=p1+1;
end
end
%以下这段程序是为了实现哈夫曼编码的最后一个步骤,并将结果存放在数组c中,数组c的第一列存放的是矩阵中所出现的不同的数值(0、4、5、6、7),
%第二列存放的是码字的十进制数值大小,第三列存放的是字码的长度
fork=1:
p0-1%四次循环
tt=k;
m1=1;
if(or(tree(k,1)<=g,tree(k,2)<=g))%该if语句用来求除了第二小概率的其他所有码字
if(tree(k,1)<=g)
c(tree(k,1),2)=c(tree(k,1),2)+m1;
m2=1;
while(tt m1=m1*2; forL=tt: p0-1 if(tree(L,1)==tt+g) c(tree(k,1),2)=c(tree(k,1),2)+m1;%求除了第二小概率其他码字的十进制值 m2=m2+1; tt=L; break; elseif(tree(L,2)==tt+g) m2=m2+1; tt=L; break; end end end c(tree(k,1),3)=m2; end tt=k;%这个if语句用来求第二小概率码字的数值和长度 m1=1; if(tree(k,2) m2=1; while(tt m1=m1*2; forL=tt: p0-1 if(tree(L,1)==tt+g) c(tree(k,2),2)=c(tree(k,2),2)+m1;%得到第二小概率码字的十进制数值 m2=m2+1; tt=L; break; elseif(tree(L,2)==tt+g) m2=m2+1; tt=L; break; end end end c(tree(k,2),3)=m2; end end end %以下这段程序是显示哈夫曼编码结果,并 求出信息源熵,平均码长,编码效率以及冗余度 [M,N]=size(c); disp('哈夫曼编码结果')%显示编码结果 A1=dec2bin(c(1,2),c(1,3))%将十进制数转换成二进制数 A2=dec2bin(c(2,2),c(2,3)) A3=dec2bin(c(3,2),c(3,3)) A4=dec2bin(c(4,2),c(4,3)) A5=dec2bin(c(5,2),c(5,3)) form=1: M if(p11(m)~=0) H(m)=-p11(m)*log2(p11(m));%求信息源熵 end end disp('信息源熵');H1=sum(H) NN=0; fori=1: M NN=NN+p11(1,i)*c(i,3); end disp('平均码长');NN disp('编码效率');yita=H1/(NN*log2 (2)) disp('冗余度');Rd=1-yita %游程编码 clearall; clc; image=imread('cameraman.tif'); subplot(1,2,1); imshow(image); title('原始图像'); thresh=graythresh(image);%设置黑白阈值 image_bw=im2bw(image,thresh);%将图像转换成黑白图像 subplot(1,2,2); imshow(image_bw); title('二值化图像'); a=image_bw'; a=a(: ); a=a'; a=double(a); rle (1)=a (1); m=2; rle(m)=1; fori=1: length(a)-1 ifa(i)==a(i+1) rle(m)=rle(m)+1; else m=m+1; rle(m)=1; end end display(rle); %算术编码 functionsuanshubianma clearall; clc formatlonge; sign=['abcd']; p=[0.10.40.20.3]; insert_sign=['cadacdb']; code_result=arithmeticode(sign,p,insert_sign)%编码 output=arithmetidecode(sign,p,code_result,length(insert_sign))%解码 functionresult=arithmeticode(sign,p,insert_sign) high_range=[]; fori=1: length(p) high_range=[high_rangesum(p(1: i))]; end low_range=[0high_range(1: length(p-1))]; idx=zeros(size(insert_sign)); fori=1: length(insert_sign) idx(i)=find(sign==insert_sign(i)); end low=0; high=1; fori=1: length(insert_sign) range=high-low; high=low+range*high_range(idx(i)); low=low+range*low_range(idx(i)); end result=low; functionsigns=arithmetidecode(sign,p,code_result,sign_length) formatlonge; high_range=[]; fork=1: length(p) high_range=[high_rangesum(p(1: k))]; end low_range=[0high_range(1: length(p)-1)]; pmin=min(p); signs=[]; fori=1: sign_length idx=max(find(low_range<=code_result)); code_result=code_result-low_
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- matlAB 图像 处理 程序 II
![提示](https://static.bingdoc.com/images/bang_tan.gif)