Designing Image Filters using OpenCV (Part 1)

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

Vardan Agarwal
DataSeries
5 min readFeb 16, 2020

--

We all have edited our images to perhaps improve the white balance, increase warmth, use different filters or even to crop out a person we don’t like. Have you ever wondered how all the software available did this or how we could do this ourselves with code? If so then you are in the right place. In this article and it's second part we will leverage the power of OpenCV to edit our images in as many as fourteen ways and another article, for creating frames for our images. Don’t worry because this does not include the usual stuff like sharpening, cropping and blurring the images but instead filters like summer, 60TVS, splash, cartooning, embossing, pencil sketch, sepia and many more where most of them are inspired by Abode Photoshop Express itself.

Bright

To increase the brightness of our image we can use HSV (Hue, Saturation, and Value) color space also known as HSB (Hue, Saturation, and Brightness) which is closer to how humans perceive color. In Opencv their maximum values of Hue range from 0–180 and of Saturation and Brightness from 0–255.

HSV color scale in OpenCV.

This image shows different colors obtained by changing the values of HSV. Hue is changed from top to bottom while Saturation and Brightness change from left to right. The right side of the image is much brighter than the middle part. So to increase the brightness of our images we need to increase the Saturation and Brightness.

In a nutshell in the code we convert the image to HSV color space, change the type to float to prevent loss after multiplying, upscale channels 1 and 2, convert all pixels values greater than 255 to 255, convert the type back to integer, and finally convert it back to BGR color space used in OpenCV before displaying it.

Details Enhancement

Well, this is sharpening and know I said that this article won’t consist of sharpening but the only reason I have included this is to show the difference between a predefined function cv2.detailEnhance against the normal sharpening done in image processing using a kernel and to show the brilliant results, we get using it.

Invert

This was the simplest to implement. I am sure all of us have used this filter once in our lives and have been scared seeing our faces turn blue and our hair white. All we got to do is to invert the values of pixels i.e. subtract each pixel value by 255. In OpenCV, we use cv2.bitwise_not for this task because inverting the bits is subtracting it by 255.

Summer

We associate summer with warmth and saturation where the impact of the red channel is more than the blue channel. So that is exactly what we are going to do in it. We are going to use the gamma function for this. Have a look at the graph below to get an idea about its working.

Gamma function values for different values of gamma.

For values of gamma above 1, the values are increased and for below 1, the pixel values are decreased. It is implemented in OpenCV by creating a lookup table and using cv2.LUT() . The values of the Red channel of BGR and Saturation of HSV is increased by taking a value of gamma greater than 1 and the values of the Blue channel is decreased by taking a value less than 1. You can play around with different values of gamma to see different results of summer filter or even make a trackbar for it.

Winter

Now that we have created a summer filter it is easy to create a winter filter which is the inverse of it using gamma function only. We associate winters with coldness and gloom. So we need to increase the values of the blue channel, decrease those of red channel and saturation using the appropriate values of gamma.

Daylight

To implement daylight filter we need to increase the light and this can be achieved in a color space called HLS (Hue, Lightness, and Saturation). All we need to do is to convert the image to HLS color space increase the value of the Lightness channel by multiplying it by some value greater than 1 and convert it back to BGR.

60TVs

In the 60s there used to black and white televisions and there was a lot of noise due to loss of data. To create this filter we convert our image to grayscale and then randomly induce a lot of noise by adding or subtracting a random number and make sure to stay within our limit of 0 to 255.

High Contrast

The high contrast filter in Abode Photoshop Express is a grayscale filter where the histogram of the image is stretched to the edges. This can again be achieved using cv2.LUT and we will create a lookup table using np.interp which is used to obtain piecewise linear interpolation between data points.

Figure showing the values of the table created.

This is it from this article. See you in the second part, where we will implement the remaining filters which are even better than the first ones.

--

--