python - 从 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
解决方案
如果您希望这样做,您可以插入图像以提取线条轮廓,这样坐标不必是整数:
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
推荐阅读
- python - 调用更新函数时,Python Bokeh 图显示两条线
- html - 从 scss 文件中获取值
- react-native - react-native 如何在多个项目之间共享代码?
- android - 网格布局不显示
- blockchain - _ 语句的 Solidity Point
- java - Proof that old Date Java API is not thread safe
- powershell - Conditional criteria in powershell group measure-object?
- angular - My custom validator is not working, reactive forms Angular 6
- beautifulsoup - print() not working in Python shell?
- javascript - jQuery.signature works fine until scrolling and then