赞
踩
OpenMV本质还是一个单片机,可以通过调用pyb中的UART使用串口通信,注意发送的数据类型为字符串,可以通过json.dumps()进行字符串转换
from pyb import UART
uart = UART(3, 9600)
uart.write('hello')
uart.read(5) # read up to 5 bytes
blob色块的各类方法:http://book.openmv.cc/image/blob.html
通过方法得到的数据类型为int,需要对其进行一系列操作
1.对其进行0拓展,保证每个数据4位数,方便单片机进行解析
2.进行0拓展的时候,先利用json.dumps()将坐标值转换为字符串,然后利用list将字符串转换为列表,利用列表的insert()方法在头部进行插入
3.完成0拓展之后,将list转换为字符串,利用’’.join()
功能描述:找到最大的红色色块的中心,通过串口发送其中心坐标数据
# Hello World Example # # Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script! import sensor, image, time import json from pyb import UART sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) sensor.skip_frames(time = 2000) # Wait for settings take effect. clock = time.clock() # Create a clock object to track the FPS. #对list进行拓展,高位补0 def Expand_String(str): while(len(str)<4): str.insert(0,'0') return str #所需要寻找的颜色的LAB范围 red_range=(43, 64, 27, 127, -128, 127) #开串口3,波特率为115200 uart=UART(3,115200) while(True): clock.tick() # Update the FPS clock. img = sensor.snapshot() # Take a picture and return the image. #寻找红色色块 blobs=img.find_blobs([red_range]) #画出所有红色色块 for blob in blobs: #print(blob.rect()) img.draw_rectangle(blob.rect()) #找出面积最大的色块,放到blob_max if blobs!=[]: blob_max=blobs[0] for blob in blobs: if blob_max.area()<blob.area(): blob_max=blob #画出blob_max的中心 img.draw_cross(blob_max.cx(),blob_max.cy(),size=5,color=(0,255,0)) #将int型的blob_max的中心转换成json形式(字符形式),再转换为list centre_x=list(json.dumps(blob_max.cx())) centre_y=list(json.dumps(blob_max.cy())) #此时用list存放的中心坐标做0拓展 Expand_String(centre_x) Expand_String(centre_y) #将list转换为字符串形式 centre_x=''.join(centre_x) centre_y=''.join(centre_y) #利用串口发出中心坐标值 uart.write(centre_x+' '+centre_y+'\r\n') print(centre_x+' '+centre_y+'\n') else: #如果没有找到的话,返回 print('0000'+' '+'0000'+'\n') #print(clock.fps()) # Note: OpenMV Cam runs about half as fast when connected # to the IDE. The FPS should increase once disconnected.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。