我有一个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
我发现有帮助的是计算线条(矢量)上的一个窗帘点的像素值。代码如下:
coordA = [0,0]
coordB = [3,4]
def intermid_pix(coorA, coorB, nb_points=8):
x_axis = (coorB[0] - coorA[0]) / (nb_points + 1)
y_axis = (coorB[1] - coorA[1]) / (nb_points + 1)
rounded = [[round(coorA[0] + i * x_axis), round(coorA[1] + i * y_axis)]
for i in range(1, nb_points + 1)]
rounded_trim = []
[rounded_trim.append(x) for x in rounded if x not in rounded_trim]
return rounded_trim
coordinates = intermed_pix(coordA, coordB, nb_points=8)
print(coordinates)
输出:
[[0, 0], [1, 1], [1, 2], [2, 2], [2, 3], [3, 4]]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句