logo

怎么距离近的时候能识别到九宫格,距离一远就就识别的不精确,抬高镜头取阈值也不行,还有怎么给识别到的九宫格编号呢,谢谢。

2024-09-25 11:39
L
y4oi

import sensor
import image
import time
import math

# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

# 设置颜色阈值,以下是一个黄色的示例
yellow_threshold = (30, 100, -10, 50, -30, 50)  # H: 30-100, S: 10-50, V: 30-50

# 创建一个时钟对象以计算FPS
clock = time.clock()

def draw_rotated_rect(img, center, angle, width, height, color=(255, 0, 0)):
    """ 绘制旋转矩形 """
    # 计算矩形四个角的坐标
    half_width = width / 2
    half_height = height / 2

    # 旋转矩形的四个角
    points = [
        (-half_width, -half_height),
        (half_width, -half_height),
        (half_width, half_height),
        (-half_width, half_height)
    ]

    # 进行旋转
    rotated_points = []
    for point in points:
        x = point[0] * math.cos(angle) - point[1] * math.sin(angle)
        y = point[0] * math.sin(angle) + point[1] * math.cos(angle)
        rotated_points.append((x + center[0], y + center[1]))

    # 绘制旋转后的矩形
    img.draw_line(int(rotated_points[0][0]), int(rotated_points[0][1]), int(rotated_points[1][0]), int(rotated_points[1][1]), color=color)
    img.draw_line(int(rotated_points[1][0]), int(rotated_points[1][1]), int(rotated_points[2][0]), int(rotated_points[2][1]), color=color)
    img.draw_line(int(rotated_points[2][0]), int(rotated_points[2][1]), int(rotated_points[3][0]), int(rotated_points[3][1]), color=color)
    img.draw_line(int(rotated_points[3][0]), int(rotated_points[3][1]), int(rotated_points[0][0]), int(rotated_points[0][1]), color=color)

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

    # 查找黄色色块
    blobs = img.find_blobs([yellow_threshold], pixels_threshold=200, area_threshold=200, merge=True)

    # 检测到的小框的角度和中心点
    centers = []
    angles = []
    rects = []

    # 遍历找到的色块
    for blob in blobs:
        # 过滤掉小的噪声
        if blob.area() > 100:
            # 获取色块的矩形框
            rect = blob.rect()
            rects.append(rect)
            # 判断是否为正方形
            if abs(rect[2] - rect[3]) < 10:  # 允许宽高差异小于10像素
                # 计算中心点和旋转角度
                center = (blob.cx(), blob.cy())
                angle = math.atan2(rect[3], rect[2])  # 计算旋转角度
                centers.append(center)
                angles.append(angle)

                # 绘制九宫格小框
                img.draw_rectangle(rect)

    # 如果检测到了九个小框
    if len(centers) == 9:
        # 计算九个小框的外接矩形
        min_x = min([rect[0] for rect in rects])
        min_y = min([rect[1] for rect in rects])
        max_x = max([rect[0] + rect[2] for rect in rects])
        max_y = max([rect[1] + rect[3] for rect in rects])

        # 计算大矩形的中心点和角度
        center_x = (min_x + max_x) // 2
        center_y = (min_y + max_y) // 2
        avg_angle = sum(angles) / len(angles)

        # 计算大矩形的宽度和高度
        width = max_x - min_x
        height = max_y - min_y

        # 绘制旋转后的大矩形
        draw_rotated_rect(img, (center_x, center_y), avg_angle, width, height, color=(0, 255, 0))

    print("FPS: {}".format(clock.fps()))
请在这里粘贴代码
2024-09-25 20:54
I
网红小智智 星瞳科技

https://www.bilibili.com/video/BV1q2vbeoExi/?spm_id_from=333.337.search-card.all.click