cv2.filter2D
时间: 2023-10-29 07:40:05 浏览: 40
cv2.filter2D is a function in the OpenCV library used for convolving a kernel with an image. It performs a 2D spatial convolution between the input image and the kernel. The function is used for various image processing tasks such as blurring, sharpening, edge detection, and noise reduction.
Syntax:
cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]])
Parameters:
1. src: Input image (numpy array).
2. ddepth: Desired depth of the output image (e.g. cv2.CV_8U, cv2.CV_32F, etc).
3. kernel: Convolution kernel (numpy array).
4. dst (optional): Output image (numpy array).
5. anchor (optional): Anchor point from where the kernel is applied (by default (-1,-1) which means that the kernel is centred).
6. delta (optional): Value added to the output after the convolution.
7. borderType (optional): Border mode used when the kernel goes beyond the image boundaries.
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 are applying a 5x5 averaging kernel to an input image and displaying the original and filtered images. The output is a blurred image as the kernel is designed to average the pixel values in the neighbourhood.