赞
踩
通过OpenMV来找靶心
OpenMV可以识别下面这张图,并且能获取靶心的坐标。
将获取的靶心的坐标可以通过串口发送给下位机单片机,进行下一步的操作,如下是OpenMV的代码:
import sensor, image, time from pyb import UART import json red_threshold = (0, 100, 32, 127, -128, 54) def find_max_circle(circles): max_size = 0 for circle in circles: if circle.r()>max_size: max_circle = circle max_size = circle.r() return max_circle sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(time = 2000) 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() #sensor.set_hmirror(True) #水平方向翻转 #sensor.set_vflip(True) #垂直方向翻转 uart = UART(3, 115200) uart.init(115200, bits = 8, parity = None, stop = 1) #8位数据位,无校验位, 1位停止位 while(True): clock.tick() img = sensor.snapshot().lens_corr(1.8) circles = img.find_circles(x_stride=4, y_stride=1, threshold = 4000, x_margin = 20, y_margin = 20, r_margin =50, r_min =6, r_max = 100, r_step = 2) if circles: max_circle = find_max_circle(circles) #找到最大的圆 area = (max_circle.x()-max_circle.r(), max_circle.y()-max_circle.r(), 2*max_circle.r(), 2*max_circle.r()) img.draw_rectangle(area, color = (250, 0, 0)) blob = img.find_blobs([red_threshold], roi=area) if blob: #如果找到了目标颜色 FH = bytearray([0xb3,0xb4]) uart.write(FH) for b in blob: img.draw_rectangle(b.rect(), color = (0,0,250)) img.draw_edges(b.min_corners(), color=(0,255,0)) img.draw_cross(b.cx(),b.cy()) print(b.cx(),b.cy()-10) x = b.cx() #色块的外框中心x坐标 y = b.cy() #色块的外框中心y坐标 data = bytearray([x, y]) #将x,y的值转为二进制数 uart.write(data) #通过串口发送出去 end = bytearray([0x01,0x01,0xb5,0x06]) uart.write(end)
也可以自定义帧格式,实现通信。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。