㈠ 圖像壓縮 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:畫出每次迭代的解碼圖象