📜  Python|图像处理中的形态学运算(结束) |第 2 组

📅  最后修改于: 2022-05-13 01:54:18.746000             🧑  作者: Mango

Python|图像处理中的形态学运算(结束) |第 2 组

在上一篇文章中,指定了Opening运算符,它在膨胀后应用腐蚀运算。它有助于消除图像中的内部噪声。关闭操作类似于打开操作。在合闸操作中,基本前提是合闸是反向进行的分闸。它被简单地定义为使用在打开操作中使用的相同结构元素的膨胀,然后是腐蚀

下面是解释关闭形态操作的Python代码 -

Python3
# Python program 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_CLOSE, 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()


输入帧:

面具:

输出: