資源簡介
OpenMV識別交通燈例程,僅需根據環境修改交通燈紅、綠、黃顏色閾值即可,思路清晰,識別率高。
代碼片段和文件信息
#?Untitled?-?By:?CZK?-?周五?5月?10?2019
import?sensor?imagetimelcd?pyb
from?pyb?import?UARTTimerLED
#
red_threshold_01?=?(60?88?23?63?6?-25)
green_threshold_01?=?(85?98?-90?1?4?-63)
yellow_threshold_01?=?(64?99?-23?18?14?66)
#
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)#320*240
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((240?240))
#sensor.snapshot().save(“/snapshot-%d.jpg“?%?pyb.rng())?#?Save?Pic.
sensor.skip_frames()
sensor.set_auto_whitebal(False)??????#關閉白平衡
sensor.set_auto_gain(False)??????????#關閉自動增益
clock?=?time.clock()
lcd.init()???????????????????????????#Initialize?the?lcd?screen.
uart?=?UART(31152008None1)???????#創建串口對象
data?=?[]
LED_Red?=?LED(1)
LED_Green?=?LED(2)
LED_Blue?=?LED(3)
BLUE_LED_PIN?=?3
def?expand_roi(roi):
????#?set?for?QQVGA?160*120
????extra?=?5
????win_size?=?(640?480)
????(x?y?width?height)?=?roi
????new_roi?=?[x-extra?y-extra?width+2*extra?height+2*extra]
????if?new_roi[0]?0:
????????new_roi[0]?=?0
????if?new_roi[1]?0:
????????new_roi[1]?=?0
????if?new_roi[2]?>?win_size[0]:
????????new_roi[2]?=?win_size[0]
????if?new_roi[3]?>?win_size[1]:
????????new_roi[3]?=?win_size[1]
????return?tuple(new_roi)
sensor.skip_frames(time?=?2000)?#?Give?the?user?time?to?get?ready.
tim?=?Timer(4freq=1)??????????????#?create?a?timer?object?using?timer?4
#tim.callback(tick)
tim.deinit()
while(True):
????img?=?sensor.snapshot()
????clock.tick()?#?Track?elapsed?milliseconds?between?snapshots().
????blobs_red?=?img.find_blobs([red_threshold_01]?area_threshold=150)
????blobs_green?=?img.find_blobs([green_threshold_01]?area_threshold=150)
????blobs_yellow?=?img.find_blobs([yellow_threshold_01]?area_threshold=150)
????if?blobs_yellow:
????#如果找到了目標顏色
????????#print(blobs)
????????#print(“黃燈“)
????????numjtdy?=?numjtdy+1
????????for?blob?in?blobs_yellow:
????????#迭代找到的目標顏色區域
????????????is_circle?=?False
????????????max_circle?=?None
????????????max_radius?=?-1
????????????new_roi?=?expand_roi(blob.rect())
????????????for?c?in?img.find_circles(threshold?=?2000?x_margin?=?20?y_margin?=?20?r_margin?=?10?roi=new_roi):
????????????????is_circle?=?True
????????????????#?img.draw_circle(c.x()?c.y()?c.r()?color?=?(255?255?255))
????????????????if?c.r()?>?max_radius:
????????????????????max_radius?=?c.r()
????????????????????max_circle?=?c
????????????if?is_circle:
????????????????#?如果有對應顏色的圓形?標記外框
????????????????#?Draw?a?rect?around?the?blob.
????????????????img.draw_rectangle(new_roi)?#?rect
????????????????img.draw_rectangle(blob.rect())?#?rect
????????????????#用矩形標記出目標顏色區域
????????????????img.draw_cross(blob[5]?blob[6])?#?cx?cy
??????
評論
共有 條評論