RGB Value from snapshot

Hello all,

i thought my problem was simple but i turn around and if you could help it will be great.
I would like to have mean or median of RGB color but i have different values than rgb chart values at bottom right of oepnmv ide.

Here is the code :

import sensor,image,time
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
print("---------------------------------" )
while True :
   img=sensor.snapshot() 
   istat=img.get_histogram().statistics()
   print("L value (mean, median) :",istat.l_mean(),istat.l_median())   
   print("A value (mean, median) :",istat.a_mean(),istat.a_median())
   print("B vlaue (mean, median) :",istat.b_mean(),istat.b_median())
   RGBmedian=image.lab_to_rgb((istat.l_median(),istat.a_median(),istat.b_median()))
   print("")
   RGBmean=image.lab_to_rgb((istat.l_mean(),istat.a_mean(),istat.b_mean()))   
   print("R value (mean, median) :",RGBmean[0],RGBmedian[0])   
   print("G value (mean, median) :",RGBmean[1],RGBmedian[1])
   print("B vlaue (mean, median) :",RGBmean[2],RGBmedian[2])
   print("---------------------------------")
   time.sleep(3000)

LAB value are nearly the same but not rgb, specially the red median ???

Thanks for help,

Yes, the conversion is not perfect. But, it should be close.

I have noted that we should support raw RGB stats.

I dont know what you think about what is closed but i have this :

L value (mean, median) : 42 and 32 ( 42 and 31 on ide )
A value (mean, median) : -4 and -9 ( -6 and -9 on ide )
B vlaue (mean, median) : -2 and 7 ( -2 and 8 on ide )

R value (mean, median) : 90 and 66 ( 80 and 25 on ide )
G value (mean, median) : 101 and 81 ( 107 and 73 on ide )
B vlaue (mean, median) : 99 and 66 ( 106 and 74 on ide )

Does it exist another way to get them ?

Thanks,

That’s a really high difference.

Mmm, not really. This wasn’t really well thought out.

You can use get_pixel() to get the values.

ok, i tried to do my own function.
Here is the code :

import sensor,image,time, pyb
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((373, 36, 45, 45))
sensor.skip_frames(time=2000)

def getMeanMedian(img):
   imagsize= img.width()*img.height()
   RGB   = [[],[],[]]
   RGBmean=[0,0,0]
   RGBmedian=[0,0,0]
   for i in range( img.width() ) :
      for j in range(img.height() ) :
         tRGB = img.get_pixel((i,j))
         for k in range(3) : RGB[k].append(int(tRGB[k]))
   for k in range(3) :
      RGBmean[k] = int ( sum(RGB[k]) / imagsize )
      RGBmedian[k] = int( (sorted(RGB[k])[int(round((len(RGB[k]) - 1) / 2.0))] + sorted(RGB[k])[int(round((len(RGB[k]) - 1) // 2.0))]) / 2.0 )
   return RGBmean, RGBmedian
   
print("---------------------------------" )
while True :
   img=sensor.snapshot()
   istat=img.get_histogram().statistics()
   print("L value (mean, median) :",istat.l_mean(),istat.l_median())
   print("A value (mean, median) :",istat.a_mean(),istat.a_median())
   print("B vlaue (mean, median) :",istat.b_mean(),istat.b_median())
   RGBmedian=image.lab_to_rgb((istat.l_median(),istat.a_median(),istat.b_median()))
   print("")
   RGBmean=image.lab_to_rgb((istat.l_mean(),istat.a_mean(),istat.b_mean()))
   print("R convert (mean, median) :",RGBmean[0],RGBmedian[0])
   print("G convert (mean, median) :",RGBmean[1],RGBmedian[1])
   print("B convert (mean, median) :",RGBmean[2],RGBmedian[2])
   print("")
   start=pyb.millis()
   RGBmean, RGBmedian = getMeanMedian( img )
   print ( "time to (%d, %d) img calcultate : %d" % ( img.width(), img.height(), pyb.elapsed_millis(start) ) )
   print("R calculate (mean, median) :",RGBmean[0],RGBmedian[0])
   print("G calculate (mean, median) :",RGBmean[1],RGBmedian[1])
   print("B calculate (mean, median) :",RGBmean[2],RGBmedian[2])
   print("---------------------------------")
   time.sleep(3000)

I have got this with another kind of view :

L value (mean, median) : 30 31
A value (mean, median) : -7 -10
B vlaue (mean, median) : -2 3

R convert (mean, median) : 58 and 58 ( 54 and 58 on ide )
G convert (mean, median) : 77 and 81 ( 80 and 77 on ide )
B convert (mean, median) : 74 and 66 ( 80 and 82 on ide )

time to (44, 44) img calcultate : 48ms
R calculate (mean, median) : 52 and 49 ( 53 and 58 on ide )
G calculate (mean, median) : 77 and 77 ( 79 and 77 on ide )
B calculate (mean, median) : 77 and 82 ( 80 and 82 on ide )

I pushed the rgb values under excel and got the same result as calculated…
I am a bit lost, are you sure about your red median calculation ?

Yeah, but, again, the IDE is working on JPEG data versus your code is working on RAW data. Whatever value you have is more exact as it’s before a compression and uncompression cycle.