例6.9灰度级-彩色变换
I=imread('lz.bmp'); imshow(I); I=double(I); [M,N]=size(I); L=256; for i=1:M for j=1:N
if I(i,j) G(i,j)=4*I(i,j); B(i,j)=L; else if I(i,j)<=L/2 R(i,j)=0; G(i,j)=L; B(i,j)=-4*I(i,j)+2*L; else if I(i,j)<=3*L/4; R(i,j)=4*I(i,j)-2*L; G(i,j)=L; B(i,j)=0; else R(i,j)=L; G(i,j)=-4*I(i,j)+4*L; B(i,j)=0; end end end end end for i=1:M for j=1:N G2C(i,j,1)=R(i,j); G2C(i,j,2)=G(i,j); G2C(i,j,3)=B(i,j); end end G2C=G2C/256; figure imshow(G2C); clc 7.4计算图像的熵 function h=entropy(x,n) error(nargchk(1,2,nargin)); if nargin<2 n=256; end x=double(x); xh=hist(x(:),n); xh=xh/sum(xh(:)); i=find(xh); h=-sum(xh(i).*log(xh(i))); 7.10下面对大小为512*512像素、灰度级为256的标准Lena图像进行无损的一阶预测码 X=imread('cameraman.tif','tif'); X=double(X); Y=LPCencode(X); XX=LPCdecode(Y); figure(1); imshow(mat2gray(Y)); e=double(X)-double(XX); [m,n]=size(e); erms=sqrt(sum(e(:).^2)/(m*n)) figure(2); [h,x]=hist(X(:)); subplot(121);bar(x,h,'k'); [h,x]=hist(Y(:)); subplot(122);bar(x,h,'k'); 7.11计算经编码后的误差 X=[14 15 14 15 13 15 15 14 20 26 27 28 27 27 29 37 47 62 75 77 78 79 80 81 81 82 82] t=entropy(X) a=1;delt=6.5; y=DMencode(X,a,delt); XX=DMdecode(y); ax=[0:1:26] [X2,Y2]=STAIRS(ax,XX) plot(X2,Y2,'k',ax,X,'k'); 7.12对大小为256*256像素、灰度级为256的标准woman图像运用德尔塔调制方法进行编码 load woman figure(1); imshow(mat2gray(X)); t=entropy(X) a=1;delt=6.5; y=DMencode(X,a,delt); figure(2); imshow(mat2gray(y)); tt=entropy(y) XX=DMdecode(y); figure(3); imshow(mat2gray(XX)); erms=compare(X,XX,1); 7.14进行FFT实现图像数据的压缩,我们保留32个系数,实现2:1的数据压缩,然后进行逆变换 cr=0.5; I1=imread('zhx2.jpg'); I1=double(I1)/255; figure(1); imshow(I1); fftcoe=blkproc(I1,[8 8],'fft2(x)'); coevar=im2col(fftcoe,[8 8],'distinct'); coe=coevar; [y,ind]=sort(coevar); [m,n]=size(coevar); snum=64-64*cr; for i=1:n coe(ind(1:snum),i)=0; end B2=col2im(coe,[8 8],[256 256],'distinct'); I2=blkproc(B2,[8 8],'ifft2(x)'); figure(2); imshow(I2); e=double(I1)-double(I2); [m,n]=size(e); erms=sqrt(sum(e(:).^2)/(m*n)) 7.16先将图像分割成8*8子图像,进行哈达玛变换,保留32个系数,进行2:1压缩。 cr=0.5; I1=imread('zhx1.jpg'); I1=double(I1)/255; figure(1); imshow(I1); T=hadamard(8);