官术网_书友最值得收藏!

Implementing a curve filter using lookup tables

Curve filters are computationally expensive because the values of f(x) must be interpolated whenever x does not coincide with one of the prespecified anchor points. Performing this computation for every pixel of every image frame that we encounter would have dramatic effects on performance.

Instead, we make use of a lookup table. Since there are only 256 possible pixel values for our purposes, we need to calculate f(x) only for all the 256 possible values of x. Interpolation is handled by the UnivariateSpline function of the scipy.interpolate module, as shown in the following code snippet:

from scipy.interpolate import UnivariateSpline 
 
def spline_to_lookup_table(spline_breaks: list, break_values: list):
spl = UnivariateSpline(spline_breaks, break_values)
return spl(range(256)

The return argument of the function is a list of 256 elements that contains the interpolated f(x) values for every possible value of x.

All we need to do now is to come up with a set of anchor points, (xi, yi), and we are ready to apply the filter to a grayscale input image (img_gray):

import cv2 
import numpy as np x = [0, 128, 255] y = [0, 192, 255] myLUT = spline_to_lookup_table(x, y) img_curved = cv2.LUT(img_gray, myLUT).astype(np.uint8)

The result looks like this (the original image is on the left, and the transformed image is on the right):

In the next section, we'll design the warming and cooling effect. You will also learn how to apply lookup tables to colored images, and how warming and cooling effects work.

主站蜘蛛池模板: 娱乐| 叙永县| 宁陕县| 龙海市| 宁安市| 西平县| 阳春市| 济南市| 西宁市| 潼关县| 来宾市| 商河县| 华阴市| 白山市| 内丘县| 盐城市| 多伦县| 平原县| 新和县| 临西县| 崇明县| 南溪县| 宜兰县| 二手房| 怀远县| 龙川县| 彭山县| 棋牌| 永康市| 阿城市| 清原| 蓝田县| 礼泉县| 洛川县| 德庆县| 宁强县| 阳江市| 海门市| 涿州市| 资阳市| 德惠市|