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

如何从图像提取像素坐标生成指定格式numpy数组供tda-mapper使用

实现方案(Python环境)

你可以直接用OpenCV+Numpy实现坐标提取,支持全量输出和自定义采样,直接匹配tda-mapper的输入格式:

  • 第一步:导入依赖库
import cv2
import numpy as np
  • 第二步:读取图像并生成全量像素坐标
# 替换为你的本地图像路径
img = cv2.imread("test_img.png")
height, width = img.shape[:2]

# 生成所有像素的(x,y)坐标,x对应图像宽度方向,y对应高度方向
x_arr = np.tile(np.arange(width), height)
y_arr = np.repeat(np.arange(height), width)
full_pixel_coords = np.column_stack((x_arr, y_arr))

此时输出的full_pixel_coords就完全符合你要求的格式:

array([[   0,    0],
       [   1,    0],
       [   2,    0],
       ...,
       [1277,  716],
       [1278,  716],
       [1279,  716]])
  • 可选:按需采样降低数据量
    如果你不需要全量像素,可以选择两种采样方式:
  1. 均匀间隔采样
step = 4 # 每4个像素取1个,步长可自行调整
sampled_coords = full_pixel_coords[::step]
  1. 固定数量随机采样
sample_count = 20000 # 总采样点数,可自行调整
sampled_coords = full_pixel_coords[np.random.choice(full_pixel_coords.shape[0], sample_count, replace=False)]
  • 可选:仅提取前景像素坐标
    如果图像有大面积空白背景,可以过滤背景只保留有效像素坐标,降低tda-mapper的计算量:
# 假设背景为纯白色,非白像素判定为前景,阈值可根据你的图像调整
mask = (img != [255,255,255]).all(axis=-1).flatten()
foreground_coords = full_pixel_coords[mask]
  • 注意事项
    如果你的tda-mapper要求坐标原点在图像左下角(OpenCV默认原点为图像左上角),只需翻转y轴坐标即可:
full_pixel_coords[:, 1] = height - 1 - full_pixel_coords[:, 1]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 02:36:04