Open In App

Python | Morphological Operations in Image Processing (Closing) | Set-2

Last Updated : 11 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Morphological operations are shape-based image processing techniques, mainly used on binary images to clean noise, refine boundaries and analyze structures. They require two inputs: the image and a structuring element (kernel), which defines the shape logic applied to the image.

In the previous article, we covered the Opening operation, which applies erosion followed by dilation to remove internal noise. In this article, we’ll focus on the Closing operation.

Closing Operation

Closing is a morphological operation that involves two steps: Dilation -> followed by -> Erosion.

Closing_morphologicalOperation

Interpretation of the Equation

  • A●B: Closing of image A by structuring element B
  • ⊕: Dilation operation
  • ⊖: Erosion operation

Closing helps fill small black holes or gaps in white objects without altering their overall shape. It’s useful for closing small dark spots or breaks inside objects, preserving their outer structure.

Function to perform Closing Operation

Syntax for closing:

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

Parameters:

  • image: Input binary or grayscale image
  • cv2.MORPH_CLOSE: Specifies the morphological closing operation
  • kernel: Structuring element (defines how dilation/erosion is applied)

Code Example

This code captures live video from webcam, detects blue regions in the frame and applies Closing operation to fill small black holes inside the detected areas. It displays both original mask and result after applying Closing.

Python
import cv2  
import numpy as np  

# Start capturing from webcam  
screenRead = cv2.VideoCapture(0)

while True:
    # Capture a single frame from the webcam
    _, image = screenRead.read()
    
    # Convert to HSV color space
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    
    # Define blue color range
    blue1 = np.array([110, 50, 50])
    blue2 = np.array([130, 255, 255])
    
    # Create binary mask for blue color
    mask = cv2.inRange(hsv, blue1, blue2)

    # Define 5x5 structuring element (kernel)
    kernel = np.ones((5, 5), np.uint8)
    
    # Apply Closing to fill small black holes in the object
    closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
   
    # Show both original mask and result after closing
    cv2.imshow('Original Blue Mask', mask)
    cv2.imshow('After Closing (Holes Filled)', closing)
    
    # Press 'a' key to stop
    if cv2.waitKey(1) & 0xFF == ord('a'):
        break

# Clean up
cv2.destroyAllWindows()
screenRead.release()

Input Frame:

Input_Frame

Original Blue Mask:

Original_BlueMask

After Closing (Holes Filled):

After_Closing

Explanation:

  • cv2.VideoCapture(0): Starts webcam stream.
  • screenRead.read(): Reads a frame from webcam.
  • cv2.cvtColor(image, cv2.COLOR_BGR2HSV): Converts BGR image to HSV color space.
  • np.array([110, 50, 50]) and np.array([130, 255, 255]): Define HSV range for detecting blue color.
  • cv2.inRange(hsv, blue1, blue2): Creates a binary mask where blue areas are white (255), others are black (0).
  • np.ones((5, 5), np.uint8): Creates a 5×5 kernel (structuring element) of type `uint8`.
  • cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel): Performs Closing(Dilation followed by Erosion) to fill small black holes.
  • cv2.waitKey(1) & 0xFF == ord('a'): Keeps looping until 'a' key is pressed.
  • cv2.destroyAllWindows(): Closes all OpenCV windows.
  • screenRead.release(): Releases webcam resource.

Practice Tags :

Similar Reads