我有这张图像,我想抑制该图像中的峰值,它代表原始图像中的划痕(但这不是重点)。我想要的只是制作一种算法,自动识别图像中的峰值,然后抑制它们(使其为 0)。
from skimage.draw import disk
import matplotlib.pyplot as plt
original = plt.imread('orginal image.jpg')
plt.figure(figsize=(15,15))
plt.imshow(original)
plt.show()
import math
# Calculate the next power of 2 for both dimensions
next_power_of_2_x = 2 ** math.ceil(math.log2(original.shape[0] + 249))
next_power_of_2_y = 2 ** math.ceil(math.log2(original.shape[1]))
# Pad the array to the next power of 2 in both dimensions
f = np.pad(original, ((0, next_power_of_2_x - original.shape[0]), (0, next_power_of_2_y - original.shape[1]), (0, 0)))
f_hat = fft2(f, axes=(0, 1))
shifted = np.fft.fftshift(f_hat)
# magnitude and phase
mag=np.abs(shifted)
phase=np.unwrap(np.angle(shifted))
plt.figure(figsize=(10,10))
plt.imshow(np.log(1+np.linalg.norm(mag,axis=-1)),cmap='gray')
plt.show()
# strong peaks
mag_norm=np.linalg.norm(mag,axis=-1).copy()
rr,cc=disk((1024,1024),80)
mag_norm[rr, cc]=0
mag_norm[mag_norm<mag_norm.max()*0.01]=0
plt.figure(figsize=(10,10))
plt.imshow(np.log(1+mag_norm),cmap='gray')
plt.show()
# weak peaks
mag_norm=np.linalg.norm(mag,axis=-1).copy()
rr,cc=disk((1024,1024),100)
mag_norm[rr, cc]=0
mag_norm[mag_norm<mag_norm.max()*0.001]=0
plt.figure(figsize=(10,10))
plt.imshow(np.log(1+mag_norm),cmap='gray')
plt.show()
# set periodic patterns to zero
centers_strong=[(1000,1000)]
for r,c in centers_strong:
for center in [(r,c),(2024-r,c),(r,2024-c),(2024-r,2024-c)]:
rr,cc=disk(center,20)
mag[rr, cc]=0
centers_weak=[(1000,1000)]
for r,c in centers_weak:
for center in [(r,c),(2024-r,c),(r,2024-c),(2024-r,2024-c)]:
rr,cc=disk(center,10)
mag[rr, cc]=0
mag[:,1000]=0
mag[:,2024-1000]=0
mag[1000,:]=0
mag[2024-1000,:]=0
plt.figure(figsize=(10,10))
plt.imshow(np.log(1+np.linalg.norm(mag,axis=-1)),cmap='gray')
plt.show()
%%time
# combine modified magnitude and phase, take inverse fft
m=mag*np.exp(1j*phase)
m=np.fft.ifftshift(m)
m=np.real(ifft2(m,axes=(0,1)))[:1150,...]
m/=m.max()
m = np.abs(m)
plt.figure(figsize=(15,15))
#plt.imshow(m, vmin=0, vmax=1)
plt.imshow(1-m, cmap='gray')
plt.show()
这是我编写的代码,通过它我收到了给定的结果。你可以查看这些库,我已经用 Python 编写了它。
我想要什么? 我希望该算法采用 Python,它会自动检测 FFT 峰值(坐标),然后抑制它们。如果有人知道如何实现这一目标,我们将非常感谢您的帮助。预先感谢。