The open imaging DSP library
Loading...
Searching...
No Matches
How to do collect image statistics

Use libmpix to collect statistics about the image.

In order to apply various color and contrast correction to an image or other purposes, it is useful to collect statistics from the image.

How to collect statistics with 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

Then several types of statistics can be gathered from the image in one function call.

This will sample the specified number of values from the image, and accumulate the information into the statistics struct. For instance here for 1000 values.

struct mpix_stats stats = { .nvals = 1000 };
mpix_stats_from_buf(&img, &stats);
void mpix_stats_from_buf(struct mpix_stats *stats, const uint8_t *buf, struct mpix_format *fmt)
Collect red, green, blue channel averages of all pixels in an RGB24 frame.
Definition types.h:168
uint16_t nvals
Definition types.h:182

The content of mpix_stats can be browsed directly for any purpose.

Note
Statistics collection must be done before any other operation.

Then derived statistics can optionally be computed out of the generated histograms:

/* Get the mean lightness of the image */
val = mpix_stats_get_y_mean(&stats);
uint8_t mpix_stats_get_y_mean(struct mpix_stats *stats)
Get the mean value from a histogram.

How to support more statistics?

The statistics collection is somewhat minimal at the moment, as focused on providing the minimum.

  • Applications will needvery different statistics depending on their goal. For custom statistics, refer to How to sample pixels for directly collecting pixel values from the image.
  • libmpix strives to integrate classical computer vision building blocks including statistics, let us know what you needed!