Python | Morphological operations in image processing (closure) | Set-2



Syntax: cv2.morphologyEx (image, cv2.MORPH_CLOSE, kernel)

Parameters:
– & gt;  image : Input Image array.
– & gt;  cv2.MORPH_CLOSE : Applying the Morphological Closing operation.
– & gt;  kernel : Structuring element.

Below is the Python code explaining how to close the morphological operation —

# Python program for illustration
# Closing the morphological operation
# on the image

 
# organization of import

import cv2 

import numpy as np 

  
# return video from the first webcam to your computer.

screenRead = cv2. VideoCapture ( 0 )

 
# the loop starts if there was a capture initialized.

while ( 1 ):

# reads frames from the camera

_, image = screenRead.read ()

 

  # Converts to HSV color space, OCV reads colors as BGR

  # the frame is converted to hsv

hsv = cv2.cvtColor (image, cv2.COLOR_BGR2H SV)

 

# define the masking range

blue1 = np.array ([ 110 , 50 , 50 ])

blue2 = np.array ([ 130 , 255 , 255 ])

 

# mask initialization for

  # collapsed above the input image

  mask = cv2.inRange (hsv, blue1, blue2)

 

# bitwise_and transfer

# every pixel is meandering

res = cv2.bitwise_and (image, image, mask = mask)

 

# core definition i.e. structuring element

kernel = np.ones (( 5 , 5 ), np.uint8)

  

# definition of the closing function

# over image and structuring element

closing = cv2.morphologyEx (mask, cv2.MORPH_OPEN, kernel)

 

# Mask and close operation

# is displayed I`m in a window

cv2.imshow ( `Mask` , mask)

  cv2.imshow ( `Closing` , closing)

 

# Wait for the "a" key to stop the program

if cv2.waitKey ( 1 ) & amp;  0xFF = = ord ( `a` ):

break

  
# Deallocate any associated memory usage
cv2.destroyAllWindows ()

 
# Close window / release webcam
screenRead.release ()

Input frame:

Mask:

Exit: