Python | Morphological Operations in Image Processing (Gradient) | Set-3
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: image and a structuring element (kernel), which defines shape logic applied to the image.
In the previous article, we covered Opening and Closing operations. In this one, we’ll explore Gradient operation a simple way to highlight object edges without affecting their internal details.
Gradient Operation
Gradient is a morphological operation that involves two steps: Dilation − Erosion.

Interpretation of the Equation
- A: input image
- B: structuring element (kernel)
- ⊕: Dilation
- ⊖: Erosion
Gradient highlights the boundaries of objects by finding difference between their dilated and eroded versions. It’s useful for detecting edges and outlining shapes without affecting their internal regions.
Function to perform Gradient Operation
Syntax for Gradient:
cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel)
Parameters:
- image: Input binary or grayscale image
- cv2.MORPH_GRADIENT: Specifies the gradient operation
- kernel: Structuring element defining dilation/erosion shape
Code Example
This code captures live video from webcam, detects blue areas in each frame and applies Gradient operation to highlight their edges. It displays both original mask and edge-highlighted result.
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 Gradient to highlight object boundaries
gradient = cv2.morphologyEx(mask, cv2.MORPH_GRADIENT, kernel)
# Show both original mask and result after gradient
cv2.imshow('Original Blue Mask', mask)
cv2.imshow('After Gradient (Edges Highlighted)', gradient)
# Press 'a' key to stop
if cv2.waitKey(1) & 0xFF == ord('a'):
break
# Clean up
cv2.destroyAllWindows()
screenRead.release()
Output:

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_GRADIENT, kernel): Performs Gradient to highlight edges of objects.
- cv2.waitKey(1) & 0xFF == ord('a'): Keeps looping until 'a' key is pressed.
- cv2.destroyAllWindows(): Closes all OpenCV windows.
- screenRead.release(): Releases webcam resource.