In this section, we will see implementations of a few filters that we saw in Chapter 2, Filters and Features, using the OpenCV library. As we saw in the previous chapter, filters are created by convolution of a kernel with an image. To do this operation, OpenCV provides the cv2.filter2D() function, which takes the image, destination image depth, and kernel as input. Using this we can create our own filter.
Consider the following example:
>>> import cv2>>> import numpy as np>>> img = cv2.imread("image.jpg")>>> ker = np.array([[1, 1, 1],... [1, 1, 1],... [1, 1, 1]])>>> new_img = cv2.filter2D(img,-1,ker)>>> cv2.imwrite("filter.jpg", new_img)>>> cv2.imshow("filter", new_img)
The kernel is used as follows:
The following figure is the ...