Python|图像处理中的形态学运算(开幕) |第一组
形态学操作用于提取对区域形状的表示和描述有用的图像分量。形态学操作是一些依赖于图片形状的基本任务。它通常在二进制图像上执行。它需要两个数据源,一个是输入图像,另一个是结构化组件。形态运算符将输入图像和结构组件作为输入,然后使用集合运算符组合这些元素。输入图像中的对象根据图像形状的属性进行处理,这些属性在结构化组件中进行编码。
开放类似于侵蚀,因为它倾向于从前景像素区域的边缘移除明亮的前景像素。运算符的作用是保护与结构化成分相似的前景区域,或者可以完全包含结构化成分的前景区域,同时取出每个其他区域的前景像素。开运算用于去除图像中的内部噪声。
开运算是腐蚀运算,然后是膨胀运算。
Syntax: cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
Parameters:
-> image: Input Image array.
-> cv2.MORPH_OPEN: Applying the Morphological Opening operation.
-> kernel: Structuring element.
下面是解释打开形态学运算的Python代码——
Python3
# Python program to illustrate
# Opening morphological operation
# on an image
# organizing imports
import cv2
import numpy as np
# return video from the first webcam on your computer.
screenRead = cv2.VideoCapture(0)
# loop runs if capturing has been initialized.
while(1):
# reads frames from a camera
_, image = screenRead.read()
# Converts to HSV color space, OCV reads colors as BGR
# frame is converted to hsv
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# defining the range of masking
blue1 = np.array([110, 50, 50])
blue2 = np.array([130, 255, 255])
# initializing the mask to be
# convoluted over input image
mask = cv2.inRange(hsv, blue1, blue2)
# passing the bitwise_and over
# each pixel convoluted
res = cv2.bitwise_and(image, image, mask = mask)
# defining the kernel i.e. Structuring element
kernel = np.ones((5, 5), np.uint8)
# defining the opening function
# over the image and structuring element
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
# The mask and opening operation
# is shown in the window
cv2.imshow('Mask', mask)
cv2.imshow('Opening', opening)
# Wait for 'a' key to stop the program
if cv2.waitKey(1) & 0xFF == ord('a'):
break
# De-allocate any associated memory usage
cv2.destroyAllWindows()
# Close the window / Release webcam
screenRead.release()
输入帧:
面具:
输出帧:
系统将定义的蓝皮书识别为输入,在Opening函数的帮助下消除并简化了感兴趣区域的内部噪声。