cv2.filter2D
时间: 2023-10-19 18:08:18 浏览: 46
cv2.filter2D is a function in OpenCV that applies a convolution operation on an image using a kernel matrix. The function takes in an input image and a kernel matrix and returns the filtered image.
Syntax: cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]])
Parameters:
- src: input image
- ddepth: depth of the output image; if -1, the output image will have the same depth as the input image
- kernel: the kernel matrix used for convolution
- dst: output image
- anchor: anchor point of the kernel; by default, (-1, -1) which corresponds to the center of the kernel
- delta: value added to the filtered image; by default, 0
- borderType: type of border replication; by default, cv2.BORDER_DEFAULT
Example:
```
import cv2
import numpy as np
img = cv2.imread('image.jpg', 0)
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
cv2.imshow('Original Image', img)
cv2.imshow('Filtered Image', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
In this example, we read an image in grayscale format, define a 5x5 kernel matrix with all elements equal to 1/25, and apply the filter2D function to the input image using the defined kernel matrix. The filtered image is then displayed using the imshow function.