Error using .* Array dimensions must match for binary array op. Error in uas1 (line 7) If_low = If.*c;

10 ビュー (過去 30 日間)
mukti
mukti 2024 年 1 月 7 日
コメント済み: Dyuman Joshi 2024 年 1 月 7 日
I = imread ('gray.jpg');
If=fft2(I);
If=fftshift(If);
[x,y] = meshgrid(1:1, 1:1)
z = sqrt(x.^2 + y.^2)
c = (z<3)
If_low = If.*c;
If_low_inv = ifft2(If_low);
subplot (2,2,1), imshow(I), title ('foto asli')
subplot (2,2,2), fftshow(If), title ('foto transformasi fourier')
subplot (2,2,3), imshow(c), title ('low passfilter')
subplot (2,2,4), fftshow(If_low_inv, 'abs'), title ('filtering')
  3 件のコメント
madhan ravi
madhan ravi 2024 年 1 月 7 日
meshgrid() arguments are scalar
Dyuman Joshi
Dyuman Joshi 2024 年 1 月 7 日
Clearly, the dimensions of If and c are not compatible for element-wise multiplication.
However, since you have not described what you are trying to do, we can't provide a definitive suggestion to help solve this issue.
A general solution is to make sure that the variables are compatible for element-wise multiplication.

サインインしてコメントする。

回答 (1 件)

Hassaan
Hassaan 2024 年 1 月 7 日
I = imread('gray.jpg');
% Make sure the input image is grayscale
if size(I, 3) == 3
I = rgb2gray(I);
end
% Convert image to double for FFT
I_double = im2double(I);
% Apply 2D FFT and shift zero frequency component to the center
If = fft2(I_double);
If_shifted = fftshift(If);
% Create a meshgrid for the frequency domain that matches the image size
[rows, cols] = size(I_double);
[x, y] = meshgrid(-cols/2:cols/2-1, -rows/2:rows/2-1);
z = sqrt(x.^2 + y.^2);
% Create a circular low-pass filter mask
cutoff = 30; % You may need to adjust the cutoff frequency
c = (z < cutoff);
% Apply the low-pass filter to the shifted frequency domain image
If_low = If_shifted .* c;
% Shift back and apply inverse FFT
If_low_unshifted = ifftshift(If_low);
If_low_inv = ifft2(If_low_unshifted);
% Display the results
subplot(2, 2, 1), imshow(I), title('Original Image');
subplot(2, 2, 2), imshow(log(1 + abs(If_shifted)), []), title('Fourier Transform');
subplot(2, 2, 3), imshow(c), title('Low Pass Filter');
subplot(2, 2, 4), imshow(abs(If_low_inv)), title('Filtered Image');
  • The meshgrid is centered around zero and then shifted to match the centering performed by fftshift.
  • The mask c is then also centered with ifftshift to match the FFT-shifted frequency array before applying the element-wise multiplication.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering

カテゴリ

Help Center および File ExchangeRead, Write, and Modify Image についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by