Py-geeks / Cooling-Filter

Cooling Filter with Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cooling Effect

Here we are with another filter effect Cooling Effect.
In this session you will know about how to utilize and build lookup_table,splitting RGB channels,
applying mapping to channels & merging channels.
As we are manipulating warming effect hecnce we have to work with Only red & blue channels,
we don't need to distort green channel.
This is an important project to sharpen your skills on openCV.

Tools and Languages:

OpenCV

VS Code

pip

Python


Installation

Use the package manager pip to install cv2 and numpy.

pip install cv2
pip install numpy

Import

Use import keyword to import modules.

import cv2
import numpy as np
from scipy.interpolate import UnivariateSpline

Reading image from file

img = cv2.imread("cat.png")

Steps to be followed:

1.First create a copy of the image to work on.
2.Set original x & y-axis values.
3.Create and set Lookup Table of both channels.
4.Split the channels.
5.Apply Lookuptable-mapping into both channels.
6.For desired output merge the channels.

Step-1:

Class for cooling effect

class CoolingFilter():

Step-2:

Initialize look-up table for curve filter

def __init__(self):
        # create look-up tables for increasing and decreasing a channel
        self.incr_ch_lut = self._create_LUT_8UC1([0, 64, 128, 192, 256],
                                                 [0, 70, 140, 210, 256])
        self.decr_ch_lut = self._create_LUT_8UC1([0, 64, 128, 192, 256],
                                                 [0, 30,  80, 120, 192])

Step-3:

def render(self, img_rgb):
        c_r, c_g, c_b = cv2.split(img_rgb)
        c_r = cv2.LUT(c_r, self.incr_ch_lut).astype(np.uint8)
        c_b = cv2.LUT(c_b, self.decr_ch_lut).astype(np.uint8)
        img_rgb = cv2.merge((c_r, c_g, c_b))


       
        c_h, c_s, c_v = cv2.split(cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV))
        c_s = cv2.LUT(c_s, self.incr_ch_lut).astype(np.uint8)

        return cv2.cvtColor(cv2.merge((c_h, c_s, c_v)), cv2.COLOR_HSV2RGB)

Step-4:

def _create_LUT_8UC1(self, x, y):
        """Creates a look-up table using scipy's spline interpolation"""
        spl = UnivariateSpline(x, y)
        return spl(range(256))

Step-5:

#apply mapping to red channel
y = CoolingFilter()
Cool = y.render(img)

#comparing original vs resized
cv2.imshow('ORIGINAL',img)
cv2.imshow('C

Step-6:

cv2.waitKey(0)
cv2.destroyAllWindows()

Images

Original png Cooled

Developed by

Ashish ku. Behera

About

Cooling Filter with Python


Languages

Language:Python 100.0%