0
我已經寫了圖像壓縮代碼,按照我的教授的解釋。它工作正常,但對於一些圖像表明發生了以下錯誤:MATLAB-矩陣尺寸必須一致
**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');**
代碼工作正常幾張圖片,並顯示上述錯誤的幾張圖片。我檢查了工作區。僅當原始圖像尺寸爲奇數(255x255)時纔會出現此錯誤。如果圖像尺寸是偶數(256x256),則此代碼正常工作。如何解決這個問題without using imresize
命令?請幫忙。
我的壓縮代碼:
%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);
'dwtmode('status')'返回什麼?另外,循環之後,'idwt2'之前的'size(c)'是什麼? – chappjc
你的錯誤發生在'idwt2'。你可能是四捨五入/鋪地的東西(也許你除以2),所以你有問題時,你有奇數。或者你不能在你劃分的地方進行四捨五入/地板,而你應該。尋找任何部門,並確保你在奇數出現時做正確的事情。 – Frederick
我試着用下面的命令來擺脫這個錯誤..它適用於M = N但不M不等於N.%[p q] = size(G);如果mod(p,2)&& mod(q,2) %G = padarray(G,[11],'pre'); %else –