无人机视觉 | 搭载MaixPy开发板-K210进行循迹+识别

前言:基于spieed出品的MaixPy-Dock开发板(K210芯片)进行实践。编译运用MicroPython语言,其是基于 Python3 的语法做的一款解析器,包含了 Python3 的大多数基础语法,主要运行在性能和内存有限的嵌入式芯片上。

        计算机视觉为无人机的导航提供了不可估量的作用,本文实现无人机对基础图形颜色的识别与视觉循迹功能(视觉算法部分  不包括串口通信)

import sensor, image, time, lcd

sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 灰度更快
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
lcd.init()
clock = time.clock()
#----颜色参数选择----#
color_a = (20, 52, -13, 70, 2, 65)

#---------------------------------功能函数--------------------------------------#
#----------圆形图像识别---------#
def fcircle():
    img = sensor.snapshot().lens_corr(1.8)
    img.laplacian(1, sharpen=True)
    #lcd.display(img)
    for c in img.find_circles(threshold = 5000, x_margin = 10, y_margin = 10, r_margin = 10,r_min = 1, r_max = 80, r_step = 2):
        a=img.draw_circle(c.x(), c.y(), c.r(), color = (0, 255, 0))
        #lcd.display(a)
        print(c)
#----------矩形图像识别---------#
def frectangle():
    img = sensor.snapshot().lens_corr(1.8)
    img.laplacian(1, sharpen=True)
    for r in img.find_rects(threshold = 10000):
       img.draw_rectangle(r.rect(), color = (255, 0, 0))
       for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
       print(r)
#----------特定颜色识别---------#
def find_max_blob(blobs):
    maxsize = 0
    for blob in blobs:
        if blob[2]*blob[3] > maxsize:
            mmax_blob = blob
            maxsize = blob[2]*blob[3]
    return mmax_blob

def fcolors(color):
    img = sensor.snapshot() # 拍一张照片并返回图像。
    blobs = img.find_blobs([color])
    if blobs:
        b = find_max_blob(blobs)
        if b[2] > 15 and b[3] > 15:
            img.draw_rectangle(b[0:4])
            img.draw_cross(b[5], b[6])
            print(b[5], b[6])
        else:
            print('No')
    else:
        print('NO')
#-----------循迹----------#
def tracing(colors):
    # 拍摄一张图片并按阈值二值化
    img = sensor.snapshot()
    # 使用kpu加速的锐化进行边缘增强
    #img.conv3(con_sharpen)
    # 对于图像进行kpu加速的高斯模糊,使图像中心化平滑
    img.conv3(con_gauss)
    img = img.binary(line_color_union)
    # 对二值化的图片进行线性回归得到直线
    line = img.get_regression([(100,100,0,0,0,0)], roi = (2,2,img.width()-4,img.height()-4))
    img.draw_rectangle((2,2,img.width()-4,img.height()-4))
    if line and line.magnitude() > 3:
            # 在图片上画线
            img.draw_line(line.line(), color = 110, thickness = 5)
            # 计算横滚误差
            erho = abs(line.rho())-img.width()/2
            # 计算偏航误差
            if line.theta() > 90:
                etheta = line.theta()-180
            else:
                etheta = line.theta()
            # 误差归一化
            res_erho = erho/(img.width()/2)
            res_etheta = etheta/90
            # 误差输出
            print('tt',int(res_erho*1000), int(res_etheta*1000))
    else:
        # 定义误差只能小于1000,若输出无效值则寻迹无效
        print('tt',1001, 1001)
    # 显示图片
    lcd.display(img)
#------------------------------------主函数-------------------------------------#
while True:
    clock.tick()
    #fcircle()
    #frectangle()
    #fcolors(color_a)
    #tracing(color_a)
    print("FPS %f" % clock.fps())

###大家可根据自身需求对代码进行更改参数、恢复注释

参考资料:

MaixPy 文档简介 – Sipeed Wiki

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年8月3日
下一篇 2023年8月3日

相关推荐