在上一篇文章中, 指定了Opening运算符, 该运算符在膨胀后执行腐蚀操作。它有助于消除图像中的内部噪点。闭幕与打开操作类似。在关闭操作中, 基本前提是关闭是反向执行的。它被简单地定义为扩张后侵蚀使用与打开操作相同的结构元素。
文章图片
语法:cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)参数:-> image:输入图像数组。 -> cv2.MORPH_CLOSE:应用形态学关闭操作。 -> 内核:结构元素。以下是解释关闭形态运算的Python代码-
# Python programe to illustrate
# Closing 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 closing function
# over the image and structuring element
closing = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)# The mask and closing operation
# is shown in the window
cv2.imshow( 'Mask' , mask)
cv2.imshow( 'Closing' , closing)# 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()
【Python图像处理中的形态运算(关闭)|S2】输入框:
文章图片
面具:
文章图片
输出如下:
文章图片
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- Python图像处理中的形态运算(梯度)|S3
- Python模块介绍和用法示例
- Python使用Tkinter进行消息编码和解码
- Python Tkinter使用Canvas类创建不同的形状
- Python Tkinter使用Canvas类创建不同类型的线
- Python Tkinter Entry小部件用法实例
- Python Tkinter无框窗口用法示例
- 超星尔雅答案|王艳茹网课答案创业基础超星尔雅答案2021
- 二进制方式部署k8s集群(超详细)