Files
digital-image-processing/3/1.py
T
2024-09-02 16:43:04 +08:00

60 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["font.family"] = ["SimSun"]
src = cv.imread(r"img\peppers.bmp", cv.IMREAD_GRAYSCALE)
dft0 = cv.dft(np.float32(src), flags=cv.DFT_COMPLEX_OUTPUT) # type: ignore
row, col = src.shape[:2]
src1 = np.zeros((row, col), dtype=np.float32)
for i in range(row):
for j in range(col):
src1[i, j] = np.float32(src[i, j]) * ((-1) ** (i + j))
dft1 = cv.dft(src1, flags=cv.DFT_COMPLEX_OUTPUT)
magnitude0 = cv.magnitude(dft1[:, :, 0], dft1[:, :, 1])
magnitude1 = 20 * np.log(1 + magnitude0) # type: ignore
phase = cv.phase(dft1[:, :, 0], dft1[:, :, 1])
idft0 = cv.idft(dft0)
idft1 = cv.magnitude(idft0[:, :, 0], idft0[:, :, 1])
fft2 = np.fft.fft2(src)
fftshift = np.fft.fftshift(fft2)
abs0 = np.abs(fftshift)
abs1 = 20 * np.log(1 + abs0)
angle = np.angle(fftshift)
ifft20 = np.fft.ifft2(fftshift)
ifft21 = np.abs(ifft20)
plt.figure()
plt.subplot(2, 4, (1, 5))
plt.title("输入图像")
plt.imshow(src, cmap="gray")
plt.axis("off")
plt.subplot(2, 4, 2)
plt.title("幅值谱(OpenCV)")
plt.imshow(magnitude1, cmap="gray")
plt.axis("off")
plt.subplot(2, 4, 3)
plt.title("相位谱(OpenCV)")
plt.imshow(phase, cmap="gray")
plt.axis("off")
plt.subplot(2, 4, 4)
plt.title("重构图像(OpenCV)")
plt.imshow(idft1, cmap="gray")
plt.axis("off")
plt.subplot(2, 4, 6)
plt.title("幅值谱(NumPy)")
plt.imshow(abs1, cmap="gray")
plt.axis("off")
plt.subplot(2, 4, 7)
plt.title("相位谱(NumPy)")
plt.imshow(angle, cmap="gray")
plt.axis("off")
plt.subplot(2, 4, 8)
plt.title("重构图像(NumPy)")
plt.imshow(ifft21, cmap="gray")
plt.axis("off")
plt.tight_layout()
plt.show()