The open imaging DSP library
Loading...
Searching...
No Matches
How to correct images

Use libmpix to apply image correction to improve contrasts and colors.

Raw images coming from a sensor are typically too dark or over-exposed, with green colors and other visible defects.

Some of the defects cannot be corrected (too much noise, over-exposure), and some are expected and corrected as part of any classical image correction pipeline.

If the image arrives without such defects, this suggests that the correction is included inside the image sensor or the video acquisition hardware.

The input format is always specified when opening the image and does not need to be specified. What remains to do is to call mpix_image_convert and specify the output pixel format.

How to do image correction on libmpix

First load a buffer into an image struct, specifying the pixel format:

struct mpix_image img;
struct mpix_format fmt = { .width = 640, .height = 480, .fourcc = MPIX_FMT_RGB24 };
mpix_image_from_buf(&img, buf, sizeof(buf), &fmt);
#define MPIX_FMT_RGB24
Definition formats.h:125
static void mpix_image_from_buf(struct mpix_image *img, const uint8_t *buffer, size_t size, const struct mpix_format *fmt)
Initialize an image from a memory buffer.
Definition image.h:30
Image format description.
Definition types.h:72
uint16_t width
Definition types.h:76
Represent the image currently being processed.
Definition types.h:136

Each type of correction have their own struct, all of which are represented as members of the mpix_correction_all struct (convenient to store all parameters) and mpix_correction_any (used to control a particular operation).

To control the correction level, you may adjust each parameter to fit a particular camera and light condition manually:

  • MPIX_CID_BLACK_LEVEL is the value to subtract to every pixel to set the black back to 0. This can be set to the minimum value you observe in an image and rarely needs to be updated.
  • MPIX_CID_RED_BALANCE and MPIX_CID_BLUE_BALANCE are defined as a proportion to the green channel, with 1.0 mapped to 1024. Let's try to apply x2 to both blue and red to correct the green tint: 1024 * 2 = 2048.
  • MPIX_CID_GAMMA_LEVEL gamma correction to non-linearly increase the brightness: dark/bright colors remain dark/bright but intermediate tones become brighter to reflect eye's natural light sensitivity. Contrasts will appear more natural and accurate.

Then, apply each step of the correction you wish to the image:

static int mpix_image_correct_black_level(struct mpix_image *img)
Apply Black Level Correction (BLC) to the image.
Definition image.h:179
static int mpix_image_correct_white_balance(struct mpix_image *img)
Apply White Balance Correction (AWB) to the image.
Definition image.h:165
static int mpix_image_correct_gamma(struct mpix_image *img)
Apply Gamma Correction (GC) to the image.
Definition image.h:193

It is now possible to apply extra correction to it (where 1 << 10 is to convert to fixed-point):

static int mpix_image_ctrl_value(struct mpix_image *img, enum mpix_control_id cid, int32_t value)
Set a control to a pipeline.
Definition image.h:440
@ MPIX_CID_BLUE_BALANCE
Definition types.h:60
@ MPIX_CID_RED_BALANCE
Definition types.h:58
@ MPIX_CID_GAMMA_LEVEL
Definition types.h:56
@ MPIX_CID_BLACK_LEVEL
Definition types.h:54

The image will now be corrected using each of the steps specified.

See Supported operations for a list of all supported image correction operations.