MATLAB code for shifting a gray scale image to x and y axis
Assistant
To shift a grayscale image in the x and y axes, you can use the imtranslate function in MATLAB. Here is an example code that demonstrates how you can shift an image in the x and y directions:
MATLAB
% Load the grayscale image
image = imread('image.jpg');
% Convert the image to double for image manipulation
image = im2double(image);
% Define the shift values for the x and y axes
shift_x = 50; % Amount to shift in the x-axis
shift_y = 30; % Amount to shift in the y-axis
% Shift the image using the imtranslate function
shifted_image = imtranslate(image, [shift_x, shift_y], 'FillValues', 0);
% Display the original and shifted images side by side
figure;
subplot(1, 2, 1);
imshow(image);
title('Original Image');
subplot(1, 2, 2);
imshow(shifted_image);
title('Shifted Image');
User
MATLAB code for shifting a gray scale image to x and y axis and then find its subpixel level phase shifts in both axis.
Assistant
To find subpixel level phase shifts in both the x and y axes after shifting a grayscale image, you can use the dftregistration function in MATLAB. The dftregistration function performs subpixel precision image registration based on the discrete Fourier transform (DFT).
Here is an example code that demonstrates how you can shift a grayscale image in the x and y axes, and then find its subpixel level phase shifts in both axes:
User
In MATLAB, take a gray scale image with moire effect. Reduce the moire effect in frequency domain using Butterworth filter and compare the images. Provide the code for this.
Assistant
To reduce the moire effect in a grayscale image using a Butterworth filter in the frequency domain, you can follow these steps:
In this code, we assume that the grayscale image is stored in a file named 'image.jpg'. You can replace it with the actual filename of your image. The imtranslate function is then used to shift the image by the specified amounts in the x and y directions. The 'FillValues' option is set to 0 to fill the empty areas resulting from the shift with black pixels. Finally, the original and shifted images are displayed side by side using the imshow function.
MATLAB
% Load the grayscale image
image = imread('image.jpg');
% Convert the image to double for image manipulation
image = im2double(image);
% Define the shift values for the x and y axes
shift_x = 50; % Amount to shift in the x-axis
shift_y = 30; % Amount to shift in the y-axis
% Shift the image using the imtranslate function
shifted_image = imtranslate(image, [shift_x, shift_y], 'FillValues', 0);
% Calculate the subpixel level phase shifts using dftregistration function
[output, ~, ~] = dftregistration(fft2(image), fft2(shifted_image), 100);
% Extract the subpixel level phase shifts in both x and y axes
subpixel_x_shift = output(4);
subpixel_y_shift = output(3);
% Display the original and shifted images with subpixel level phase shifts
figure;
subplot(1, 2, 1);
imshow(image);
title('Original Image');
subplot(1, 2, 2);
imshow(shifted_image);
title('Shifted Image');
% Display the subpixel level phase shifts
disp(['Subpixel X Shift: ', num2str(subpixel_x_shift)]);
disp(['Subpixel Y Shift: ', num2str(subpixel_y_shift)]);
In this code, we assume that the grayscale image is stored in a file named 'image.jpg'. You can replace it with the actual filename of your image. The imtranslate function is used to shift the image by the specified amounts in the x and y directions. Then, the dftregistration function is applied to calculate the subpixel level phase shifts between the original and shifted images. The subpixel level phase shifts are stored in the output variable, and you can extract them as output(4) for the x-axis shift and output(3) for the y-axis shift. Finally, the original and shifted images are displayed side by side, and the subpixel level phase shifts are displayed in the MATLAB command window.
MATLAB
% Load the grayscale image with moire effect
image = imread('moire_image.jpg');
% Convert the image to double for image manipulation
image = im2double(image);
% Perform Fourier transform on the image
fft_image = fftshift(fft2(image));
% Define the Butterworth filter parameters
D0 = 50; % Cutoff frequency
n = 4; % Filter order
% Create the Butterworth filter
[M, N] = size(image);
X = 1:N;
Y = 1:M;
[X, Y] = meshgrid(X, Y);
centerX = ceil(N/2);
centerY = ceil(M/2);
distX = abs(X - centerX);
distY = abs(Y - centerY);
filtershape = 1 ./ (1 + (distX./D0).^(2*n) + (distY./D0).^(2*n));
% Apply the Butterworth filter to the Fourier image
filtered_image = ifft2(ifftshift(fft_image .* filtershape));
% Normalize the image to the range [0, 1]
filtered_image = mat2gray(abs(filtered_image));
% Display the original and filtered images side by side
figure;
subplot(1, 2, 1);
imshow(image);
title('Original Image');
subplot(1, 2, 2);
imshow(filtered_image);
title('Filtered Image');
In this code, we assume that the grayscale image with the moire effect is stored in a file named 'moire_image.jpg'. You can replace it with the actual filename of your image. The im2double function is used to convert the image to double for manipulation.
The code then performs a Fourier transform of the image using the fft2 function, followed by fftshift to move the DC component to the center of the spectrum.
Next, we define the parameters for the Butterworth filter, including the cutoff frequency D0 and the filter order n. The higher the order, the steeper the filter roll-off.
We create the Butterworth filter using the inverse relationship between the cutoff frequency and the filter response. The filter shape is calculated for each pixel position in the image using the radial distance from the center. A higher value indicates more attenuation.
We apply the Butterworth filter to the Fourier image by element-wise multiplication. The filtered image is obtained by performing an inverse Fourier transform using ifft2, followed by ifftshift to move the DC component back to the top-left corner of the image.
Finally, we normalize the filtered image to the range [0, 1] using the mat2gray function and display the original and filtered images side by side using the imshow function.