logo

识别棋盘最大矩形不成功,有的时候能识别到,感觉没什么问题,但没有效果

2024-10-31 16:58
L
y4oi

import sensor
import image
import time
import math

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

clock = time.clock()

# 检测黄色的颜色阈值
yellow_threshold = (35, 90, -20, 60, 40, 100)

# 插值函数,用于计算每个格子的角点
def interpolate(p1, p2, factor):
    return (p1[0] + (p2[0] - p1[0]) * factor, p1[1] + (p2[1] - p1[1]) * factor)

find_blobs = True

# 主循环
while True:
    clock.tick()
    img = sensor.snapshot().lens_corr(strength=1.0, zoom=1.0)

    if find_blobs:
        # 使用颜色阈值来识别黄色区域
        yellow_blobs = img.find_blobs([yellow_threshold], pixels_threshold=3000, area_threshold=3000, merge=True)

        max_area = 0
        max_rect = None

        # 遍历所有黄色区域
        for blob in yellow_blobs:
            # 在黄色区域内寻找矩形
            rects = img.find_rects(roi=blob.rect(), threshold=5000)

            for rect in rects:
                area = rect.w() * rect.h()
                aspect_ratio = rect.w() / rect.h()

                # 判断面积和长宽比是否符合条件
                if 0.8 < aspect_ratio < 1.2 and area > max_area and 50 < rect.w() < 100 and 50 < rect.h() < 100:
                    max_area = area
                    max_rect = rect

        # 如果找到符合条件的最大矩形
        if max_rect:
            corners = max_rect.corners()
            angle = -math.degrees(math.atan2(corners[1][1] - corners[0][1], corners[1][0] - corners[0][0]))

            if -45 < angle < 45:
                print("矩形旋转角度:", angle)

            # 绘制矩形的四个角点和边框
            for corner in corners:
                img.draw_circle(corner[0], corner[1], 5, color=(255, 0, 0), thickness=2, fill=False)
            for i in range(len(corners)):
                start_point = corners[i]
                end_point = corners[(i + 1) % 4]
                img.draw_line(start_point[0], start_point[1], end_point[0], end_point[1], color=(255, 0, 0))

            # 在棋盘上添加编号
            num = 9
            for row in range(3):
                for col in range(3):
                    # 计算每个格子的四个顶点的插值
                    top_left = interpolate(interpolate(corners[0], corners[3], row / 3.0), interpolate(corners[1], corners[2], row / 3.0), 1 - col / 3.0)
                    top_right = interpolate(interpolate(corners[0], corners[3], row / 3.0), interpolate(corners[1], corners[2], row / 3.0), 1 - (col + 1) / 3.0)
                    bottom_left = interpolate(interpolate(corners[0], corners[3], (row + 1) / 3.0), interpolate(corners[1], corners[2], (row + 1) / 3.0), 1 - col / 3.0)
                    bottom_right = interpolate(interpolate(corners[0], corners[3], (row + 1) / 3.0), interpolate(corners[1], corners[2], (row + 1) / 3.0), 1 - (col + 1) / 3.0)

                    # 计算格子的中心坐标
                    center_x = int((top_left[0] + bottom_right[0]) / 2)
                    center_y = int((top_left[1] + bottom_right[1]) / 2)

                    # 在每个格子的中心位置绘制编号
                    img.draw_string(center_x - 10, center_y - 10, str(num), color=(255, 0, 0), scale=2)
                    num -= 1
2024-11-05 19:12
I
网红小智智 星瞳科技

可以参考一下这个链接:https://book.openmv.cc/project/chess.html