Designing Image Filters using OpenCV(Part 2)

Learn to make photo filters like the ones present in image processing Softwares like Abode Photoshop using OpenCV in python.

Vardan Agarwal
DataSeries
4 min readFeb 16, 2020

--

In the first article, we implemented quite a lot of image editing filters. In this one, we are going to implement even better filters so let’s get started right away.

Sepia

Let’s start this article with the ever famous sepia filter. Almost every image editing software has this filter and I’ve never understood why so I checked it out and found this.

The word “sepia” refers to the name of the rich brown pigment that was widely used by photographers back in the early days of photography. Sepia effect gives your images a warm brownish tone. Sepia filter improves the general look and feel of your image.

So let’s implement it. All we got to do is to multiply a special matrix with our image which can be done using cv2.transform and normalize the values above 255 to 255.

Splash

In the Splash filter, only certain colors are left as it is and the rest is converted to grayscale. To perform this we will use cv2.inRange in the HSV color space. This can be used to form a mask of all the pixels having values within that range and those pixels are taken as it is using cv2.bitwise_and. For the grayscale part, we find the inverse of the mask and convert it to grayscale. They are then combined using cv2.bitwise_or.

Duo-Tone

In the duo-tone filter, a screen of a particular color is added giving the effect that the image was clicked with a paper of that color against the lens. For this exponential function was used on a particular channel and the values of other channels were set to zero. In the exponential function, the change in values is less at first and more later on just like exponents. Like some of the filters in the previous article, we will use cv2.LUT after creating a lookup table. The values of the lookup table are shown in the graph below.

Figure showing the values of the table created.

Cartooning

Image is taken from here

What comes to mind when we think of a cartoon? The picture above? So can we convert our images to Mickey Mouse? Well no, but we can try to model our images with the characteristics of cartoons. They are pretty smooth with not a lot of color variation and pretty well-defined edges. For this task, we can use cv2.edgePreservingFilter or cv2.bilateralFilter which reduces the number of colors used in the images while maintaining the edges. The bilateral filter is pretty slow so we will use the first one. To introduce edges we can use cv2.canny if we want thin edges or cv2.adaptiveThreshold after blurring for thick edges.

Emboss

So what is an emboss effect? This is what Google defines emboss as:

carve, mould, or stamp a design on (a surface or object) so that it stands out in relief.

Now, what is image embossing? From Wikipedia:

Image embossing is a computer graphics technique in which each pixel of an image is replaced either by a highlight or a shadow, depending on light/dark boundaries on the original image. Low contrast areas are replaced by a gray background.

Now that we have understood what image embossing is we need to implement it using OpenCV. If it seems tough then you are wrong because all we have to do is to create a kernel and apply convolution using cv2.conv2D and add a value of 128 to all pixels.

The kernels we can use

Have a look at the different kernels and you can easily figure out a relationship. We can even join the results of two or more embossed results to create a stronger emboss.

Pencil Sketch

This is by far my favorite effect of both of the parts even more because there is a predefined function to perform this in OpenCV. Use the cv2.pencilSketch function and we get pencil sketches in both color and grayscale.

Hope you enjoyed this effects and filters and you create some of your own. I will soon write an article for making frames like vignette, round circle, wooden frame and more so see you then.

--

--