首页 > 解决方案 > 从 A 点到 B 点的像素坐标

问题描述

我有一个灰度50 x 50 像素的图片作为 numpy 2D 数组。

每个像素都是从左上角[0, 0]到右下角[50, 50]的坐标。

我如何获取从点 A 到 B 的线上的每个像素的坐标,其中这些点可以是任何给定的像素对,即 A[19, 3] 到 B[4, 4] 或 A[3, 12] 到 B [0, 33]?

示例:从 A [ 4 , 9 ] 到 B[ 12 , 30 ] 的线穿过哪些像素?

在此先感谢
Evo

标签: pythonalgorithmnumpy

解决方案


如果您希望这样做,您可以插入图像以提取线条轮廓,这样坐标不必是整数:

from scipy.ndimage import map_coordinates 
from skimage.data import coins 
from matplotlib import pyplot as plt 
import numpy as np

npts = 128 
rr = np.linspace(30, 243, npts) # coordinates of points defined here 
cc = np.linspace(73, 270, npts) 

image = coins() 
# this line extracts the line profile from the image 
profile = map_coordinates(image, np.stack((rr, cc))) 

# can visualise as 
fig, ax = plt.subplots(ncols=2) 
ax[0].matshow(image) 
ax[0].plot(cc, rr, 'w--') 
ax[1].plot(profile) # profile is the value in the image along the line

输出


推荐阅读