Get RGB histogram statistics (not in LAB format!)

I would like to get the average R, G, and B values from a captured image on the OpenMV. The get_statistics method only returns average values in the LAB color space. However, I would like the average values in the RGB565 color space. The histogram in the IDE is able to calculate the average RGB values, so clearly the numbers are there. I just want to be able to access them in code.

Results of the example code with RGB565 pixformat:

{“l_mean”:0, “l_median”:1, “l_mode”:1, “l_stdev”:0, “l_min”:0, “l_max”:1, “l_lq”:1, “l_uq”:1, “a_mean”:0, “a_median”:-1, “a_mode”:-1, “a_stdev”:0, “a_min”:-1, “a_max”:0, “a_lq”:-1, “a_uq”:-1, “b_mean”:0, “b_median”:0

The values that I want are the R_mean, G_mean, and B_mean displayed in the IDE:
Screen Shot 2021-04-26 at 1.21.42 AM

Hi, just convert the LAB average to RGB average. There’s a conversion method in the image API.

Is that equivalent? Is the average R, G, B, value equal to the average LAB value converted to a RGB format? I couldn’t find documentation to confirm this.

If they are equal, there’s a difference in the converted RGB value and the IDE displayed value.


img = sensor.snapshot()
h = img.histogram().get_statistics()
LABvals = (h[0], h[8], h[16]) #LAB
RGBvals = image.lab_to_rgb(LABvals)

Some examples:

Print From Code ||||| From Histogram in IDE
(165,158,156) ||||| (164, 162, 155)
(33, 44, 24) ||||| (38, 48, 26 )
(57, 52, 66) ||||| (60, 57, 69 )
According to previous forum posts its because of some conversion error? If so, which one is accurate?

Yeah, it’s not exact. Note that the IDE is working off of a JPEG image. Do not expect to get exactly the same values. However, it should be good enough for whatever you need to do.

The OV* camera line isn’t really that high quality on the RGB parts. Also, remember the image is RGB565 so it’s +/-8 points is expected.

Thanks! I still can’t find any docs to suggest that the average of LAB values converted to RGB is the same as the average of RGB values. In fact, looking at the conversion process it seems like it isn’t. Is there a source to confirm that the averaging is equivalent?

Just to confirm, the IDE works off JPEG, but the code should work off the raw RGB right? I just want to confirm that calling histogram.get_statistics() inside the code gives the averages of the raw RGB, and not the JPEG like the IDE.

For my application I need raw RGB averages instead of the average of the JPEG. If the code does that then it’s perfect!

1 Like