在Python中使用 OpenCV 实现 Photoshop 高通滤波器 (HPF)
在本文中,我们将讨论如何在Python OpenCV 中实现 Photoshop 高通滤波器 (HPF) 图像。
高通滤波器
当我们谈论数字信号处理或数字图像处理时,滤波器是最基本的概念。滤波器或数字滤波器用于从信号中筛选出不需要的频率响应。滤波器可用于完成数字信号处理中的两个主要任务,即信号分离和信号恢复。信号分离基本上是对输入信号的噪声分离和噪声衰减。这个过程主要用于心电图,一种心电图监测器,用于过滤母亲的呼吸和心跳。信号恢复是输入信号以任何方式失真时的过程。这主要用于对图像进行去模糊处理。我们将看看高通滤波器主要用于什么。
高通滤波器(HPF)的主要优点是通过衰减低频来锐化图像。当脉冲响应或信号通过高通滤波器时,HPF 主要允许高频通过。由于高通滤波器用于锐化图像,因此获得的频率与截止频率(ωc)相比较小。在 OpenCV 和数字图像处理中,我们还使用 HPF 功能来查找图像中的边缘。
方法一: Python OpenCV 中的高通滤波器(HPF)
在这里,我们将在Python中使用 OpenCV 执行 HPF
使用的图像:
现在我们有了一个图像,我们将使用Python OpenCV 模块读取图像。
img = cv2.imread(“outimage.(jpeg/png/jpg)”)
由于图像的大小,我们也可以调整形状的大小,这一步是完全可选的。在调整图像大小时,您可以传递插值以使图像保持其质量。您可以自由选择 cv2.INTER_BITS、cv2.INTER_CUBIC 或 cv2.INTER_LINEAR 方法来调整插值大小。
img = cv2.resize(img,(width,height),interpolation=cv2.INTER_BITS)
下一步是模糊图像。模糊图像的原因是为图像添加平滑效果。通过模糊图像,我们可以过滤掉图像中不需要的噪声。在 OpenCV 库中,我们广泛使用 Gaussian Filter。它采用了“内核卷积”技术。
注意: 127是在减去带有模糊图像的图像后添加的,以添加灰色外观。我们将使用高斯模糊来模糊图像。
hpf = img – cv2.GaussianBlur(img,(21,21),3)+127
执行
Python3
import cv2
img = cv2.imread("naruto.jpg")
img = cv2.resize(img, (680, 520),
interpolation=cv2.INTER_CUBIC)
# subtract the original image with the blurred image
# after subtracting add 127 to the total result
hpf = img - cv2.GaussianBlur(img, (21, 21), 3)+127
# display both original image and filtered image
cv2.imshow("Original", img)
cv2.imshow("High Passed Filter", hpf)
# cv2.waitkey is used to display
# the image continuously
# if you provide 1000 instead of 0 then
# image will close in 1sec
# you pass in milli second
cv2.waitKey(0)
cv2.destroyAllWindows()
Python3
import cv2
import numpy as np
import matplotlib.pyplot as plt
# read image
img = cv2.imread("naruto.jpeg")
# resize image
img = cv2.resize(img, (500, 450), interpolation=cv2.INTER_CUBIC)
# convert image to gray scale image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# apply laplacian blur
laplacian = cv2.Laplacian(gray, cv2.CV_64F)
# sobel x filter where dx=1 and dy=0
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=7)
# sobel y filter where dx=0 and dy=1
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=7)
# combine sobel x and y
sobel = cv2.bitwise_and(sobelx, sobely)
# plot images
plt.subplot(2, 2, 1)
plt.imshow(laplacian, cmap='gray')
plt.title('Laplacian')
plt.subplot(2, 2, 2)
plt.imshow(sobelx, cmap='gray')
plt.title('SobelX')
plt.subplot(2, 2, 3)
plt.imshow(sobely, cmap='gray')
plt.title('SobelY')
plt.subplot(2, 2, 4)
plt.imshow(sobel, cmap='gray')
plt.title('Sobel')
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
Python3
import cv2
import numpy as np
img = cv2.imread('naruto.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# dimension of gray image is in 2D
dimensions = gray.shape # (height,weight)
print("Image Dimensions:", dimensions)
n = int(input("Enter the size of the original image to be captured:"))
print("The matrix of the original image:")
for i in range(0, n):
for j in range(0, n):
print(gray[i][j], end=" ")
print()
"""
Apply below filter on image matrix
0 -1 0
-1 4 -1
0 -1 0
"""
filter = np.zeros(shape=(n, n))
for i in range(0, n):
for j in range(0, n):
filter[i][j] = 0*gray[i][j]-1*gray[i][j+1]\
+0*gray[i][j+2]-1*gray[i+1][j]+4 * \
gray[i+1][j+1]-1*gray[i+1][j+2]+0*gray[i+2][j] - \
1*gray[i+2][j+1]+0*gray[i+2][j+2]
print("\n")
print("The matrix form after HPF masking the captured image is:")
print("\n")
for hpf in filter:
print(hpf)
cv2.imshow("Image", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
方法 2:使用 Sobel 和 Laplacian
Python OpenCV 支持 Sobel 和 Laplacian 实现。
Syntax for Laplacian: laplacian=cv2.Laplacian(gray,cv2.CV_64F)
Syntax For SobelX: [dx=1 and dy=0]: sobelx=cv2.Sobel(gray,cv2.CV_64F,1,0,ksize=7)
Syntax For SobelY: [dx=0 and dy=1]: sobely=cv2.Sobel(gray,cv2.CV_64F,dx=0,dy=1,ksize=7)
注意: cv2.CV_64F 表示图像的深度
Python3
import cv2
import numpy as np
import matplotlib.pyplot as plt
# read image
img = cv2.imread("naruto.jpeg")
# resize image
img = cv2.resize(img, (500, 450), interpolation=cv2.INTER_CUBIC)
# convert image to gray scale image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# apply laplacian blur
laplacian = cv2.Laplacian(gray, cv2.CV_64F)
# sobel x filter where dx=1 and dy=0
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=7)
# sobel y filter where dx=0 and dy=1
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=7)
# combine sobel x and y
sobel = cv2.bitwise_and(sobelx, sobely)
# plot images
plt.subplot(2, 2, 1)
plt.imshow(laplacian, cmap='gray')
plt.title('Laplacian')
plt.subplot(2, 2, 2)
plt.imshow(sobelx, cmap='gray')
plt.title('SobelX')
plt.subplot(2, 2, 3)
plt.imshow(sobely, cmap='gray')
plt.title('SobelY')
plt.subplot(2, 2, 4)
plt.imshow(sobel, cmap='gray')
plt.title('Sobel')
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
方法三:手动对图像进行高通滤波
由于我们已经定义了矩阵方法,让我们手动过滤我们的图像数据。通过保持对拉普拉斯矩阵的引用,可以在 HPF 遮罩后显示图像的像素矩阵格式。
Python3
import cv2
import numpy as np
img = cv2.imread('naruto.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# dimension of gray image is in 2D
dimensions = gray.shape # (height,weight)
print("Image Dimensions:", dimensions)
n = int(input("Enter the size of the original image to be captured:"))
print("The matrix of the original image:")
for i in range(0, n):
for j in range(0, n):
print(gray[i][j], end=" ")
print()
"""
Apply below filter on image matrix
0 -1 0
-1 4 -1
0 -1 0
"""
filter = np.zeros(shape=(n, n))
for i in range(0, n):
for j in range(0, n):
filter[i][j] = 0*gray[i][j]-1*gray[i][j+1]\
+0*gray[i][j+2]-1*gray[i+1][j]+4 * \
gray[i+1][j+1]-1*gray[i+1][j+2]+0*gray[i+2][j] - \
1*gray[i+2][j+1]+0*gray[i+2][j+2]
print("\n")
print("The matrix form after HPF masking the captured image is:")
print("\n")
for hpf in filter:
print(hpf)
cv2.imshow("Image", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出: