Use the statistics of the image

If I wonder the average color or the color which occupy the largest area?

Use STATISTICS!

ROI: region of interest


The form of ROI is tupple of (x, y, w, h).

  • x: the x-coordinate in the upper left corner of the ROI
  • y: ROI the y-coordinate in the upper right corner of the ROI
  • w: the width of the ROI
  • h: the height of the ROI

Statistics

image.get_statistics(roi=Auto)

ROI is the target area. Note: here the parameters like ROI and bins should be marked in explicit way. For example:

img.get_statistics(roi=(0,0,10,20))

The ROI would not work is it is img.get_statistics((0,0,10,20)).

  • statistics.mean() Return to the mean of the grayscale (0-255) (int).You can also acquire it throughstatistics[0].

  • statistics.median() Return to the median of the grayscale (0-255) (int). You can also acquire it through statistics[1].

  • statistics.mode() Return to the mode of the grayscale (0-255) (int). You can also acquire it through statistics[2].

  • statistics.stdev() Return to the standard deviation of the grayscale (0-255) (int). You can also acquire it through statistics[3].

  • statistics.min() Return to the minimum of the grayscale (0-255) (int). You can also acquire it through statistics[4].

  • statistics.max() Return to the maximum of the grayscale (0-255) (int). You can also acquire it through statistics[5].

  • statistics.lq() Return to the lower quartile of the grayscale (0-255) (int). You can also acquire it through statistics[6].

  • statistics.uq() Return to the upper quartile of the grayscale (0-255) (int). You can also acquire it through statistics[7].

The above is the value of the grayscale and the following is the mean, median, mode, standard deviation, minimum, maximum, lower quartile and upper quartile of the three LAB channels.

  • l_mean,l_median,l_mode,l_stdev,l_min,l_max,l_lq,l_uq,
  • a_mean,a_median,a_mode,a_stdev,a_min,a_max,a_lq,a_uq,
  • b_mean,b_median,b_mode,b_stdev,b_min,b_max,b_lq,b_uq,

Example

Detect the color value in the upper left area.

import sensor, image, time

sensor.reset() # reset the camera
sensor.set_pixformat(sensor.RGB565) # the format is RGB565.
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000) # skip10 frames to bring the new setting into effect 
sensor.set_auto_whitebal(False)               # Create a clock object to track the FPS.

ROI=(80,30,15,15)

while(True):
    img = sensor.snapshot()         # Take a picture and return the image.
    statistics=img.get_statistics(roi=ROI)
    color_l=statistics.l_mode()
    color_a=statistics.a_mode()
    color_b=statistics.b_mode()
    print(color_l,color_a,color_b)
    img.draw_rectangle(ROI)

Result:

Terminal

56 66 51
56 66 55
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51
56 66 55
56 66 51
56 66 51
56 66 51
56 66 51
56 66 51

results matching ""

    No results matching ""