Python | Morphological operations in image processing (discovery) | Set-1

Disclosure — erosion operation followed by expansion operation.

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

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

Below is the Python code explaining the start of the morphological operation —

# Python program for illustration
# Opening 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 the capture was 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_BGR2HSV )

 

# 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)

  

# open function definition

# over image and structuring element

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

 

# Mask and open operation

# appears in window

cv2.imshow ( ` Mask` , mask)

cv2 .imshow ( `Opening` , opening)

  

  # 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:

Output frame:

The system recognizes a certain blue book as input information as it removes and simplifies internal noise in the area of ​​interest using the open function.