openmv 程序
import sensor, image, time, math
from pyb import UART
import json
import ustruct
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
uart = UART(3,115200) #定义串口3变量
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
def find_max(circles): #定义寻找球面积最大的函数
max_size=0
for circle in circles:
if circle.magnitude() > max_size:
max_circle=circle
max_size = circle.magnitude()
return max_circle
def sending_data(cx,cy): #发送函数
global uart;
#frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
#data = bytearray(frame)
data = ustruct.pack("<bbhhb",
0x2C,
0x12,
int(cx),
int(cy),
0x5B)
uart.write(data);
def recive_data(): #接收函数
global uart
if uart.any():
tmp_data = uart.readline();
print(tmp_data)
while(True):
clock.tick()
img = sensor.snapshot()
circles = img.find_circles(threshold = 2500, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2);
cx=0;cy=0;
if circles:
#如果找到了目标球
max_c = find_max(circles);
#在圆周围画一个圆
#img.draw_circle(circle.x(), circle.y(), circle.r(), color = (255, 0, 0))
#用圆标记出目标颜色区域
img.draw_cross(max_c[0], max_c[1]) # cx, cy
img.draw_cross(160, 120) # 在中心点画标记
#在目标颜色区域的中心画十字形标记
cx=max_c[0];
cy=max_c[1];
img.draw_line((80,60,cx,cy), color=(127));
#img.draw_string(160,120, "(%d, %d)"%(160,120), color=(127));
img.draw_string(cx, cy, "(%d, %d)"%(cx,cy), color=(127));
sending_data(cx,cy); #发送点位坐标
recive_data();
|