this is the code: import numpy as np import matplotlib.pyplot as plt from…
- User
this is the code: import numpy as np import matplotlib.pyplot as plt from matplotlib.image import imread
Load image
img = imread('/Users/abhi/2 Areas/JOB/Teisnach Thesis/Important images/New Images/Medium_Grinded_KT copy.jpg.jpeg')
Convert to grayscale if it's a colored image
if img.ndim == 3: img = np.mean(img, axis=2)
Apply FFT to the image
fft_result = np.fft.fft2(img) fft_shift = np.fft.fftshift(fft_result)
Define the thickness of the line
thickness = 5 # Change this value to increase or decrease the line thickness
Add a thick horizontal line at the center, avoiding the center point
center_row = fft_shift.shape[0] // 2 center_col = fft_shift.shape[1] // 2
Create a copy to avoid altering the center point
fft_shift_with_line = np.copy(fft_shift)
Zero out 'thickness' number of rows around the center row, except the center point
for offset in range(-thickness // 2, thickness // 2 + 1): fft_shift_with_line[center_row + offset, :] = 0 # Set the rows to zero fft_shift_with_line[center_row + offset, center_col] = fft_shift[center_row + offset, center_col] # Restore the center point
Get the magnitude spectrum
magnitude_spectrum = np.log(np.abs(fft_shift_with_line) + 1)
Perform the inverse FFT
ifft_shift = np.fft.ifftshift(fft_shift_with_line) img_reconstructed = np.fft.ifft2(ifft_shift) img_reconstructed = np.real(img_reconstructed) # Take only the real part
Plot the original, magnitude spectrum, and the reconstructed images
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1) plt.title("Original Image") plt.imshow(img, cmap='gray')
plt.subplot(1, 3, 2) plt.title("Magnitude Spectrum with Thick Line") plt.imshow(magnitude_spectrum, cmap='gray')
plt.subplot(1, 3, 3) plt.title("Reconstructed Image") plt.imshow(img_reconstructed, cmap='gray')
plt.show()
find where I can change the thickness of the line?
- Assistant
In the code you provided, the thickness of the line in the frequency space is defined by the variable
thickness. You can change the thickness of the line by modifying the value assigned to this variable. Here's the relevant part of the code:# Define the thickness of the line thickness = 5 # Change this value to increase or decrease the line thicknessIf you want a thicker line, increase the value of
thickness. If you want a thinner line, decrease the value ofthickness. This variable determines how many rows around the center row are set to zero in the frequency domain, effectively creating a horizontal line. The current value is set to 5, so a larger value will result in a thicker line, and a smaller value will result in a thinner line.After changing the value of
thickness, the following loop creates the line in the frequency domain:# Zero out 'thickness' number of rows around the center row, except the center point for offset in range(-thickness // 2, thickness // 2 + 1): fft_shift_with_line[center_row + offset, :] = 0 # Set the rows to zero fft_shift_with_line[center_row + offset, center_col] = fft_shift[center_row + offset, center_col] # Restore the center point