Home > other >  Openmv cruise double small sample program
Openmv cruise double small sample program

Time:10-28

The import sensor, image, time, and math
The from pyb import UART
Import json # Tracks a black line. Use [(128, 255)] for a tracking a white line.

GRAYSCALE_THRESHOLD=[(128, 255)]

# set threshold, if it is a black line, GRAYSCALE_THRESHOLD=[(0, 64)];

# if it is a white line, GRAYSCALE_THRESHOLD=[(128255)] # 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 marketers weights where most of the weight is assigned

# to the ROI near the bottom of the image and less to the next ROI and so on.

# modified sampling area here

ROI=(0, 100, 320, 40) # Camera setup...

Sensor. The 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

# close white balance

Clock=time. Clock () # Tracks FPS. # attribute selection sort, here we choose the color piece width

Def get_blob_value (blob) : return blob. W () # choose two of the biggest black color piece

Def compare_blob (blob1 blob2) : comp_result=get_blob_value (blob1) - get_blob_value (blob2) if comp_result & gt; 3: return 1 elif comp_result & lt; - 3: the else return - 1: return 0 def get_direction (left_blob, right_blob) : # three white part according to the fact, calculate the Angle value # thewire & lt; Turn left 0 # thewire & gt; 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: have derailed the 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 () & gt; 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))
  • Related