Gradient | Morphological Transformations in OpenCV in C++
In this article, a Morphological Operator called Gradient is discussed. The gradient is defined as the difference between the Dilation and Erosion of an image. Every pixel inside the Gradient image represents the contrast intensity in the neighborhood of a pixel. It is used for image segmentation and edge detection. It is of two types:
- Internal Gradient: It enhances the internal boundaries of objects brighter than the background and for binary image generates a mask of the internal boundaries of the foreground image object.
- External Gradient: It enhances the external boundaries of objects darker than their background.
morphologyEx (src, dst, op, kernel, anchor, iterations, borderType, borderValue)
- src: It is the input image.
- dst: It is the output image.
- op: Type of morphological operation.
- kernel: Structuring element used for Closing.
- anchor: Anchor position inside the structuring element. The default value is [-1, -1} signifying position as the center of the structuring element.
- iterations: Number of times Closing is applied.
- borderType: Type of border ( BORDER_CONSTANT, BORDER_REPLICATE, etc.)
- borderValue: Border value
- Return: Output Image (Mat Object)
Gradient operator is given the expression:
Below is the C++ program to demonstrate the Gradient Morphological Operation: