2023电赛E题视觉部分

该部分主要要完成正方形区域的识别,并返回对应的坐标,但是由于距离1m,过远。因此需要引入图像增强,下面代码完成基本流程测试,仅供参考:

import sensor

import image

import time

# 初始化摄像头

sensor.reset()

sensor.set_pixformat(sensor.RGB565)

sensor.set_framesize(sensor.QVGA)

sensor.skip_frames(time = 2000)

# 设置阈值,用于图像增强

thresholds = [(30, 100, -64, -8, -32, 32)] # 根据实际情况调整阈值

while True:

    img = sensor.snapshot() # 获取图像

    # 图像增强

    img.binary([thresholds])

    # 寻找轮廓

    blobs = img.find_blobs([thresholds], pixels_threshold=200, area_threshold=200)

    # 遍历找到的轮廓

    for blob in blobs:

        # 判断是否为正方形

        if blob.is_square():

            # 计算正方形的中心坐标

            x = blob.cx()

            y = blob.cy()

            # 计算距离

            distance = 1 / blob.w() # 假设正方形的宽度为1米

            # 在图像上绘制正方形和坐标

            img.draw_rectangle(blob.rect())

            img.draw_cross(x, y)

            # 打印坐标和距离

            print("Square found at (x={}, y={}), distance={}m".format(x, y, distance))

    # 显示图像

    img.show()

猜你喜欢

转载自blog.csdn.net/qq_33505204/article/details/132062107