Array dimensions must match for binary array op.
2 次查看(过去 30 天)
显示 更早的评论
Hi, im trying to do image noise redution using various low pass filter but i'm getting this error for most of the images i load. This error does not come when i use the cameraman.tif image and the coins.png image
This is the error:
Array dimensions must match for binary array op.
Error in Lowpassfull>gaussianLPFilter (line 81)
C=L.*Hi;
Error in Lowpassfull (line 23)
C=gaussianLPFilter(L,rows,columns);
THIS THE CODE:
clc;
clear;
close all;
im=im2double(imread('noisy.jpg')); % convert int double
im = imresize(im,[256,256]);
im = imnoise(im,'gaussian', 0.1);
subplot(3,3,1);
imshow(im);
title('original image');
fftImage=fft2(im);
Y = fftshift(fftImage);
subplot(3,3,2);
imshow(real(log(Y)), []);
title('In Frequency Domain');
[rows columns] = size(Y);
L=Y;
B=idealLPFilter(L,rows,columns);
C=gaussianLPFilter(L,rows,columns);
D=butterworthLPFilter(L,rows,columns);
subplot(3,3,3);
imshow(real(log(B)), [])
title('Ideal Low Pass Filter(Frequency Domain)');
subplot(3,3,4);
imshow(log(abs(C)),[]);
title('Gaussian Low Pass Filter(Frequency Domain)');
subplot(3,3,5);
imshow(log(abs(D)), []);
title('Butterworth Low Pass Filter(Frequency Domain)');
filteredImage = real(ifft2(ifftshift(B)));
subplot(3,3,6);
imshow(real(filteredImage), []);
title('Output: Ideal Low Pass Filter');
GfilteredImage = real(ifft2(ifftshift(C)));
subplot(3,3,7);
imshow(real(GfilteredImage), []);
title('Output: Gaussain Low Pass Filter');
BfilteredImage = real(ifft2(ifftshift(D)));
subplot(3,3,8);
imshow(real(BfilteredImage), []);
title('Output: Butterworth Low Pass Filter');
err = immse(real(BfilteredImage), im);
fprintf('\n The mean-squared error is %0.4f\n', err);
function B = idealLPFilter(L,rows,columns)
window = 100;
B=L;
B(1:end, 1:window) = 0;%top left
B(1:window,1:end) = 0;
B(1:end, end-window:end) = 0; %top right
B(end-window:end, 1:end) = 0;%down right
end
function C = gaussianLPFilter(L,rows,columns)
R=20; %Filter size parameter
X=0:columns-1;
Y=0:rows-1;
[X Y]=meshgrid(X,Y);
Cx=0.5*columns;
Cy=0.5*rows;
Hi = exp(-((X-Cx).^2+(Y-Cy).^2)./(2*R).^2);
C=L.*Hi;
end
function D = butterworthLPFilter(L,rows,columns)
n=1;
D0=20;
[p q]=meshgrid(-floor(columns/2):floor(columns/2)-1,-floor(rows/2):floor(rows/2)-1);
D = sqrt(p.^2 + q.^2);
hhp = 1 ./ (1 + ((D ./ D0).^(2 * n)));
size(L)
size(hhp)
D=L.*hhp;
end
0 个评论
采纳的回答
Tommy
2020-6-5
'cameraman.tif' and 'coins.png' are both grayscale, meaning your im is a NxM array and your code works.
For any image with color, such as 'pears.png', im is a NxMx3 array. Per the documentation for size():
"When dim is not specified and fewer than ndims(A) output arguments are listed, then all remaining dimension lengths are collapsed into the last argument in the list. For example, if A is a 3-D array with size [3 4 5], then [sz1,sz2] = size(A) returns sz1 = 3 and sz2 = 20."
Therefore, columns equals M*3, not M, and you get the error.
If you are open to the possibility of im being a 3D array, i.e.
[rows, columns, ~] = size(Y);
instead of
[rows, columns] = size(Y);
then you won't get an error for images with color. However, to be honest, I've got no idea if this one change alone would give you the correct results. I believe making this change alone would pass each channel of your image (red, green, and blue) through each of your filters separately. Maybe that is correct?
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Computer Vision with Simulink 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!