如何提高数组的for循环速度?

时间:2019-06-18 01:36:07

标签: python-3.x numpy

我有线段点(x1,y1),(x2,y2),并且具有两种不同的图像分辨率: 一种。 255x480 b。 848x480

我想要生成一个x坐标在图像b的红色区域中的蒙版。

我使用for循环生成,有没有有效的方法?

def get_all_pts_on_line(x1, y1, x2, y2):
    points = []
    issteep = abs(y2-y1) > abs(x2-x1)
    if issteep:
        x1, y1 = y1, x1
        x2, y2 = y2, x2
    rev = False
    if x1 > x2:
        x1, x2 = x2, x1
        y1, y2 = y2, y1
        rev = True
    deltax = x2 - x1
    deltay = abs(y2-y1)
    error = int(deltax / 2)
    y = y1
    ystep = None
    if y1 < y2:
        ystep = 1
    else:
        ystep = -1
    for x in range(x1, x2 + 1):
        if issteep:
            points.append((y, x))
        else:
            points.append((x, y))
        error -= deltay
        if error < 0:
            y += ystep
            error += deltax
    # Reverse the list if the coordinates were reversed
    if rev:
        points.reverse()
    return points

line_pts = get_all_pts_on_line(x1, y1, x2, y2)

for pt in line_pts:
            for i in range(pt[0]):
                image_b[pt[1],:][image_a[pt[1], :]==i] = 0

enter image description here

0 个答案:

没有答案