在图像中查找一条线上的像素坐标。

5
我有一个被表示为2D数组的图像。我想获取从点1到点2经过的线上的像素坐标。
例如,假设我有一个大小为5x4的图像,如下图所示。我有一条从坐标点1 (0, 2) 到坐标点2 (4, 1) 的直线,就像下图中的红色线一样: enter image description here 因此,我希望按照以下列表顺序获取蓝色像素的坐标:[(0, 2), (1, 2), (2, 2), (2, 1), (3, 1), (4, 1)] 如何实现呢?
我正在使用Python和numpy,但任何语言的解决方案(包括伪代码)都将很有帮助。然后我可以尝试将其转换为numpy解决方案。
2个回答

4

1
很高兴它对你有用。祝你的项目好运! - Mark Setchell

2
你可以使用布雷森汉姆直线算法
这里是来自geeksforgeeks的Python代码。
def bresenham(x1,y1,x2, y2):
 
    m_new = 2 * (y2 - y1)
    slope_error_new = m_new - (x2 - x1)
 
    y=y1
    for x in range(x1,x2+1):
     
        print("(",x ,",",y ,")\n")
 
        # Add slope to increment angle formed
        slope_error_new =slope_error_new + m_new
 
        # Slope error reached limit, time to
        # increment y and update slope error.
        if (slope_error_new >= 0):
            y=y+1
            slope_error_new =slope_error_new - 2 * (x2 - x1)
         
     
 
 
# driver function
if __name__=='__main__':
    x1 = 3
    y1 = 2
    x2 = 15
    y2 = 5
    bresenham(x1, y1, x2, y2)

谢谢您发布算法及其理论!我选择了另一个答案,因为那正是我所需要的,但这也非常有用。 - Canol Gökel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接