两个色块颜色一样但大小不一样,一个大一个小,我该如何去识别小的色块,请大佬求教
-
import sensor, image, time thresholds = [(9, 89, 103, -114, -117, -36)] sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) sensor.set_auto_whitebal(False) clock = time.clock() def find_small(blobs): max_size=0 for blob in blobs: if blob[4] < 5000: small_blob=blob return small_blob while(True): clock.tick() img = sensor.snapshot() blobs = img.find_blobs(thresholds,pixels_threshold=3500, area_threshold=3500,merge=True) if blobs: small_blob = find_small(blobs) img.draw_rectangle(small_blob.rect()) # rect img.draw_cross(small_blob.cx(), small_blob.cy()) # cx, cy print(clock.fps())
以上是我的源代码。
我曾尝试用blob.pixels小于某个值实现,但出现了报错,下面是报错的提示,提示错的代码行是 return small_blob
不知道错在哪里了,向大佬求教。
-
-
def find_small(blobs): max_size=0 small_blob=None for blob in blobs: if blob[4] < 5000: small_blob=blob return small_blob
-
@kidswong999
大佬按照你的提示,自己又修改了一下代码,代码如下
import sensor, image, time
thresholds = [(79, 39, 100, 31, -1, -96)]
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()def find_small(blobs):
max_size=0
small_blob=None
for blob in blobs:
if (blob[2]*blob[3] > max_size) and (blob[4] < 5000):
small_blob=blob
return small_blobwhile(True):
clock.tick()
img = sensor.snapshot()
blobs = img.find_blobs(thresholds,pixels_threshold=3500, area_threshold=3500,merge=True)
if blobs:
small_blob = find_small(blobs)
img.draw_rectangle(small_blob.rect())
img.draw_cross(small_blob.cx(), small_blob.cy()) # cx, cy
print(clock.fps())
但是又出现了下面这个错误,不知道怎么办,请教一下大佬
-
@dehi 如果没找到色块呢?你要处理一下。
-
@kidswong999
好的大佬,我明白了