Complete 3
This commit is contained in:
1 parent
3f76e3503f
commit
09a04fcb73
3 files changed
+141
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
from math import * # type: ignore
|
||||
import random
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
plt.rcParams["font.sans-serif"] = ["SimSun"]
|
||||
plt.rcParams["axes.unicode_minus"] = False
|
||||
|
||||
|
||||
def addGaussianNoise(src, means, sigma):
|
||||
NoiseImg = src / src.max()
|
||||
rows = NoiseImg.shape[0]
|
||||
cols = NoiseImg.shape[1]
|
||||
for i in range(rows):
|
||||
for j in range(cols):
|
||||
NoiseImg[i, j] = NoiseImg[i, j] + random.gauss(means, sigma)
|
||||
if NoiseImg[i, j] < 0:
|
||||
NoiseImg[i, j] = 0
|
||||
elif NoiseImg[i, j] > 1:
|
||||
NoiseImg[i, j] = 1
|
||||
return NoiseImg
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
img0 = cv.imread(r"img\peppers.bmp", cv.IMREAD_GRAYSCALE)
|
||||
img = addGaussianNoise(img0, 0, 0.1)
|
||||
f = np.fft.fft2(img)
|
||||
fshift = np.fft.fftshift(f)
|
||||
magnitude_spectrum0 = 20 * np.log(1 + np.abs(fshift))
|
||||
plt.figure(figsize=(10, 5))
|
||||
plt.subplot(141)
|
||||
plt.imshow(img, cmap="gray")
|
||||
plt.title("噪声图像")
|
||||
plt.axis("off")
|
||||
plt.subplot(142)
|
||||
plt.imshow(magnitude_spectrum0, cmap="gray")
|
||||
plt.title("噪声图像幅值谱")
|
||||
plt.axis("off")
|
||||
r = 50
|
||||
m, n = fshift.shape
|
||||
H = np.zeros((m, n))
|
||||
for i in range(m):
|
||||
for j in range(n):
|
||||
d = sqrt((i - m / 2) ** 2 + (j - n / 2) ** 2)
|
||||
if d < r:
|
||||
H[i, j] = 1
|
||||
G = H * fshift
|
||||
magnitude_spectrum1 = 20 * np.log(1 + np.abs(G))
|
||||
f1 = np.fft.ifftshift(G)
|
||||
img1 = np.abs(np.fft.ifft2(f1))
|
||||
plt.subplot(143)
|
||||
plt.imshow(magnitude_spectrum1, cmap="gray")
|
||||
plt.title("ILPF滤波后幅值谱")
|
||||
plt.axis("off")
|
||||
plt.subplot(144)
|
||||
plt.imshow(img1, cmap="gray")
|
||||
plt.title("ILPF滤波后重构图像")
|
||||
plt.axis("off")
|
||||
plt.show()
|
||||
@@ -0,0 +1,52 @@
|
||||
# 编写程序实现巴特沃斯低通滤波,H(u,v)=1/(1+[D(u,v)/D0]^2n),实验图8是阶数n=4时的滤波效果图,改变n值,查看并分析阶数n对滤波器的影响
|
||||
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
from math import * # type: ignore
|
||||
import random
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
plt.rcParams["font.sans-serif"] = ["SimSun"]
|
||||
plt.rcParams["axes.unicode_minus"] = False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
img = cv.imread(r"img\alphabet.jpg", cv.IMREAD_GRAYSCALE)
|
||||
f = np.fft.fft2(img)
|
||||
fshift = np.fft.fftshift(f)
|
||||
magnitude_spectrum0 = 20 * np.log(1 + np.abs(fshift))
|
||||
plt.figure(figsize=(10, 5))
|
||||
plt.subplot(151)
|
||||
plt.imshow(img, cmap="gray")
|
||||
plt.title("原图像")
|
||||
plt.axis("off")
|
||||
plt.subplot(152)
|
||||
plt.imshow(magnitude_spectrum0, cmap="gray")
|
||||
plt.title("原幅值谱")
|
||||
plt.axis("off")
|
||||
D0 = 20
|
||||
n = 4
|
||||
rows, cols = fshift.shape
|
||||
crow, ccol = rows // 2, cols // 2
|
||||
H = np.zeros((rows, cols))
|
||||
for u in range(rows):
|
||||
for v in range(cols):
|
||||
D = sqrt((u - crow) ** 2 + (v - ccol) ** 2)
|
||||
H[u, v] = 1 / (1 + (D / D0) ** (2 * n))
|
||||
G = H * fshift
|
||||
magnitude_spectrum1 = 20 * np.log(1 + np.abs(G))
|
||||
f1 = np.fft.ifftshift(G)
|
||||
img1 = np.abs(np.fft.ifft2(f1))
|
||||
plt.subplot(153)
|
||||
plt.imshow(H, cmap="gray")
|
||||
plt.title("巴特沃斯传递函数")
|
||||
plt.axis("off")
|
||||
plt.subplot(154)
|
||||
plt.imshow(magnitude_spectrum1, cmap="gray")
|
||||
plt.title("巴特沃斯滤波后的幅值谱")
|
||||
plt.axis("off")
|
||||
plt.subplot(155)
|
||||
plt.imshow(img1, cmap="gray")
|
||||
plt.title("重构图像")
|
||||
plt.axis("off")
|
||||
plt.show()
|
||||
@@ -0,0 +1,29 @@
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
plt.rcParams["font.sans-serif"] = ["SimHei"]
|
||||
plt.rcParams["axes.unicode_minus"] = False
|
||||
|
||||
img = cv.imread(r"img\peppers.bmp", 0)
|
||||
m, n = img.shape
|
||||
if m % 2 == 0:
|
||||
m = m + 1
|
||||
if n % 2 == 0:
|
||||
n = n + 1
|
||||
img = cv.resize(img, (m, n))
|
||||
f = np.fft.fft2(img)
|
||||
fshift = np.fft.fftshift(f)
|
||||
fmax = np.max(np.abs(fshift))
|
||||
u0, v0 = (m - 1) // 2, (n - 1) // 2
|
||||
u1 = u0 - 40
|
||||
v1 = v0 + 40
|
||||
fshift[v1, u1] = fmax / 5
|
||||
u2 = m - 1 - u1
|
||||
v2 = n - 1 - v1
|
||||
fshift[v2, u2] = fmax / 5
|
||||
f1 = np.fft.ifftshift(fshift)
|
||||
img1 = abs(np.fft.ifft2(f1))
|
||||
plt.imshow(img1, cmap="gray")
|
||||
plt.axis("off")
|
||||
plt.show()
|
||||
Reference in new issue
Block a user