dilate
Usage
Dilates images. It is widely used for feature enhancement and shape analysis in image processing and computer vision. Dilatation is a morphological operation that can enhance the bright area in an image. It is usually used to remove small noise, fill holes in an object, or connect adjacent objects. This operation expands the border of the bright area using structure elements (convolution kernel).
Interface
1 | cv2.dilate(src, kernel, anchor=None, iterations=1, borderType=cv2.BORDER_CONSTANT, borderValue=None) |
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
src |
Input image (usually a binary or grayscale image). |
Not null |
Input |
kernel |
Structure elements (usually a matrix) used for dilation. |
Not null |
Input |
anchor |
Anchor, the center of structure elements. The default value is none. |
Tuple type in the (x, y) format x: horizontal offset. Value range: [0, kernel_width–1] y: vertical offset. Value range: [0, kernel_height–1] kernel_width and kernel_height are determined by the kernel parameter. |
Input |
iterations |
Number of dilation iterations. The default value is 1. Increasing the value further dilates the image. |
Non-negative integer (usually 1 or larger) |
Input |
borderType |
Border type. The default value is cv2.BORDER_CONSTANT. KPCV supports only the BORDER_CONSTANT type. |
cv2.BORDER_* constant, such as cv2.BORDER_CONSTANT, cv2.BORDER_REPLICATE, and cv2.BORDER_REFLECT |
Input |
borderValue |
Constant value for border padding. The default value is 0. This parameter is valid only when borderType is set to BORDER_CONSTANT. |
Float or int number |
Input |
Return Values
- Success: KP_CV_SUCCESS
- Failure: an error code
Error Codes
Error Code |
Description |
|---|---|
INVALID_PARAM_MSG |
The src parameter has a null pointer. |
NOT_SUPPORT_MSG |
src is not a 3-channel uint8 image; the input image is incorrect; the type is not supported by borderType; or the input kernel is incorrect. |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 | import numpy as np import cv2 # Create a 5x5 image. src = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]], dtype=np.uint8) # Kernel of the dilation operation kernel = np.ones((3, 3), np.uint8) dst = cv2.dilate(src, kernel, iterations=1) print(dst) |
Output:
1 2 3 4 5 | [[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]] |