Deconstructing the code

When you run the preceding program, you will see a window with a video stream from the webcam. If you press S, the video stream will change to sketch mode, and you will see its pencil-like outline. If you press C, you will see the color-cartoonized version of the input stream. If you press any other key, it will return to the normal mode.

Let's look at the cartoonize_image function and see how we did it. First, we converted the image to a grayscale image, and ran it through a median filter. Median filters are very good at removing salt and pepper noise. This is the kind of noise where you see isolated black or white pixels in the image. It is common in webcams and mobile cameras, so we need to filter it out before ...

Get OpenCV 3.x with Python By Example - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.