当前位置:   article > 正文

通过OpenMV来找靶心_靶心识别

靶心识别

通过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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

也可以自定义帧格式,实现通信。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/825696
推荐阅读
相关标签
  

闽ICP备14008679号