0

I have written image compression code as per my professor's explanation. It works fine but for some images it show the following error:

**Error using  + 
Matrix dimensions must agree.

Error in idwt2 (line 90)
x = upsconv2(a,{Lo_R,Lo_R},sx,dwtEXTM,shift)+ ... % Approximation.

Error in nw (line 56)
DeCompImg=idwt2(cA1,c,cV1,cD1,'haar');** 

The code works fine for few images and show above error for few images. I checked the workspace. This error occurs only when the original image dimensions are odd numbers(255x255). If the image dimensions are even numbers(256x256), this code works fine. How to fix this issue without using imresize command? Please help.

My Compression Code:

%Reading an Image
I=imread('mandrill.jpg');
G=rgb2gray(I);
%Applying Haar Wavelet 

[cA1,cH1,cV1,cD1] = dwt2(G,'haar');

%Applying 2x2 window averaing 

CompImgH=blockproc(cH1, [2 2], @(x) mean(x.data(:)));
CompImgV=blockproc(cV1, [2 2], @(x) mean(x.data(:)));
CompImgD=blockproc(cD1, [2 2], @(x) mean(x.data(:)));

figure(1),
subplot(2,2,1);
imshow(G);
title('Original Image');
subplot(2,2,2);
imshow(CompImgH);
title('Horizontal Component');
subplot(2,2,3);
imshow(CompImgV);
title('Vertical Component');
subplot(2,2,4);
imshow(CompImgD);
title('Diagonal Component');

%DECOMPRESSION


%Inverse process for 2x2 window averaging 
b=CompImgH;
[m,n,colormap]=size(b);

k=1; %Counter for Row and
l=1; %Column replication
%f=input('enter replication factor: ');
f=2; % replication factor

for i=1:m %Loop for reading row and
    for t=1:f %Row replication

        for j=1:n %Loop for reading column and
            for t=1:f %Column replication

                               c(k,l)=b(i,j);
                                l=l+1;
            end
        end
        l=1;
        k=k+1;

    end
end

DeCompImg=idwt2(cA1,c,cV1,cD1,'haar');

DecompressedImage=uint8(DeCompImg);

Orig_Image = im2double(G);%---Convert image to double class
    Reconstructed_Image = im2double(DecompressedImage);%---Convert image to double class
    [M N] = size(Orig_Image);%---Size of Original Image
    err = Orig_Image - Reconstructed_Image;%---Difference between two images
    MSE = (sum(sum(err .* err)))/(M * N);
Premnath D
  • 249
  • 1
  • 3
  • 14
  • What does `dwtmode('status')` return? Also, what is `size(c)` after the loops, before `idwt2`? – chappjc Oct 24 '13 at 21:33
  • Your error occurs in `idwt2`. You are probably rounding/flooring something (maybe that you divided by 2) and so you have problems when you have odd numbers. OR you could NOT be rounding/flooring where you divide, and you should. Look for any division and make sure that you are doing the right thing when odd numbers arise. – Frederick Oct 24 '13 at 22:00
  • I tried to use the below commands to get rid of this error.. it works when M=N but not for M not equal to N. %[p q]= size(G); % if mod(p,2) && mod(q,2) % G=padarray(G,[1 1],'pre'); % else – Premnath D Oct 25 '13 at 01:20

1 Answers1

1

It appears that in function idwt2 on line 90 you are trying to add two arrays of different sizes. The only way to solve this problem is to track down its cause. You should start by setting a breakpoint at line 90, running the code, and looking at the sizes of variables.

Alternatively use dbstop if error, which will invoke the debugger automatically when you hit the error.

Dima
  • 38,860
  • 14
  • 75
  • 115