import sensor, image, time, math
from pyb import UART
import json # Tracks a black line. Use [(128, 255)] for a tracking a white line.
GRAYSCALE_THRESHOLD = [(128, 255)]
#設定閾值,如果是黑線,GRAYSCALE_THRESHOLD = [(0, 64)];
#如果是白線,GRAYSCALE_THRESHOLD = [(128,255)] # Each roi is (x, y, w, h). The line detection algorithm will try to find the
# centroid of the largest blob in each roi. The x position of the centroids
# will then be averaged with different weights where the most weight is assigned
# to the roi near the bottom of the image and less to the next roi and so on.
# 在這里修改取樣區
ROI = (0, 100, 320, 40) # Camera setup...
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # use grayscale.
sensor.set_framesize(sensor.QVGA) # use QVGA for speed. 320* 240
sensor.skip_frames(30) # Let new settings take affect.
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
#關閉白平衡
clock = time.clock() # Tracks FPS. # 選擇排序的屬性, 這里我們選取的是色塊寬度
def get_blob_value(blob): return blob.w() # 選擇最大的兩個黑線色塊
def compare_blob(blob1, blob2): comp_result = get_blob_value(blob1) - get_blob_value(blob2) if comp_result > 3: return 1 elif comp_result < -3: return -1 else: return 0 def get_direction(left_blob, right_blob): # 根據左中右三塊白色部分,計算出角度值 # ratio < 0 左拐 # ratio > 0 右拐 MAX_WIDTH = 320 # 調節theta來設定中間寬度的比重, theta越高ratio越靠近0 # 需要根據賽道寬度與攝像頭高度重新設定到合適大小 theta = 0.01 # 這里的b是為了防止除數是0的情況發生, 設定一個小一點的值 b = 3 x1 = left_blob.x() - int(0.5 * left_blob.w()) x2 = right_blob.x() + int(0.5 * right_blob.w()) w_left = x1 w_center = math.fabs(x2 - x1) w_right = math.fabs(MAX_WIDTH - x2) direct_ratio = (w_left + b + theta * w_center) / (w_left + w_right + 2 * b + 2 * theta * w_center) - 0.5 return direct_ratio def get_top2_blobs(blobs): # 找到最大的兩個色塊, 回傳左色塊與右色塊 for blob in blobs: pass #print(blob) # img.draw_rectangle(blob.rect()) if len(blobs) < 2: # 已偏離軌道 return (None, None) top_blob1 = blobs[0] top_blob2 = blobs[1] if compare_blob(top_blob1, top_blob2) == -1: top_blob1, top_blob2 = top_blob2, top_blob1 for i in range(2, len(blobs)): if compare_blob(blobs[i], top_blob1) == 1: top_blob2 = top_blob1 top_blob1 = blobs[i] elif compare_blob(blobs[i], top_blob2) == 1: top_blob2 = blobs[i] if top_blob1.cx() > top_blob2.cx(): return (top_blob2, top_blob1) else: return (top_blob1, top_blob2) def draw_direct(img, direct_ratio): # 可視化方向(左右) 標識及其幅度 img.draw_circle(160, 80, 5) img.draw_line((160, 80, int(160 + direct_ratio * 200), 80)) while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image. blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=ROI, merge=True) #目標區域找到直線 if blobs: left_blob, right_blob = get_top2_blobs(blobs) if(left_blob == None or right_blob == None): print("Out Of Range") continue else: print("-------") print("left blob") print(left_blob) print("right blob") print(right_blob) uart=UART(3,115200) uart.init(115200,bits=8,parity=None,stop=1) a=left_blob.w() b=left_blob.h() c=left_blob.pixels() d=left_blob.count() f=right_blob.w() g=right_blob.h() x=right_blob.pixels() y=right_blob.count() data=https://bbs.csdn.net/topics/bytearray([0xb3,0xa3,b,c,d,f,g,x,y]) uart.write(data) img.draw_rectangle(left_blob.rect()) img.draw_cross(left_blob.cx(), left_blob.cy()) img.draw_rectangle(right_blob.rect()) img.draw_cross(right_blob.cx(), right_blob.cy()) direct_ratio = get_direction(left_blob, right_blob) img.draw_string(10, 10, "%.2f"%direct_ratio) draw_direct(img, direct_ratio) img.draw_rectangle(ROI, color=(255, 0, 0))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/134013.html
上一篇:安裝mlab庫的時候報錯UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 1
