Numpy从cv2压平数组数组

时间:2016-06-29 17:24:39

标签: python numpy

等待!在你之前' harrumph'这是一个重复的问题,但我已经看过其他问题而且它们并不适合我的用例。

我正在逐一将图像读入带有cv2的列表中,目的是在它们上面运行分类器。例如:

lst = []

for picture in directory:
    img = cv2.imread(picture)
    img = img.flatten() # because classifiers require it to be flat
    lst.append(img)

这导致lst数组如此:

array([array([43, 25,  8, ..., 70, 68, 50], dtype=uint8),
       array([ 24,  40,  16, ..., 182, 183, 167], dtype=uint8),
       array([ 39,  35,  34, ..., 117, 114, 106], dtype=uint8), ...,
       array([31, 50, 41, ..., 16, 16, 10], dtype=uint8),
       array([ 14,  17,  15, ...,  95, 109, 105], dtype=uint8),
       array([101, 102, 122, ..., 178, 187, 214], dtype=uint8)], dtype=object)

这不是我想要的。我希望lst是一个(10000,311520)数组,可以抛出到分类器,但现在lst是一个(10000,)数组,而各个元素的形状(311520,)

我已经尝试了np.flatten(doh),np.concatenate,np.hstack / vstack。他们都没有帮助。

有什么东西可以用来帮助吗?这甚至是正确的做法吗?

非常感谢你的帮助! :)

1 个答案:

答案 0 :(得分:0)

这是一个建议。只有在展平后所有图像的尺寸确实相同时才有效。

p2 := plot3d([x^2, 3^6*z-432], x=-50..50, z=0..20):
plots:-display([p1,p2]);
相关问题