画像統計量の利用
ある領域の平均色や、最も面積の大きい色を知りたい場合はどうすればよいでしょうか?
統計量 - Statistics を使いましょう!
ROI(関心領域)
\
roi のフォーマットは (x, y, w, h) のタプルです。
- x: ROI 領域の左上隅の x 座標
- y: ROI 領域の左上隅の y 座標
- w: ROI の幅
- h: ROI の高さ
Statistics
image.get_statistics(roi=Auto)
roi は対象領域です。roi や bins などのパラメータは明示的に指定する必要があることに注意してください。例えば:
img.get_statistics(roi=(0,0,10,20))
img.get_statistics((0,0,10,20)) のように書くと、ROI は機能しません。
statistics.mean はグレースケール(0-255)の平均値(int)を返します。statistics[0] からも取得できます。
statistics.median はグレースケール(0-255)の中央値(int)を返します。statistics[1] からも取得できます。
statistics.mode() はグレースケール(0-255)の最頻値(int)を返します。 statistics[2]からも取得できます。
statistics.stdev() はグレースケール値(0-255)の標準偏差(int)を返します。 statistics[3]からも取得できます。
statistics.min() はグレースケール(0-255)の最小値(int)を返します。statistics[4]からも取得できます。
statistics.max はグレースケールの最大値(0-255)(int)を返します。statistics[5]からも取得できます。
statistics.lq はグレースケールの第1四分位数(0-255)(int)を返します。statistics[6]からも取得できます。
statistics.uq はグレースケールの第3四分位数(0-255)(int)を返します。statistics[7]からも取得できます。
以上はグレースケール値についてですが、続いて
- 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,
は LAB の3つのチャンネルそれぞれの、平均値、中央値、最頻値、標準偏差、最小値、最大値、第1四分位数、第3四分位数です。
例
左上の領域のカラー値を検出します。
import csi, image, time
csi0 = csi.CSI()
csi0.reset() # カメラを初期化
csi0.pixformat(csi.RGB565) # フォーマットをRGB565に設定
csi0.framesize(csi.QVGA)
csi0.snapshot(frames=10) # 新しい設定を反映させるため10フレームをスキップ
csi0.auto_whitebal(False) # Create a clock object to track the FPS.
ROI=(80,30,15,15)
while(True):
img = csi0.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)
結果:

ターミナル
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