Calculating Power of Specific Spatial Frequencies of pictures
Hi experts,
I want to calculate the power of the spatial frequency range 20-30 cycles per picture in an picture. I am not sure how I can accomplish this using MATLAB code. FYI. Cycles per picture is a spatial frequency unit, also called cycles per image.
I downloaded following code online and made some modifications. Could you check the code for me and tell me whether I am correct?
% 1. Read the image
img = imread(‘your_image.jpg’);
img_gray = rgb2gray(img); % Convert to grayscale if the image is colored
% 2. Compute the Fourier transform of the image
img_fft = fft2(double(img_gray));
img_fft_shifted = fftshift(img_fft);
% Get the size of the image
[rows, cols] = size(img_gray);
% Compute the frequency coordinates
u = (-rows/2:(rows/2-1)) / rows;
v = (-cols/2:(cols/2-1)) / cols;
[U, V] = meshgrid(u, v);
D = sqrt(U.^2 + V.^2);
% 3. Calculate the power in the 20-30 cycles per image range
freq_low = 20 / rows;
freq_high = 30 / rows;
mask = (D >= freq_low) & (D <= freq_high);
energy = sum(sum(abs(img_fft_shifted .* mask).^2));
% Output the power
fprintf(‘Power in the range 20-30 cycles per image: %fn’, energy);Hi experts,
I want to calculate the power of the spatial frequency range 20-30 cycles per picture in an picture. I am not sure how I can accomplish this using MATLAB code. FYI. Cycles per picture is a spatial frequency unit, also called cycles per image.
I downloaded following code online and made some modifications. Could you check the code for me and tell me whether I am correct?
% 1. Read the image
img = imread(‘your_image.jpg’);
img_gray = rgb2gray(img); % Convert to grayscale if the image is colored
% 2. Compute the Fourier transform of the image
img_fft = fft2(double(img_gray));
img_fft_shifted = fftshift(img_fft);
% Get the size of the image
[rows, cols] = size(img_gray);
% Compute the frequency coordinates
u = (-rows/2:(rows/2-1)) / rows;
v = (-cols/2:(cols/2-1)) / cols;
[U, V] = meshgrid(u, v);
D = sqrt(U.^2 + V.^2);
% 3. Calculate the power in the 20-30 cycles per image range
freq_low = 20 / rows;
freq_high = 30 / rows;
mask = (D >= freq_low) & (D <= freq_high);
energy = sum(sum(abs(img_fft_shifted .* mask).^2));
% Output the power
fprintf(‘Power in the range 20-30 cycles per image: %fn’, energy); Hi experts,
I want to calculate the power of the spatial frequency range 20-30 cycles per picture in an picture. I am not sure how I can accomplish this using MATLAB code. FYI. Cycles per picture is a spatial frequency unit, also called cycles per image.
I downloaded following code online and made some modifications. Could you check the code for me and tell me whether I am correct?
% 1. Read the image
img = imread(‘your_image.jpg’);
img_gray = rgb2gray(img); % Convert to grayscale if the image is colored
% 2. Compute the Fourier transform of the image
img_fft = fft2(double(img_gray));
img_fft_shifted = fftshift(img_fft);
% Get the size of the image
[rows, cols] = size(img_gray);
% Compute the frequency coordinates
u = (-rows/2:(rows/2-1)) / rows;
v = (-cols/2:(cols/2-1)) / cols;
[U, V] = meshgrid(u, v);
D = sqrt(U.^2 + V.^2);
% 3. Calculate the power in the 20-30 cycles per image range
freq_low = 20 / rows;
freq_high = 30 / rows;
mask = (D >= freq_low) & (D <= freq_high);
energy = sum(sum(abs(img_fft_shifted .* mask).^2));
% Output the power
fprintf(‘Power in the range 20-30 cycles per image: %fn’, energy); digital image processing, fft2, fftshift MATLAB Answers — New Questions