In the previous articles, the Opening operation and the Closing operations were specified. In this article, another morphological operation is elaborated that is Gradient. It is used for generating the outline of the image. There are two types of gradients, internal and external gradient. The internal gradient enhances the internal boundaries of objects brighter than their background and external boundaries of objects darker than their background. For binary images, the internal gradient generates a mask of the internal boundaries of the foreground image objects.
Syntax: cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel)
Parameters:
-> image: Input Image array.
-> cv2.MORPH_GRADIENT: Applying the Morphological Gradient operation.
-> kernel: Structuring element.
Below is the Python code explaining Gradient Morphological Operation –
# Python programe to illustrate # Gradient morphological operation # on input frames # organizing imports import cv2 import numpy as np # return video from the first webcam on your computer. screenRead = cv2.VideoCapture(0) # loop runs if capturing has been initialized. while(1): # reads frames from a camera _, image = screenRead.read() # Converts to HSV color space, OCV reads colors as BGR # frame is converted to hsv hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # defining the range of masking blue1 = np.array([110, 50, 50]) blue2 = np.array([130, 255, 255]) # initializing the mask to be # convoluted over input image mask = cv2.inRange(hsv, blue1, blue2) # passing the bitwise_and over # each pixel convoluted res = cv2.bitwise_and(image, image, mask = mask) # defining the kernel i.e. Structuring element kernel = np.ones((5, 5), np.uint8) # defining the gradient function # over the image and structuring element gradient = cv2.morphologyEx(mask, cv2.MORPH_GRADIENT, kernel) # The mask and closing operation # is shown in the window cv2.imshow('Gradient', gradient) # Wait for 'a' key to stop the program if cv2.waitKey(1) & 0xFF == ord('a'): break # De-allocate any associated memory usage cv2.destroyAllWindows() # Close the window / Release webcam screenRead.release() |
Result:

The output image frame shows the outline generated over the blue book and the blue object in top left.
Recommended Posts:
- Python | Morphological Operations in Image Processing (Opening) | Set-1
- Python | Morphological Operations in Image Processing (Closing) | Set-2
- Image segmentation using Morphological operations in Python
- Morphological operations in MATLAB
- Point Processing in Image Processing using Python-OpenCV
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- Image processing with Scikit-image in Python
- Image Processing in MATLAB | Fundamental Operations
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
- Image Processing in Java | Set 7 (Creating a random pixel image)
- Image Processing in Java | Set 8 (Creating mirror image)
- Image Processing in Java | Set 11 (Changing orientation of image)
- Image Processing in Java | Set 10 ( Watermarking an image )
- Image Edge Detection Operators in Digital Image Processing
- PyQtGraph – Setting Predefined Gradient to Image View
- Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection)
- Image Processing without OpenCV | Python
- Python - Blood Cell Identification using Image Processing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

