It is very easy to use cv2.filter2D() to implement image filtering in python opencv. In this tutorial, we will use an example to show you how to do.
1.Read an image
import numpy as np import cv2 #read image img_src = cv2.imread('sample.jpg')
2.Define a kernel
#prepare the 5x5 shaped filter kernel = np.array([[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]]) kernel = kernel/sum(kernel)
3.Apply image filtering using kernel
#filter the source image img_rst = cv2.filter2D(img_src,-1,kernel)
3.Save filtered image
#save result image cv2.imwrite('result.jpg',img_rst)
Run this code, you will see this result.