本文介绍了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]的线与哪些像素交叉?

I have a gray scale 50 x 50 pixels picture as a numpy 2D array.

Each pixel is a coordinate starting top left [ 0 , 0 ] to bottom right [ 50 , 50 ].

How do i get coordinates of each pixel that is on the line from point A to B where those points can be any given pixel pairs i.e. A[ 19 , 3 ] to B[ 4 , 4 ] or A[ 3 , 12 ] to B[ 0 , 33 ]?

Example: line from A [ 4 , 9 ] to B[ 12 , 30 ] crosses which pixels?

预先感谢

Evo

Thanks in advance
Evo

推荐答案

我发现有帮助的是计算线(矢量)上的一个窗帘点的像素值。下面的代码:

What I figure out helps is to calculate pixel value at a curtain point on the line (vector). Code below:

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]]

这篇关于A点到B点的像素坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:04