‘壹’ 图像压缩 MATLAB实现 如何解压缩一个被压缩过的图像 急急急
法对图像进行压缩,得到一组压缩编码,然后解码这组编码,得到一幅解压缩图像.对解码图像与原始图像求差值,得到一差值图像,然后对该差值图像进行适当的编码.对差值图像的编码与初级编码共同构成对原始图像的编码.这种方法需要选择合适的初级编码方法与差值编码方法,使得这两者相结合,可以得到一种综合性能较好的编码方法。
其定义如图。
%%%%%%%%%%%%%%
clear
tic
%Image1=imread('pic\cameraman.tif');
xianshi;
number=input(' input the number:');
Image1=suoxiao('pic\cameraman.tif',number);
[imagem imagen]=size(Image1);
Sr=4;Sd=8;
Rnum=(imagem/Sr)*(imagen/Sr);
Dnum=(imagem/Sd)*(imagen/Sd);
Image2=zeros(Dnum,Sr,Sr);
Image2=blkproc(Image1,[Sd/Sr,Sd/Sr],'mean(mean(x))');
%压缩image1为原来1/2
% there are no eight tranformation for simpleness
RBlocks=zeros(Rnum,Sr,Sr);
DBlocks=zeros(Dnum,Sd,Sd);
DBlocksRece=zeros(Dnum*8,Sr,Sr);
%%取R块,K记标号----------------------------------
for i=1:imagem/Sr
for j=1:imagen/Sr
k=(i-1)*imagen/Sr+j;
RBlocks(k,:,:)=Image1((i-1)*Sr+1:i*Sr,(j-1)*Sr+1:j*Sr);
end
end
%取R块,K记标号----------------------------------
for i=1:imagem/Sd
for j=1:imagen/Sd
k=(i-1)*imagen/Sd+j;
m=Sr;n=Sr;
DBlocksRece(k,:,:)=Image2((i-1)*Sr+1:i*Sr,(j-1)*Sr+1:j*Sr);
DBlocksRece(k+Dnum,:,:)=DBlocksRece(k,m:-1:1,:); % 行上下翻转===(x轴对称)
DBlocksRece(k+2*Dnum,:,:)=DBlocksRece(k,:,n:-1:1); % 列左右翻转 ==== y轴对称
DBlocksRece(k+3*Dnum,:,:)=DBlocksRece(k,m:-1:1,n:-1:1); % 先行翻,再列翻 旋转180度
DBlocksRece(k+4*Dnum,:,:)=reshape(DBlocksRece(k,:,:),Sr,Sr)'; % 关于y=-x对称
A=reshape( DBlocksRece(k+3*Dnum,:,:),Sr,Sr)';
DBlocksRece(k+5*Dnum,:,:)=A(:,n:-1:1); % 关于y=x对称
DBlocksRece(k+6*Dnum,:,:)=imrotate(reshape(DBlocksRece(k,:,:),Sr,Sr),90); % 逆时针旋转90度
DBlocksRece(k+7*Dnum,:,:)=imrotate(reshape(DBlocksRece(k,:,:),Sr,Sr),270); % 逆时针旋转270度
DBlocks(k,:,:)=Image1((i-1)*Sd+1:i*Sd,(j-1)*Sd+1:j*Sd);
end
end
RandDbest=zeros(Rnum,1)+256^3;
RandDbests=zeros(Rnum,1);
RandDbesto=zeros(Rnum,1);
RandDbestj=zeros(Rnum,1);
for i=1:Rnum
x=reshape(RBlocks(i,:,:),Sr*Sr,1);
meanx=mean(x);
for j=1:Dnum*8
y=reshape(DBlocksRece(j,:,:),Sr*Sr,1);
meany=mean(y);
s=(x-meanx)'*(y-meany)/((y-meany)'*(y-meany));%计算s
o=(meanx-s*meany);%计算o
c=(x-s*y-o)'*(x-s*y-o);%距离
if (RandDbest(i)>c)&(abs(s)<1)
RandDbest(i)=c;
RandDbests(i)=s;
RandDbesto(i)=o;
RandDbestj(i)=j;%可以找到对应变换和D块
end
end
end
%iteration limit
toc
tic
m=8;%解码迭代次数
e=mean(mean(Image1));
Image3=e*ones(imagem,imagen);%解码初始图象
for L=1:m
Image4=blkproc(Image3,[Sd/Sr,Sd/Sr],'mean(mean(x))');
for i=1:imagem/Sr
for j=1:imagen/Sr
m=Sr;n=Sr;
k=(i-1)*imagen/Sr+j;
l=RandDbestj(k);
k1=mod(l-1,Dnum)+1;%第几个D
l1=(l-k1)/Dnum+1;%变换号
%R对应D在Image4的起始点
j1=mod(k1-1,imagen/Sd)+1;
i1=(k1-j1)/(imagen/Sd)+1;
%变换------------------------------------------------------------------------
DBlocksRece(k1,:,:)=Image4((i1-1)*Sr+1:i1*Sr,(j1-1)*Sr+1:j1*Sr);
switch l1-1
case 0
DBlocksRece(l,:,:)=Image4((i1-1)*Sr+1:i1*Sr,(j1-1)*Sr+1:j1*Sr);
case 1
DBlocksRece(l,:,:)=DBlocksRece(k1,m:-1:1,:);
case 2
DBlocksRece(l,:,:)=DBlocksRece(k1,:,n:-1:1);
case 3
DBlocksRece(l,:,:)=DBlocksRece(k1,m:-1:1,n:-1:1);
case 4
DBlocksRece(l,:,:)=reshape(DBlocksRece(k1,:,:),Sr,Sr)';
case 5
DBlocksRece(k1+3*Dnum,:,:)=DBlocksRece(k1,m:-1:1,n:-1:1);
A=reshape( DBlocksRece(k1+3*Dnum,:,:),Sr,Sr)';
DBlocksRece(l,:,:)=A(:,n:-1:1);
case 6
DBlocksRece(l,:,:)=imrotate(reshape(DBlocksRece(k1,:,:),Sr,Sr),90);
case 7
DBlocksRece(l,:,:)=imrotate(reshape(DBlocksRece(k1,:,:),Sr,Sr),270);
end
%变换结束--------------------------------------------------------------------
RBlocks(k,:,:)=RandDbests(k)*DBlocksRece(l,:,:)+RandDbesto(k);
%生成R---------------------------
Image3((i-1)*Sr+1:i*Sr,(j-1)*Sr+1:j*Sr)=reshape(RBlocks(k,:,:),Sr,Sr);%更新迭代图象
end
end
wucha=double(Image1)-Image3;%误差图
Ps1(L)=20*log10(255/(sqrt(mean(mean(wucha.^2)))))
PSNR=psnr(wucha)
figure
imshow(uint8(Image3))
end
toc
figure
wucha=uint8(wucha);
imshow(wucha)
figure
imshow(uint8(Image1)),title('原图');
save('sa.mat')
fangtu(wucha);%%%%分形主函数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
子函数:1:
function b=suoxiao(filename,bili)
a=imread(filename);
a=double(a);
[m,n]=size(a);
i=1;
while i=m/bili
j=1;
while j=n/bili
k=mean(mean(a(bili*(i-1)+1:bili*(i-1)+bili,bili*(j-1)+1:bili*(j-1)+bili)));
b(i,j)=k;
j=j+1;
end
i=i+1;
end
%b=uint8(b);
size(b)
%imshow(b)
子函数2:
%clc
function fangtu(a)
J=a;
%计算灰度图象的直方图数据,a为如象数组
L=256; %灰度级
Ps = zeros(L,1); %统计直方图结果数据
nk=zeros(L,1);
[row,col]=size(a);
n=row*col; %总像素个数
for i = 1:row
for j = 1:col
num = double(a(i,j))+1; %获取像素点灰度级
nk(num) = nk(num)+1; %统计nk
end
end
%计算直方图概率估计
for i=1:L
Ps(i)=nk(i)/n;
end
figure;
subplot(3,1,1);imshow(J),title('误差图');
subplot(3,1,2),plot(nk),title('直方图(nk)');
subplot(3,1,3),plot(Ps),title('直方图(Ps)');
子函数3:
function PSNR=psnr(a)
[m,n]=size(a);
a=uint8(a);
a=double(a);
imagesize=m*n;
MSE=sum(dot(a,a))/ imagesize;
PSNR=10*log10(255^2/MSE);
%%%%%%%%%%%%%%%%%%%%%%
说明:
1、因为本程序时间长,FX中先选择图片的大小
2、编码与解码
3、做误差图和只方图
4:画出每次迭代的解码图象
‘贰’ 如何用MATLAB进行图像压缩
1、首先在电脑中双击matlab软件,使用语句:x=0:0.2:7*pi:创建一个一维数组,表示三维离散序列图的在x轴上的分布范围。
‘叁’ 如何用MATLAB进行图像压缩
I
=
imread('cameraman.tif');
%
输入图像
I
=
im2double(I);
%
数据类型转换
T
=
dctmtx(8);
%
计算二维离散DCT矩阵
dct
=
@(x)T
*
x
*
T';
%
设置函数句柄
B
=
blkproc(I,[8
8],dct);
%
图像块处理
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],@(x)mask.*
x);
%
图像块处理
invdct
=
@(x)T'
*
x
*
T;
%
设置函数句柄
I2
=
blkproc(B2,[8
8],invdct);
%
图像块处理
imshow(I),
figure,
imshow(I2)
%
显示原始图像和压缩重构图像
‘肆’ 压缩感知中 稀疏基有很多种 怎么用matlab表示
‘伍’ matlab关于压缩感知的峰值信噪比,运行时间,相对误差,重构概率的定义或资料
一种常用的峰值均方误差PMSE:
式中,A为 的最大值。实用中还常采用简单的形式 。此时,对于8比特精度的图像,A=255,M、N为图像尺寸。
峰值均方误差PMSE也被表示成等效的峰值信噪比PSNR: