Change language

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

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

Parameters:
-"  image : Input Image array.
-"  cv2.MORPH_CLOSE : Applying the Morphological Closing operation.
-"  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:

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically