Premnath D
Premnath D

Reputation: 249

MATLAB- Matrix dimensions must agree

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);

Upvotes: 0

Views: 891

Answers (1)

Dima
Dima

Reputation: 39389

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.

Upvotes: 1

Related Questions