图像中给定两点位置,可以确定一条直线,计算出图像上这两点形成的线段上像素坐标,可以先求出直线方程,然后依次计算出点坐标,但这样需对每一个像素都使用直线方程来计算,这里采用DDA(digital differential analyzer)画直线
def LineDDA(start, end, points):
start_x = start[0]
start_y = start[1]
end_x = end[0]
end_y = end[1]
delta_x = end_x - start_x
delta_y = end_y - start_y
if abs(delta_x) > abs(delta_y):
steps = abs(delta_x)
else:
steps = abs(delta_y)
x_step = delta_x / steps
y_step = delta_y / steps
x = start_x
y = start_y
while steps >= 0:
points.append([round(x), round(y)])
x += x_step
y += y_step
steps -= 1
start = [6, 3]
end = [2, 8]
points = []
LineDDA(start, end, points)
print(points)