【实验目的】

1.掌握常见的图像编码、恢复和重建原理

2.掌握利用Matlab进行编程实现图像编码、恢复和重建的方法

3.观察图像编码、恢复和重建的效果

【实验内容】

1.利用Matlab对标准测试图像和自建图像进行图像编码

2.利用Matlab对标准测试图像和自建图像进行图像恢复

3.利用Matlab对标准测试图像和自建图像进行图像重建

【实验要求】

1.写出实现图像编码、恢复和重建的Matlab源代码

2.给出图像编码、恢复和重建前后的效果图

实验程序

程序1:

clear;
close all;
I=imread('F:\MatlabShijueTupian/flower.JPG');
 [m,n]=size(i);
 p1=1;s=m*n;
 for k=1:m
    for L=1:n
        f=0;
        for b=1:p1-1
            if(c(b,1)==I(k,L))f=1;break;end
        end
        if(f==0)c(p1,1)=I(k,L);p1=p1+1;end
    end
 end%上面这段程序将图像的不同颜色统计在数组c的第一列中
 for g=1:p1-1
    p(g)=0;c(g,2)=0;
    for k=1:m
        for L=1:n
            if(c(g,1)==I(k,L))p(g)=p(g)+1;end
        end
    end
    p(g)=p(g)/s;
 end %这段程序将相同颜色的像素数占图像总数的比例统计在数组p中
 pn=0;p0=1;
 while(1)
    if(pn>=1.0)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;
        tree(p0,1)=p2;tree(p0,2)=p3;
        p0=p0+1;p1=p1+1;
    end
 end%这段程序在数组p(相同颜色的像素数占图像总数的比例)中找出两个最小的概率,将它们向加在一起,然后继续进行该过程,直到两概率之和为1为止。
for k=1:p0-1
    tt=k;m1=1;
    if(or(tree(k,1)<g,tree(k,2)<g))
        if(tree(k,1)<g)
            c(tree(k,1),2)=c(tree(k,1),2)+m1;
            m2=1;
            while(tt<p0-1)
                m1=m1*2;
                for L=tt:p0-1
                    if(tree(L,1)==tt+g)
                        c(tree(k,1),2)=c(tree(k,1),2)+m1;
                        m2=m2+1;tt=1;break;
                    elseif(tree(L,2)==tt+g)
                        m2=m2+1;tt=1;break;
                    end
                end
            end
        c=(tree(k,2)<g)
        end
        tt=k;m1=1;
        if(tree(k,2)<g)
            m2=1;
            while(tt<p0-1)
                m1=m1*2;
                for L=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

程序2:

clear;
close all;
 I=checkerboard(8);
[m,n]=size(I);
 J=[];
 for i=1:m
     value=I(i,1);
     num=1;
     for j=2:n
         if I(i,j)==value
             num=num+1;
         else
             J=[J num value];
             num=1;
             value=I(i,j);
         end
     end
     J=[J num value 0 0];%添加的行判断位0 0
 end
 disp('原图像大小')
 whos('I');
 disp('压缩图像大小')
 whos('J');
 disp('图像的压缩比')
 disp(m*n/length(J))

程序3:

clear;
close all;
I=imread('F:\MatlabShijueTupian/lena.gif'); I=im2double(I);   %将无符号位图像转换为双精度类型
 T=dctmtx(8);      %离散余弦变换矩阵
 B=blkproc(I,[8 8],'P1*x*P2',T,T');
 mask=[1 1 1 1 0 0 0 0 
       1 1 1 0 0 0 0 0 
       1 1 0 0 0 0 0 0
       1 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0 ];
 B2=blkproc(B,[8 8],'P1.*x',mask);
 I2=blkproc(B2,[8 8],'P1*x*P2',T',T);
 figure(22),subplot(1,2,1),imshow(I);
 title('原始图像')
 subplot(1,2,2),imshow(I2);
 title('压缩图像')

程序4:

clear;
close all;
i=imread('F:\MatlabShijueTupian/5.jpg');
I=rgb2gray(i); %将无符号位图像转换为双精度类型
 T=dctmtx(8);      %离散余弦变换矩阵
 B=blkproc(I,[8 8],'P1*x*P2',T,T');
 mask=[1 1 1 1 0 0 0 0 
       1 1 1 0 0 0 0 0 
       1 1 0 0 0 0 0 0
       1 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0 ];
 B2=blkproc(B,[8 8],'P1.*x',mask);
 I2=blkproc(B2,[8 8],'P1*x*P2',T',T);
 figure(22),subplot(1,2,1),imshow(I);
 title('原始图像')
 subplot(1,2,2),imshow(I2);
 title('压缩图像')

程序5:

clear;
close all;
I=checkerboard(8); %创建一个棋盘图像
 noise=0.1*randn(size(I));%产生随机噪声
 PSF=fspecial('motion',21,11);
 Blurred=imfilter(I,PSF,'circular');%输入图像滤波,使图像变模糊
 BlurredNoisy=im2uint8(Blurred+noise);%添加噪声
NP=abs(fftn(noise)).^2;%噪声功率
 NPOW=sum(NP(:))/prod(size(noise));
 NCORR=fftshift(real(ifftn(NP)));%噪声的自相关
 IP=abs(fftn(I)).^2;%原始图像功率
 IPOW=sum(IP(:))/prod(size(I));
 ICORR=fftshift(real(ifftn(IP)));%图像的自相关
 ICORR1=ICORR(:,ceil(size(I,1)/2));
 NSR=NPOW/IPOW;%噪信比
 figure(17),subplot(2,2,1);imshow(BlurredNoisy,[]);
 title('模糊和噪声图像');
subplot(2,2,2);
imshow(deconvwnr(BlurredNoisy,PSF,NSR),[]);
 title('deoconvwnr(A,PSF,NSR)');
subplot(2,2,3);
imshow(deconvwnr(BlurredNoisy,PSF,NCORR,ICORR),[]);
title('deconvwnr(A,PSF,NCORR,ICORR)');
subplot(2,2,4);
imshow(deconvwnr(BlurredNoisy,PSF,NPOW,ICORR1),[]);
 title('deconvwnr(A,PSF,NPOW,ICORR-1-D)');

结果图:
在这里插入图片描述
在这里插入图片描述

本人能力有限,解释尚不清楚明了,如遇任何问题,大家可留言或私信。因使用私人图片进行实验,结果图不便上传,望理解。后续将程序文件打包上传,供大家学习使用。

本文希望对大家有帮助,当然上文若有不妥之处,欢迎指正。

分享决定高度,学习拉开差距