📅  最后修改于: 2021-01-07 06:39:05             🧑  作者: Mango
图像过滤是通过更改像素的阴影或颜色来修改图像的过程。它还用于增加亮度和对比度。在本教程中,我们将学习几种类型的过滤器。
OpenCV提供了bilateralFilter()函数,以将双边过滤器应用于图像。双边滤波器可以很好地减少有害噪声,同时保持边缘清晰。该函数的语法如下:
cv2.bilateralFilter(src, dst, d, sigmaSpace, borderType)
考虑以下示例:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\baloon.jpg',1)
kernel = np.ones((5,5),np.float32)/25
blur = cv2.bilateralFilter(img,9,75,75)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Bilateral Filter')
plt.xticks([]), plt.yticks([])
cv2.imshow("Image",blur)
输出量
我们可以使用boxfilter()函数执行此过滤器。它类似于平均模糊操作。该函数的语法如下:
cv2. boxfilter(src, dst, ddepth, ksize, anchor, normalize, bordertype)
考虑以下示例:
import cv2
import numpy as np
# using imread('path') and 0 denotes read as grayscale image
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\baloon.jpg',1)
img_1 = cv2.boxFilter(img, 0, (7,7), img, (-1,-1), False, cv2.BORDER_DEFAULT)
#This is using for display the image
cv2.imshow('Image',img_1)
cv2.waitKey(3) # This is necessary to be required so that the image doesn't close immediately.
#It will run continuously until the key press.
cv2.destroyAllWindows()
输出量
它将图像与内核结合在一起。我们可以使用Filter2D()方法对图像执行此操作。该函数的语法如下:
cv2.Filter2D(src, dst, kernel, anchor = (-1,-1))
考虑以下示例:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\baloon.jpg',1)
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Filter2D')
plt.xticks([]), plt.yticks([])
plt.show()
输出量