shows the error matrix dimensions must agree

1 view (last 30 days)
clc;
close all;
A=imread('index.jpg');
im=rgb2gray(A);
im=imnoise(im,'gaussian',0,0.5);
[m,n,d]=size(im);
xc=round(m/2);
yc=round(n/2);
sigma=7;
figure,subplot(2,3,1),imshow(im);title('original Image');
[X,Y]=meshgrid(1:1:256);
amplitude = 1 / (sigma * sqrt(2*pi));
z=amplitude*exp(-((X-xc).^2+(Y-yc).^2)/(2*sigma^2));
subplot(2,3,2),imshow(z,[]);title('Filter in Time');
% figure,surf(1:1:256,1:1:256,z);shading
title('Gaussian in Time');
ft_z=fft2(z);
% subplot(2,3,3),
ft_sft=fftshift(abs(ft_z));
% figure,surf(1:1:256,1:1:256,ft_sft);shading
subplot(2,3,3);
imshow(ft_sft,[]);title('Filter in freq');
%%%
im_f=fft2(im);
subplot(2,3,4),imshow(fftshift(log(abs(im_f))),[]);title('Image in Freq')
ft_imf=im_f.*ft_z;
subplot(2,3,5),imshow(fftshift(log(abs(ft_imf))),[]);title('Gaussian filtered output');
r_t=fftshift(ifft2(ft_imf));
subplot(2,3,6),imshow(r_t,[]);title('Reconstructed output');
%%%
Matrix dimensions must agree.
Error in quency (line 26)
ft_imf=im_f.*ft_z;
  1 Comment
Geoff Hayes
Geoff Hayes on 19 Nov 2020
Sathiya - what are the dimensions for im_f and ft_z? Since you are using .* (element-wise multiplication) then the dimensions of both matrices has to be identical.

Sign in to comment.

Answers (1)

KSSV
KSSV on 19 Nov 2020
clc;
close all;
A=imread('index.jpg');
im=rgb2gray(A);
im=imnoise(im,'gaussian',0,0.5);
[m,n,d]=size(im);
xc=round(m/2);
yc=round(n/2);
sigma=7;
figure,subplot(2,3,1),imshow(im);title('original Image');
% Change here
xi = linspace(1,256,n) ;
yi = linspace(1,256,m) ;
[X,Y]=meshgrid(xi,yi);
% The above will make dimensions of im_f and ft_z same.
amplitude = 1 / (sigma * sqrt(2*pi));
z=amplitude*exp(-((X-xc).^2+(Y-yc).^2)/(2*sigma^2));
subplot(2,3,2),imshow(z,[]);title('Filter in Time');
% figure,surf(1:1:256,1:1:256,z);shading
title('Gaussian in Time');
ft_z=fft2(z);
% subplot(2,3,3),
ft_sft=fftshift(abs(ft_z));
% figure,surf(1:1:256,1:1:256,ft_sft);shading
subplot(2,3,3);
imshow(ft_sft,[]);title('Filter in freq');
%%%
im_f=fft2(im);
subplot(2,3,4),imshow(fftshift(log(abs(im_f))),[]);title('Image in Freq')
ft_imf=im_f.*ft_z;
subplot(2,3,5),imshow(fftshift(log(abs(ft_imf))),[]);title('Gaussian filtered output');
r_t=fftshift(ifft2(ft_imf));
subplot(2,3,6),imshow(r_t,[]);title('Reconstructed output');
%%%
Matrix dimensions must agree.
Error in quency (line 26)
ft_imf=im_f.*ft_z;
  2 Comments
Sathiya narayanan s
Sathiya narayanan s on 19 Nov 2020
@KSSV thankyou so much , you really made my day
KSSV
KSSV on 19 Nov 2020
Thanks is accepting/ voting the answer.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!