使用OpenCV的Python最小过滤器
我正在尝试使用OpenCV库对一些图像应用min filter。如何使用OpenCV在Python中实现最小过滤器? 有什么功能可以做到这一点吗?使用OpenCV的Python最小过滤器,python,image,opencv,image-processing,filtering,Python,Image,Opencv,Image Processing,Filtering,我正在尝试使用OpenCV库对一些图像应用min filter。如何使用OpenCV在Python中实现最小过滤器? 有什么功能可以做到这一点吗? 如果没有,我怎么写呢?形态学侵蚀是一个最小的过滤器。在OpenCV中,这是在函数中实现的。通过min filter我猜您的意思是通过图像中的每个位置运行内核,并用内核像素内的最小值替换内核中心 要在Opencv中实现这一点,您只需使用,cv2.correase。文件 但是首先,您需要定义内核的大小和形状。您可以使用cv.getStructuringE
如果没有,我怎么写呢?形态学侵蚀是一个最小的过滤器。在OpenCV中,这是在函数中实现的。通过
min filter
我猜您的意思是通过图像中的每个位置运行内核,并用内核像素内的最小值替换内核中心
要在Opencv中实现这一点,您只需使用,cv2.correase
。文件
但是首先,您需要定义内核的大小和形状。您可以使用cv.getStructuringElement
doc:
例如:
size = (3, 3)
shape = cv2.MORPH_RECT
kernel = cv2.getStructuringElement(shape, size)
min_image = cv2.erode(image, kernel)
除了前面的答案之外,我还在python+opencv中实现了应用最小和最大框过滤器的代码
import cv2
def minimumBoxFilter(n, path_to_image):
img = cv2.imread(path_to_image)
# Creates the shape of the kernel
size = (n, n)
shape = cv2.MORPH_RECT
kernel = cv2.getStructuringElement(shape, size)
# Applies the minimum filter with kernel NxN
imgResult = cv2.erode(img, kernel)
# Shows the result
cv2.namedWindow('Result with n ' + str(n), cv2.WINDOW_NORMAL) # Adjust the window length
cv2.imshow('Result with n ' + str(n), imgResult)
def maximumBoxFilter(n, path_to_image):
img = cv2.imread(path_to_image)
# Creates the shape of the kernel
size = (n,n)
shape = cv2.MORPH_RECT
kernel = cv2.getStructuringElement(shape, size)
# Applies the maximum filter with kernel NxN
imgResult = cv2.dilate(img, kernel)
# Shows the result
cv2.namedWindow('Result with n ' + str(n), cv2.WINDOW_NORMAL) # Adjust the window length
cv2.imshow('Result with n ' + str(n), imgResult)
if __name__ == "__main__":
path_to_image = 'images/africa.tif'
print("Test the function minimumBoxFilter()")
minimumBoxFilter(3, path_to_image)
minimumBoxFilter(5, path_to_image)
minimumBoxFilter(7, path_to_image)
minimumBoxFilter(11, path_to_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
print("Test the function maximumBoxFilter()")
maximumBoxFilter(3, path_to_image)
maximumBoxFilter(5, path_to_image)
maximumBoxFilter(7, path_to_image)
maximumBoxFilter(11, path_to_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
其中路径\到\图像是一个字符串。例如:“images/africa.tif”
我得到以下结果:
具有最小过滤器:
具有最大过滤器:
也许您需要Numpy np.amin()。