前回はカラーピッカーをやりました。
では、特定の色を検知した場合、何らかのフラグを立てるには……?
実際のところ
import sensor, image, lcd, time lcd.init() sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.run(1) green_threshold = [(0, 80, -70, -10, -0, 30)] myroi=[(320//2)-(50//2), (240//2)-(50//2), 50, 50] # 50x50 center of QVGA. while True: img=sensor.snapshot() blobs = img.find_blobs(green_threshold,roi=myroi) if blobs: for b in blobs: tmp=img.draw_rectangle(b[0:4]) tmp=img.draw_cross(b[5], b[6]) c=img.get_pixel(b[5], b[6]) lcd.display(img)
説明
キモはimageクラスのfind_blobsメソッド。
これに閾値のタプルを渡してあげれば、該当するとこに□がでます。
定義によると、以下のようにRGBで下限、上限の順に格納する様子。
The thresholds must be a list of tuples. [(lo, hi), (lo, hi), ..., (lo, hi)] Define the range of colors you want to track.
こんな値、どう抽出すんじゃって話ですが、ここで公式IDEのヒストグラムがでてきます。
To get the threshold of the tracked object, simply select (click and drag) the tracking object in the IDE framebuffer. The histogram will be updated accordingly to the area. Then just write down the color distribution in the starting and falling positions in each histogram channel. These will be the low and high values of thresholds. Since the difference between the upper and lower quartiles is small, the threshold is manually determined.
You can also determine the color threshold by going to Tools -> Machine Vision -> Threshold Editor in the OpenMV IDE and dragging the slider from the GUI window.