该程序旨在对lena图像执行DWT / IDWT和EZW编码/解码。当我打算用压缩比和均方误差信息指定重建图像时,问题出现在最后。
clc
clear all;
close all;
T_min=32;
I=imread('lena.jpg');
infilename = 'lena.jpg'
figure(1)
subplot(2,1,1)
imshow(I);
title('original Image');
img_orig = double(imread(infilename));
dim=size(I,1);
[LL1,LH1,HL1,HH1] = dwt2(I,'haar');
[LL2,LH2,HL2,HH2] = dwt2(LL1,'haar');
[LL3,LH3,HL3,HH3] = dwt2(LL2,'haar');
I5=[LL3,LH3;HL3,HH3];
I6=[I5,LH2;HL2,HH2];
I7=[I6,LH1;HL1,HH1];
max_coef = max(max(abs(I7)))
T0 = 2^floor(log2(max_coef))
d=1;
s1=1;
sym_count=0
snb=0
ref=1;
dim=size(I,1);
count=1
symbol(1:dim,1:dim)='S';
while T0>=T_min
for i= 1:size(I,1)
for j=1:size(I,2)
if(abs(I(i,j))<T0)
dom(d,:)=[i,j];
d=d+1;
mask = masking(i,j,dim);
I1=double(I).*mask;
p1=0;
for i1=i:dim
for j1=j:dim
if (I1(i1,j1)>=T0)
p1=1;
end
end
end
if (p1==1)
symbol(i,j)='I'; %% I is isolated zero
sym_count=sym_count+1;
else
symbol(i,j)='Z'; %% Z is zerotree root
sym_count=sym_count+1;
end
else
if(I(i,j)<0)
symbol(i,j)='N'; %% N is negative
sym_count=sym_count+1;
snb=snb+1;
if (I(i,j)<=(-T0-T0/2))
refine(ref)= 1;
ref=ref+1;
else
refine(ref)= 0;
ref=ref+1;
end
else
symbol(i,j)='P'; %% P is positive
sym_count=sym_count+1;
snb=snb+1;
if (I(i,j)>=(T0+T0/2))
refine(ref)= 1;
ref=ref+1;
else
refine(ref)= 0;
ref=ref+1;
end
end
sub(s1)=I(i,j);
s1=s1+1;
end
end
end
for i=1:dim
for j=1:dim
if symbol(i,j)=='Z'
mask1=masking(i,j,dim);
for i1=i:dim
for j1=j:dim
if(mask1(i1,j1)==1)
symbol(i1,j1)='D';
end
end
end
symbol(i,j)='Z';
end
end
end
%decoding the from the significant bits (P and N)
rec=zeros(dim);
ref1=1;
for i=1:dim
for j=1:dim
if symbol(i,j)=='P'
if (refine(ref1)==1)
% rec(i,j)=T0+T0/2+T0/4;
rec(i,j)=I7(i,j);
ref1=ref1+1;
else
% rec(i,j)=T0+T0/2-T0/4;
rec(i,j)=I7(i,j);
ref1=ref1+1;
end
elseif (symbol(i,j)=='N')
if (refine(ref1)==1)
% rec(i,j)=-T0-T0/2-T0/4;
rec(i,j)=I7(i,j);
ref1=ref1+1;
else
% rec(i,j)=-T0-T0/2+T0/4;
rec(i,j)=I7(i,j);
ref1=ref1+1;
end
end
end
end
I8=rec;
% reassignment for inverse DWT.
r_LL3=I8(1:dim/8,1:dim/8);
r_LH3=I8(1:dim/8,dim/8+1:2*dim/8);
r_HL3=I8(dim/8+1:2*dim/8,1:dim/8);
r_HH3=I8(dim/8+1:2*dim/8,dim/8+1:2*dim/8);
r_LL2=[r_LL3,r_LH3;r_HL3,r_HH3];
r_LH2=I8(1:dim/4,dim/4+1:2*dim/4);
r_HL2=I8(dim/4+1:2*dim/4,1:dim/4);
r_HH2=I8(dim/4+1:2*dim/4,dim/4+1:2*dim/4);
r_LL1=[r_LL2,r_LH2;r_HL2,r_HH2];
r_LH1=I8(1:dim/2,dim/2+1:2*dim/2);
r_HL1=I8(dim/2+1:2*dim/2,1:dim/2);
r_HH1=I8(dim/2+1:2*dim/2,dim/2+1:2*dim/2);
I9=[r_LL1,r_LH1;r_HL1,r_HH1];
% inverse discrete wavelet transform
r_LL2=idwt2(r_LL3,r_LH3,r_HL3,r_HH3,'haar');
r_LL1=idwt2(r_LL2,r_LH2,r_HL2,r_HH2,'haar');
img_reconstruct=idwt2(r_LL1,r_LH1,r_HL1,r_HH1,'haar');
T0=T0/2
count=count+1
image_capacity=img_orig*8
MSE = sum(sum((img_reconstruct-img_orig).^2))/size(img_orig,1)/size(img_orig,2)
CR=(image_capacity/(2*sym_count+snb))
figure(2)
subplot(3,3,count)
imshow(uint8(img_reconstruct))
title(['reconstructed Image', num2str(count)]);
% xlabel(['MSE = ',num2str(MSE,'%3.2f'),'CR', num2str(CR,'%1.2f %%')])
xlabel({['Compression Ratio: ' num2str(CR,'%1.2f %%')],['MSE: ' num2str(MSE,'%3.2f')]})
end
代码的麻烦部分如下所示
xlabel({['Compression Ratio: ' num2str(CR,'%1.2f %%')],['MSE: ' num2str(MSE,'%3.2f')]})
我得到的错误是
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
我打算有一个与此几乎相似的情节:
我在这里做错了什么?
答案 0 :(得分:0)
我想我已经发现这里出了什么问题。我使用CR作为矩阵,但它应该是一个数字。我只需要进行以下更改。
T0=T0/2
count=count+1
image_capacity=dim*dim*8
MSE = sum(sum((img_reconstruct-img_orig).^2))/size(img_orig,1)/size(img_orig,2)
CR=(image_capacity/(2*sym_count+snb))