该部分主要要完成正方形区域的识别,并返回对应的坐标,但是由于距离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()

技术
今日推荐
阅读数 169345
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信