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.
Use the package manager pip to install cv2 and numpy.
pip install cv2
pip install numpy
Use import keyword to import modules.
import cv2
import numpy as np
from scipy.interpolate import UnivariateSpline
img = cv2.imread("cat.png")
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.
Class for cooling effect
class CoolingFilter():
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])
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)
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))
#apply mapping to red channel
y = CoolingFilter()
Cool = y.render(img)
#comparing original vs resized
cv2.imshow('ORIGINAL',img)
cv2.imshow('C
cv2.waitKey(0)
cv2.destroyAllWindows()