2023 Electric Competition---Sports Target Control and Automatic Tracking System (Question E) The code of OpenART mini is ported to OpenMV

foreword

(1) Many students have completed the first three questions based on my last blog, congratulations. Many students got stuck on the fourth question.
(2) I said that the code of OpenART mini is feasible. But they will not be ported to OpenMV, and again I will post the code after porting.
(3)Tucao, I do not understand. Why are so many people unable to be transplanted? Isn't this a matter of minutes? (wry smile)

Identify the rectangular frame and the corresponding corner points

(1) A strange error will appear on line 40 of this place, saying that corner is not defined. But it was clearly defined earlier by calling corner = r.corners().
(2) Then I wrote a sentence of corner = 0 in front, and the error message disappeared. This bug is very strange. I hope you pay attention

from machine import Pin
import sensor, image, time
import pyb
#import seekfree, pyb

# 初始化TFT180屏幕
#lcd = seekfree.LCD180(3)

# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 设置图像色彩格式为RGB565格式
sensor.set_framesize(sensor.QQVGA)  # 设置图像大小为160*120
sensor.set_auto_whitebal(True)      # 设置自动白平衡
sensor.set_brightness(3000)         # 设置亮度为3000
sensor.skip_frames(time = 20)       # 跳过帧

clock = time.clock()
corner = 0
while(True):
    clock.tick()
    img = sensor.snapshot()

# -----矩形框部分-----
    # 在图像中寻找矩形
    for r in img.find_rects(threshold = 10000):
        # 判断矩形边长是否符合要求
        if r.w() > 20 and r.h() > 20:
            # 在屏幕上框出矩形
            img.draw_rectangle(r.rect(), color = (255, 0, 0), scale = 4)
            # 获取矩形角点位置
            corner = r.corners()
            # 在屏幕上圈出矩形角点
            img.draw_circle(corner[0][0], corner[0][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
            img.draw_circle(corner[1][0], corner[1][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
            img.draw_circle(corner[2][0], corner[2][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
            img.draw_circle(corner[3][0], corner[3][1], 5, color = (0, 0, 255), thickness = 2, fill = False)

        # 打印四个角点坐标, 角点1的数组是corner[0], 坐标就是(corner[0][0],corner[0][1])
        # 角点检测输出的角点排序每次不一定一致,矩形左上的角点有可能是corner0,1,2,3其中一个
        corner1_str = f"corner1 = ({
      
      corner[0][0]},{
      
      corner[0][1]})"
        corner2_str = f"corner2 = ({
      
      corner[1][0]},{
      
      corner[1][1]})"
        corner3_str = f"corner3 = ({
      
      corner[2][0]},{
      
      corner[2][1]})"
        corner4_str = f"corner4 = ({
      
      corner[3][0]},{
      
      corner[3][1]})"
        print(corner1_str + "\n" + corner2_str + "\n" + corner3_str + "\n" + corner4_str)
    # 显示到屏幕上,此部分会降低帧率
    #lcd.show_image(img, 160, 120, 0, 0, zoom=0)  #屏幕显示

    # 打印帧率
    #print(clock.fps())

Tracking Laser Lights

from machine import Pin
import sensor, image, time
import pyb
#import seekfree, pyb

# 初始化激光灯控制引脚P0,并置为高电平
laser_light=Pin("P0", Pin.OUT)
laser_light.value(1)

# 初始化TFT180屏幕
#lcd = seekfree.LCD180(3)

# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 设置图像色彩格式为RGB565格式
sensor.set_framesize(sensor.QQVGA)  # 设置图像大小为160*120
sensor.set_auto_whitebal(True)      # 设置自动白平衡
sensor.set_brightness(3000)         # 设置亮度为3000
sensor.skip_frames(time = 20)       # 跳过帧

clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()

# -----跟踪激光部分-----
    # 设置激光颜色阈值
    red_td = [(56, 100, 45, 127, -128, 127)]  # 这里要改
    # 根据阈值找到色块
    for b in img.find_blobs(red_td,pixels_threshold=2, area_threshold=15, merge=True,invert = 0):
        # 在屏幕上画出色块
        img.draw_rectangle(b.rect(), color = (0, 255, 0), scale = 2, thickness = 2)

        # 打印激光色块的中心位置
        # 使用b.x()获取色块矩形左上角X坐标
        # 使用b.y()获取色块矩形左上角Y坐标
        # 使用b.w()获取色块矩形宽度
        # 使用b.h()获取色块矩形高度
        # 矩形中心坐标为(x + w/2,y + h/2)
        print(f"rect = {
      
      b.x() + b.w()/2},{
      
      b.y() + b.h()/2}")
        break

    # 显示到屏幕上,此部分会降低帧率
    #lcd.show_image(img, 160, 120, 0, 0, zoom=0)  #屏幕显示

    # 打印帧率
    #print(clock.fps())

Identify the rectangular frame and corresponding corner points and track the laser light

from machine import Pin
import sensor, image, time
#import seekfree, pyb
import  pyb
# 初始化激光灯控制引脚,并置为高电平
laser_light=Pin("P9", Pin.OUT)
laser_light.value(1)

# 初始化TFT180屏幕
#lcd = seekfree.LCD180(3)

# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 设置图像色彩格式为RGB565格式
sensor.set_framesize(sensor.QQVGA)  # 设置图像大小为160*120
sensor.set_auto_whitebal(True)      # 设置自动白平衡
sensor.set_brightness(3000)         # 设置亮度为3000
sensor.skip_frames(time = 20)       # 跳过帧

clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()

# -----矩形框部分-----
    # 在图像中寻找矩形
    for r in img.find_rects(threshold = 10000):
        # 判断矩形边长是否符合要求
        if r.w() > 20 and r.h() > 20:
            # 在屏幕上框出矩形
            img.draw_rectangle(r.rect(), color = (255, 0, 0), scale = 4)
            # 获取矩形角点位置
            corner = r.corners()
            # 在屏幕上圈出矩形角点
            img.draw_circle(corner[0][0], corner[0][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
            img.draw_circle(corner[1][0], corner[1][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
            img.draw_circle(corner[2][0], corner[2][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
            img.draw_circle(corner[3][0], corner[3][1], 5, color = (0, 0, 255), thickness = 2, fill = False)

            # 角点坐标打印详见OpenART mini识别矩形框以及对应角点文件

# -----跟踪激光部分-----
    # 设置激光颜色阈值
    red_td = [(56, 100, 45, 127, -128, 127)]
    # 根据阈值找到色块
    for b in img.find_blobs(red_td,pixels_threshold=2, area_threshold=15, merge=True,invert = 0):
        # 在屏幕上画出色块
        img.draw_rectangle(b.rect(), color = (0, 255, 0), scale = 2, thickness = 2)
        break

        # 坐标打印详见OpenART mini跟踪激光灯文件
    # 显示到屏幕上,此部分会降低帧率
    #lcd.show_image(img, 160, 120, 0, 0, zoom=0)  #屏幕显示

    # 打印帧率
    print(clock.fps())

Guess you like

Origin blog.csdn.net/qq_63922192/article/details/132076300