O'Reilly logo

Computer Vision with Python 3 by Saurabh Kapur

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Filters

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required