Reader small image

You're reading from  Learning OpenCV 4 Computer Vision with Python 3 - Third Edition

Product typeBook
Published inFeb 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781789531619
Edition3rd Edition
Languages
Tools
Right arrow
Authors (2):
Joseph Howse
Joseph Howse
author image
Joseph Howse

Joseph Howse lives in a Canadian fishing village, where he chats with his cats, crafts his books, and nurtures an orchard of hardy fruit trees. He is President of Nummist Media Corporation, which exists to support his books and to provide mentoring and consulting services, with a specialty in computer vision. On average, in 2015-2022, Joseph has written 1.4 new books or new editions per year for Packt. He also writes fiction, including an upcoming novel about the lives of a group of young people in the last days of the Soviet Union.
Read more about Joseph Howse

Joe Minichino
Joe Minichino
author image
Joe Minichino

Joe Minichino is an R&D labs engineer at Teamwork. He is a passionate programmer who is immensely curious about programming languages and technologies and constantly experimenting with them. Born and raised in Varese, Lombardy, Italy, and coming from a humanistic background in philosophy (at Milan's Università Statale), Joe has lived in Cork, Ireland, since 2004. There, he became a computer science graduate at the Cork Institute of Technology.
Read more about Joe Minichino

View More author details
Right arrow

Appendix A: Bending Color Space with the Curves Filter

Starting in Chapter 3, Processing Images with OpenCV, our Cameo demo application incorporated an image processing effect called curves, which it uses to emulate the color bias of certain photo films. This Appendix describes the concept of curves and their implementation using SciPy.

Curves are a technique for remapping colors. With curves, a channel's value at a destination pixel is a function of (only) the same channel's value at the source pixel. Moreover, we do not define functions directly; instead, for each function, we define a set of control points that the function must fit by means of interpolation. In pseudocode, for a BGR image, we have the following:

dst.b = funcB(src.b) where funcB interpolates pointsB
dst.g = funcG(src.g) where funcG interpolates pointsG
dst.r = funcR(src.r) where funcR interpolates pointsR...

Formulating a curve

Our first step toward curve-based filters is to convert control points into a function. Most of this work is done for us by a SciPy function called scipy.interp1d, which takes two arrays (x and y coordinates) and returns a function that interpolates the points. As an optional argument to scipy.interp1d, we may specify the kind interpolation; supported options include 'linear', 'nearest', 'zero', 'slinear' (spherical linear), 'quadratic', and 'cubic'. Another optional argument, bounds_error, may be set to False to permit extrapolation as well as interpolation.

Let's edit the utils.py script that we use with our Cameo demo and add a function that wraps scipy.interp1d with a slightly simpler interface:

def createCurveFunc(points):
"""Return a function derived from control points...

Caching and applying a curve

By now, we can get the function of a curve that interpolates arbitrary control points. However, this function might be expensive. We don't want to run it once-per-channel, per -pixel (for example, 921,600 times per frame if applied to three channels of 640 x 480 video). Fortunately, we are typically dealing with just 256 possible input values (in 8 bits per channel) and we can cheaply precompute and store that many output values. Then, our per-channel, per-pixel cost is just a lookup of the cached output value.

Let's edit the utils.py file and add a function that will create a lookup array for a given function:

def createLookupArray(func, length=256):
"""Return a lookup for whole-number inputs to a function.

The lookup values are clamped to [0, length - 1].

"""
if func is None:
return None
...

Designing object-oriented curve filters

Since we cache a lookup array for each curve, our curve-based filters have data associated with them. Thus, we will implement them as classes, not just functions. Let's make a pair of curve filter classes, along with some corresponding higher-level classes that can apply any function, not just a curve function:

  • VFuncFilter: This is a class that is instantiated with a function, which it can then apply to an image using apply. The function is applied to the V (value) channel of a grayscale image or to all the channels of a color image.
  • VCurveFilter: This is a subclass of VFuncFilter. Instead of being instantiated with a function, it is instantiated with a set of control points, which it uses internally to create a curve function.
  • BGRFuncFilter: This is a class that is instantiated with up to four functions, which it can then apply to...

Emulating photo films

A common use of curves is to emulate palettes that were common in pre-digital photography. Every type of photo film has its own unique rendition of color (or gray), but we can generalize some of the differences from digital sensors. Film tends to suffer a loss of detail and saturation in shadows, whereas digital tends to suffer these failings in highlights. Also, film tends to have uneven saturation across different parts of the spectrum, so each film has certain colors that pop or jump out.

Thus, when we think of good-looking film photos, we might think of scenes (or renditions) that are bright and that have certain dominant colors. At the other extreme, maybe we remember the murky look of an underexposed roll of film that couldn't be improved much by the efforts of the lab technician.

In this section, we are going to create four different film-like...

Summary

Building on the scipy.interp1d function, we have implemented a collection of curve filters that are efficient (due to the use of lookup arrays) and easily extensible (due to an object-oriented design). Our work has included special-purpose curve filters that can make digital images look more like film shots. These filters can be readily integrated into an application such as Cameo, as demonstrated by the use of our Portra film emulation filter in Chapter 3, Processing Images with OpenCV.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning OpenCV 4 Computer Vision with Python 3 - Third Edition
Published in: Feb 2020Publisher: PacktISBN-13: 9781789531619
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Joseph Howse

Joseph Howse lives in a Canadian fishing village, where he chats with his cats, crafts his books, and nurtures an orchard of hardy fruit trees. He is President of Nummist Media Corporation, which exists to support his books and to provide mentoring and consulting services, with a specialty in computer vision. On average, in 2015-2022, Joseph has written 1.4 new books or new editions per year for Packt. He also writes fiction, including an upcoming novel about the lives of a group of young people in the last days of the Soviet Union.
Read more about Joseph Howse

author image
Joe Minichino

Joe Minichino is an R&D labs engineer at Teamwork. He is a passionate programmer who is immensely curious about programming languages and technologies and constantly experimenting with them. Born and raised in Varese, Lombardy, Italy, and coming from a humanistic background in philosophy (at Milan's Università Statale), Joe has lived in Cork, Ireland, since 2004. There, he became a computer science graduate at the Cork Institute of Technology.
Read more about Joe Minichino