OpenMV多色塊識別
- 實作效果
- 具體代碼
實作效果

具體代碼
在螢屏上放的測驗圖片,實物可能需要更改一下閾值
import sensor, image, time ,pyb
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(100)
#初始化指示燈
led = pyb.LED(2)
led.on()
time.sleep(150)
led.off()
Red_threshold = (23, 77, 18, 101, -16, 96)
Green_threshold = (9, 87, -67, -11, -2, 66)
Blue_threshold = (12, 69, 2, 86, -128, -23)
Red_Blobs = None #本幀捕捉到的色塊串列
Green_Blobs = None
Blue_Blobs = None
Target_threshold = [Red_threshold,Green_threshold,Blue_threshold] #目標色塊閾值串列
Target_Blobs = [Red_Blobs,Green_Blobs,Blue_Blobs] #注意!: 這是嵌套串列 , 成員為色塊串列
Name_List = ['Red','Green','Blue']
Target_proportion = (1.05,2) #色塊長寬比例系數 W/H
clock = time.clock()
def TargetBlobs_Find():
temp = 0
while temp < 3:
Target_Blobs[temp] = img.find_blobs([Target_threshold[temp]],merge = True,area_threshold = 600)
if len(Target_Blobs[temp]) > 0:
for Blob in Target_Blobs[temp]:
proportion = Blob.h()/Blob.w()
if proportion > Target_proportion[0] and proportion < Target_proportion[1]:
img.draw_rectangle(Blob.rect())
img.draw_string(Blob.x(),Blob.y(),Name_List[temp])
temp += 1
while(True):
clock.tick()
img = sensor.snapshot()
img.lens_corr(strength=1.6) #矯正畫面
TargetBlobs_Find()
print(clock.fps())
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/258735.html
標籤:python
