Open In App

Python | Morphological Operations in Image Processing (Opening) | Set-1

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

Morphological operations are basic image processing techniques, typically used on binary images (images with only two pixel values: black and white) but can also be applied to grayscale images for shape-based processing to:

  • Analyze and modify shapes or structures.
  • Refine object boundaries and remove small noise.
  • Apply shape-based logic using a structuring element.

They are essential for cleaning and enhancing binary images.

Morphological operations require two inputs: the input image and a structuring element (also called a kernel). These operations apply set-based logic by combining the image and structuring element. The structuring element encodes specific shape attributes and objects in the image are processed based on how they match or interact with this shape.

Opening Operation

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

Opening_Operation

Interpretation of the Equation

  • A∘B: Opening of image A by structuring element B
  • ⊖: Erosion operation
  • ⊕: Dilation operation

It helps removes small white noise (tiny bright spots) from black backgrounds without affecting shape and size of larger white objects. Useful for cleaning internal noise like small specks or dots while preserving the main object’s structure.

Function to Perform Opening Operation

Syntax for opening:

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

Parameters: 

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

Code Example

This Python program captures live video from webcam, detects blue-colored regions using HSV color filtering and then cleans small white noise from the mask using Opening morphological operation. It displays both original noisy mask and cleaned version side by side in real time.

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 Opening to remove small white noise
    opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)

    # Show both original mask and cleaned version
    cv2.imshow('Original Blue Mask', mask)
    cv2.imshow('After Opening (Noise Removed)', opening)
    
    # Press 'a' key to stop
    if cv2.waitKey(1) & 0xFF == ord('a'):
        break

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

Input Frame: 

Original Blue Mask:

After Opening (Noise Removed):

After_Opening

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_OPEN, kernel): Performs Opening (Erosion followed by Dilation) to remove small white noise.
  • 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