GaussianBlur
Usage
Implements Gaussian blurs on an image to smooth the image and remove noise and details. Through Gaussian filtering, edges and details in the image are softened. This is applicable to scenarios such as edge detection and image preprocessing.
Interface
1 | cv2.GaussianBlur(src, ksize, sigmaX, sigmaY=0, borderType=cv2.BORDER_DEFAULT) |
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
src |
Input image. |
Not null |
Input |
ksize |
Size of the Gaussian kernel. The value must be a positive odd number, such as (3, 3) and (5, 5). |
(Width, height). The width and height are odd numbers greater than 1. |
Input |
sigmaX |
Gaussian standard deviation along the X direction, which controls the blur degree. |
(0, inf) |
Input |
sigmaY |
Gaussian standard deviation along the Y direction. The default value is 0. The automatically calculated value is the same as that of sigmaX. |
(0, inf) |
Input |
borderType |
Border type, which is used to process border pixels. The default value is cv2.BORDER_DEFAULT. |
cv2.BORDER_DEFAULT, cv2.BORDER_REPLICATE, cv2.BORDER_REFLECT, cv2.BORDER_REFLECT_101, cv2.BORDER_WRAP, cv2.BORDER_CONSTANT, etc. |
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 |
The type is not supported by borderType; the ksize value is incorrect; or src is not a unit8 image. |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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) # Define the size of the Gaussian blur kernel. ksize = (3, 3) # Define the standard deviation of the Gaussian blur kernel. sigmaX = 1 sigmaY = 1 dst = cv2.GaussianBlur(src, ksize, sigmaX, sigmaY, borderType=cv2.BORDER_DEFAULT) print(dst) |
Output:
1 2 3 4 5 | [[0 0 1 0 0] [0 0 1 0 0] [1 1 1 1 1] [0 0 1 0 0] [0 0 1 0 0]] |