将elemnets附加到numpy nd数组

时间:2018-03-09 09:56:29

标签: python numpy multidimensional-array

执行houghlinesp

我试图过滤掉角度为85到90度的线

def filter_line(lines):
"""
Filter the input the input lines to +/- 5 deg.
    Parameters:
        image: The output of a Canny transform.
"""
filtered_line= np.empty([0, 1, 4])
if lines is not None:
    for i in range(0, len(lines)):
        l = lines[i][0]
        dx = l[2]-l[0]
        dy = l[3]-l[1]
        angle= degrees(atan2(dy, dx))
        if (angle > 85):
            print(l)
            print(type(l))
            print(l.shape) 
            np.reshape(l, (1, 1, 4))
            print(l.shape)
            filtered_line = np.append(filtered_line, l, axis=0)
    return filtered_line

type(lines) - > ' numpy.ndarray'

lines.shape - > (29,1,4)

行 - > [[[339 475 339 4]] [[224 4 416 4]] [[269 33 400 33]] ...]]]

l - > [339 475 339 4]

l - > ' numpy.ndarray'

l - > (4)

我希望输出相同维度的filtered_line

  

ValueError:所有输入数组必须具有相同的维数

不确定如何使其发挥作用。

1 个答案:

答案 0 :(得分:0)

我怀疑重复使用np.append而不是列表理解然后转换为数组有什么好处。我会做的

def filter_line(lines):
    """
    Filter the input the input lines to +/- 5 deg.
        Parameters:
            image: The output of a Canny transform.
    """
    filtered_lines = []
    if lines is not None:
    for i in range(0, len(lines)):
        l = lines[i][0]
        dx = l[2]-l[0]
        dy = l[3]-l[1]
        angle= degrees(atan2(dy, dx))
        filtered_lines.append(l)
        if (angle > 85):
            filtered_lines.append(l)
    return np.array(filtered_lines)