简体   繁体   English

来自CV2的Numpy Flatten数组

[英]Numpy flatten array of array from cv2

Wait! 等待! Before you 'harrumph' and downvote, this seems like a repeat question but I've looked at the other questions and they don't really fit my use case. 在您“嘲笑”并拒绝投票之前,这似乎是一个重复的问题,但是我已经看过其他问题,它们确实不适合我的用例。

I was reading images one by one into a list with cv2, with the intention of running a classifier over them. 我正在用cv2将图像一张一张地读取,目的是对它们运行分类器。 eg: 例如:

lst = []

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

This resulted in the lst array being like so: 这导致第一个数组如下所示:

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)

Which is not really what i wanted. 这不是我真正想要的。 I wanted lst to be an (10000,311520) array which can be thrown to a classifier, but now lst is an (10000,) array, while the individual elements are of shape (311520,) 我希望lst是可以抛出分类器的(10000,311520)数组,但是现在lst是(10000,)数组,而单个元素的形状是(311520,)

I've tried np.flatten (doh) , np.concatenate, np.hstack / vstack. 我已经尝试过np.flatten(doh),np.concatenate,np.hstack / vstack。 None of them help. 他们都没有帮助。

Is there something im missing that would help? 有什么我想念的东西会有所帮助吗? Is this even the right way of doing it? 这是正确的方法吗?

Thanks so much for your help! 非常感谢你的帮助! :) :)

Here's a suggestion. 这是一个建议。 Only works if all images are indeed the same size after flattening. 仅在展平后所有图像的尺寸确实相同时才起作用。

lst = []
for picture in directory:
    img = cv2.imread(picture)
    img = img.flatten() # because classifiers require it to be flat
    if lst = []:
        lst = img
    else:
        lst = concatenate((lst, [img]), axis=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM