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()

  • 51
    点赞
  • 357
    收藏
    觉得还不错? 一键收藏
  • 22
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值