资源简介
openmv寻线,把摄像头当成8路光电数字灰度进行寻线,同时可识别十字路线,已配置uart3输出需要的数据
代码片段和文件信息
# Hello World Example
#
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
import pyb sensor image math time
from pyb import UART
import json
uart = UART(3115200)
GRAYSCALE_THRESHOLD = [(0 50)]
led = pyb.LED(3)
usb = pyb.USB_VCP()
led.on()
roi_1 = [(20 0 120 20) # 北
(20 100 120 20) # 南
(0 0 20 120) # 西
(140 0 20 120) # 东
(60404040)] # 中
sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.GRAYSCALE) # 格式为 RGB565.
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(20) # 跳过10帧,使新设置生效
sensor.set_auto_whitebal(False) # turn this off.
sensor.set_auto_gain(False) # must be turned off for color tracking
clock = time.clock()
low_threshold = (0 40 )
buf =[0 for i in range(5)]
DI =[0 for i in range(8)]
DI1 =[0 for i in range(8)]
uart.init(115200 bits=8 parity=None stop=1)
while(True):
m = -1
isten = 0
#img = sensor.snapshot()
img = sensor.snapshot().lens_corr(strength = 1.8 zoom = 1.0)
img.binary([low_threshold]invert = 1)
for r in roi_1:
m += 1
blobs = img.find_blobs(GRAYSCALE_THRESHOLD roi=r[0:4]pixels_threshold=100 area_threshold=100 merge=True)
#img.draw_rectangle(r[0:4] color=(25500))
if blobs:
most_pixels = 0
largest_blob = 0
for i in range(len(blobs)):
#目标区域找到的颜色块(线段块)可能不止一个,找到最大的一个,作为本区域内的目标直线
if blobs[i].pixels() > most_pixels:
most_pixels = blobs[i].pixels()
#merged_blobs[i][4]是这个颜色块的像素总数,如果此颜色块像素总数大于
largest_blob = i
# Draw a rect around the blob.
img.draw_rectangle(blobs[largest_blob].rect())
#将此区域的像素数最大的颜色块画矩形和十字形标记出来
评论
共有 条评论