Color correction using LibCapy

In this article I'll show how to make a CLI application to perform color correction based on a QP203 color chart. The details about the color correction itself is explained in this article. This application have three functionalities: extract a QP203 color chart from a picture; use the extracted color chart to correct the colors of the picture; apply the same color correction to different pictures. It is then possible with this application to correct the colors to match those of the reference QP203 or to match those of another picture; and to perform the correction on pictures including or not the QP203 color chart (i.e. using one shot including the card for reference and correcting another shot without the card but in the same sooting environment).

(The complete code is available at the end of the article)

Define a structure to hold the application data.

The structure of the main() body looks as usual, create the app instance and wrap the body in a try/catch block:

The command line arguments definition is as follow:

Then we decode the arguments and check them:

And we call the appropriate function depending on the mode of the application.

The exception management is setup as follow.

An initialisation function is made to commonalise the code shared by all three modes: memorising the verbose flag and loading the input image.

The extraction itself is done in its own function as it will be reused in correction mode.

The extraction mode extracts the color chart at given coordinates from the image in argument and save it.

The correction mode first extracts the color chart at given coordinates from the image in argument.

Then it creates the reference chart. It is either loaded from a file resulting from a previous extraction (when we want to match a picture to another picture), or it is the reference values of the QP203 color chart.

Next, it matches the brightness of the reference color chart to the one of the image color chart chart to avoid burnt colors in underexposed pictures.

It can now calculate the color correction matrix.

If the user requested to save the corrected image, apply the color correction matrix to the original image and save it.

Finally if the user requested to save the color correction matrix, do it.

In apply mode, we load a color correction matrix (created with the correction mode) and apply it to another picture.

Everything put together, it becomes:

Compile with the following Makefile:

Example of use:

See the related article for examples of corrected pictures.

in All, C programming, LibCapy examples,
Copyright 2021-2024 Baillehache Pascal