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

基于YOLO检测棋盘四角后绘制8×8网格的技术问询

基于检测到的棋盘四角点绘制8×8网格

问题说明

我有一张棋盘照片,已通过YOLO检测出棋盘的四个角点并获取了它们的x、y坐标,希望在这四个角点之间沿棋盘线条绘制一个8×8网格。

现有代码

from matplotlib.pyplot import figure
import matplotlib.pyplot as plt
import matplotlib.image as image

figure(figsize=(10, 10), dpi=80)

im = plt.imread('1.jpgresized.jpg')
implot = plt.imshow(im)

# 左侧边
plt.plot([0.179167 * 480, 0.063542 * 480], 
         [0.203125 * 640, 0.644531 * 640], 
         'bo', linestyle="--")

# 底部边
plt.plot([0.063542 * 480, 0.934375 * 480], 
         [0.644531 * 640, 0.627344 * 640], 
         'bo', linestyle="--")

# 右侧边
plt.plot([0.934375 * 480, 0.789583 * 480], 
         [0.627344 * 640, 0.191406 * 640], 
         'bo', linestyle="--")

# 顶部边
plt.plot([0.789583 * 480, 0.179167 * 480], 
         [0.191406 * 640, 0.203125 * 640], 
         'bo', linestyle="--")

plt.show()

解决方案

要绘制8×8网格,核心是对四边形四条边做等分,再连接对应等分点,具体实现如下:

1. 统一管理角点坐标

先将检测到的归一化坐标转换为图像实际像素坐标,按左上、左下、右下、右上的顺序存储,方便后续操作:

import numpy as np
from matplotlib.pyplot import figure
import matplotlib.pyplot as plt

# 图像尺寸
img_width = 480
img_height = 640

# 四个角点(左上、左下、右下、右上)
corners = [
    (0.179167 * img_width, 0.203125 * img_height),  # 左上
    (0.063542 * img_width, 0.644531 * img_height),  # 左下
    (0.934375 * img_width, 0.627344 * img_height),  # 右下
    (0.789583 * img_width, 0.191406 * img_height)   # 右上
]

2. 生成边的等分点

8×8网格需要9条横向和9条纵向线,因此每条边需分成8段,得到9个等分点:

num_divisions = 8  # 8个格子对应8段分割
num_points = num_divisions + 1  # 每条边的等分点数量

# 生成四条边的等分点
left_points = np.linspace(corners[0], corners[1], num_points)
bottom_points = np.linspace(corners[1], corners[2], num_points)
right_points = np.linspace(corners[2], corners[3], num_points)
top_points = np.linspace(corners[3], corners[0], num_points)

3. 绘制完整网格

连接左右侧对应位置的等分点生成纵向网格线,连接上下侧对应位置的等分点生成横向网格线:

figure(figsize=(10, 10), dpi=80)
im = plt.imread('1.jpgresized.jpg')
plt.imshow(im)

# 绘制纵向网格线
for i in range(num_points):
    plt.plot([left_points[i][0], right_points[i][0]], 
             [left_points[i][1], right_points[i][1]], 
             color='white', linestyle='-', linewidth=1)

# 绘制横向网格线
for i in range(num_points):
    plt.plot([top_points[i][0], bottom_points[i][0]], 
             [top_points[i][1], bottom_points[i][1]], 
             color='white', linestyle='-', linewidth=1)

# 绘制角点和外框(可选)
plt.plot([p[0] for p in corners], [p[1] for p in corners], 'bo', linestyle="--")

plt.show()

补充说明

  • 使用numpy.linspace可确保等分点均匀分布,避免手动计算的误差。
  • 可根据图像明暗调整网格线的颜色、线宽,提升显示清晰度。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 10:55:50