- OpenCV 4 with Python Blueprints
- Dr. Menua Gevorgyan Arsen Mamikonyan Michael Beyeler
- 247字
- 2021-06-24 16:49:58
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.
- Mastering OpenLayers 3
- AngularJS Testing Cookbook
- 在最好的年紀學Python:小學生趣味編程
- Vue.js 2 and Bootstrap 4 Web Development
- C和C++安全編碼(原書第2版)
- BIM概論及Revit精講
- PLC應用技術(三菱FX2N系列)
- C語言程序設計
- 后臺開發(fā):核心技術與應用實踐
- Delphi開發(fā)典型模塊大全(修訂版)
- 從零學Java設計模式
- Java EE架構設計與開發(fā)實踐
- C/C++代碼調(diào)試的藝術(第2版)
- Microsoft XNA 4.0 Game Development Cookbook
- Python數(shù)據(jù)科學實戰(zhàn)