You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于OpenCV实现物理棋盘棋子位置转矩阵的技术问询

从物理棋盘图像提取棋子位置并转换为python-chess矩阵格式

需求目标

  • 通过摄像头拍摄的照片,提取物理棋盘上的棋子位置
  • 将位置转换为python-chess库文档中的矩阵格式,用于后续验证棋子移动

前期尝试与问题

HoughLines提取网格失败

  • 尝试用cv2.HoughLines提取棋盘网格但未成功,推测原因是棋盘本身或摄像头鱼眼畸变
  • 即使解决网格提取问题,仍不清楚如何获取棋子坐标、区分空方格与有棋子的方格

角点检测的困境

  • 改用颜色检测思路,通过cv2.goodFeaturesToTrack成功检测到棋盘所有角点,但角点坐标是无序的
  • 尝试通过四个外部分角点划分距离获取方格中点,但因摄像头非正上方拍摄存在透视变形,方格大小不一,该方法不可靠
  • 当前核心需求:找到属于同一方格的四个角点,进而计算每个方格的中点

初始HoughLines尝试代码

import cv2
import numpy as np


def main():
    im = cv2.imread('opencv_frame_0.png')
    #im = cv2.resize(im, (640, 480))  
    #edge = cv2.imread('edge.png', 0)
    edge = cv2.Canny(im, 60, 160, apertureSize=3)
    lines = cv2.HoughLines(edge, 1, np.pi/180, 100, 100, 50)
    for rho, theta in lines[0]:
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))
        cv2.line(im, (x1, y1), (x2, y2), (0, 0, 255), 2)
        # TODO: filter the lines by color and line distance

    cv2.imshow('image', im)
    cv2.imshow('edge', edge)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

角点检测代码

import cv2
import numpy as np

def main():
    frame = cv2.imread("chessboard.jpg")

    cv2.namedWindow("Frame", cv2.WINDOW_NORMAL)

    corners = []

    corners = find_chessboard(frame)

    print(corners)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return True

def find_chessboard(frame):
    img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    corners = cv2.goodFeaturesToTrack(img, 81, 0.01, 10)
    corners = np.int0(corners)

    corners_coordinates = [[]]*len(corners)

    i = 0
    for corner in corners:
        x, y = corner.ravel()
        corners_coordinates[i] = [x, y]
        i = i+1

        cv2.circle(frame, (x, y), 5, (255, 0, 0), -1)
    cv2.imshow("Frame", frame)

    return corners_coordinates

if __name__ is "__main__":

    main()

内容的提问来源于stack exchange,提问作者SmartlessName

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 09:20:40