Using image statistics

What if I want to know the average color or the color with the largest area in a region?

Use statistics - Statistics!

ROI Region of interest

\ The format of roi is a tupple of (x, y, w, h).

  • x: x coordinate of the upper left corner of the ROI region
  • y: y coordinate of the upper left corner of the ROI region
  • w: width of ROI
  • h: height of ROI

Statistics

image.get_statistics(roi=Auto)

Where roi is the target region. Note that parameters such as roi and bins must be explicitly marked, for example:

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

If it is img.get_statistics((0,0,10,20)), ROI will not work.

  • statistics.mean() returns the mean of grayscale (0-255) (int). You can also get it through statistics[0].

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

  • statistics.mode() returns the mode(0-255) (int) of the grayscale. You can also get it via statistics[2]

  • statistics.stdev() Returns the standard deviation of the grayscale value(0-255) (int). You can also get it via statistics[3].

  • statistics.min() returns the minimum value of grayscale(0-255) (int). You can also get it through statistics[4].

  • statistics.max() returns the grayscale maximum value (0-255) (int). You can also get it from statistics[5].

  • statistics.lq() returns the grayscale first quartile (0-255) (int). You can also get it from statistics[6].

  • statistics.uq() returns the grayscale third quartile (0-255) (int). You can also get it from statistics[7].

The above is the grayscale value, and the following

  • 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,

are the average, median, mode, standard deviation, minimum, maximum, first quartile, and third quartile of the three LAB channels.

Example

Detect the color value in the upper left area.

import sensor, image, time

sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 格式为 RGB565.
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(10) # 跳过10帧,使新设置生效
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 ""