值错误:在尝试存储图像 HoG 特征时设置具有序列的数组元素



我在将转换后的数组分配给新变量时收到 ValueError。我希望变量img_hogs由每个图像的 HoG 特征组成。所以它是一个2D数组,其中每一行代表一个图像的HoG特征。

tmp_hogs = [] 
for filen in out: # get hog features for each training image
        filepath = "C:\path\cropped_cars\"
        readfile = filepath + filen
        curr_img = color.rgb2gray(io.imread(readfile))
        imgs.append(curr_img)
        fd, hog_image = hog(curr_img, orientations=8, pixels_per_cell=(16, 16),
                 cells_per_block=(1, 1), visualise=True, normalise=True)
        tmp_hogs.append(fd)
        i+=1
img_hogs = np.array(tmp_hogs, dtype=float)

错误出现在上面的最后一行,说:

Traceback (most recent call last):
  File "C:pathgbc_classifier_test_2.py", line 71, in <module>
    img_hogs = np.array(tmp_hogs, dtype=float) 
ValueError: setting an array element with a sequence.

这里有什么问题?我以前似乎没有问题地使用它。我还没有初始化img_hogs任何东西。

编辑

当我打印出tmp_hogs时,我得到数组,这是一个:

array([  9.43988506e-02,   1.16434393e-01,   1.66949456e-01,
         1.67258585e-01,   1.55137816e-01,   1.18421903e-01,
         7.17979933e-02,   1.07738525e-01,   5.04513644e-02,
         .. # and it goes on for about 270 lines...
         1.02937285e-01,   3.79174496e-02,         2.82693731e-02])

然后这个:

, array([ 0.17943262,  0.17266624,  0.18836   , ...,  0.03710205,
        0.01623901,  0.00623639]), array([ 0.12019744,  0.18129971,  0.17268482, ...,  0.03478858,
        0.01075355,  0.0086803 ]), array([ 0.13604284,  0.11251585,  0.13396777, ...,  0.07344526,
        0.01842701,  0.00995614]), array([ 0.09036218,  0.15552837,  0.14932259, ...,  0.09498851,
        0.08212626,  0.01198428]), array([ 0.03504824,  0.07993138,  0.08090841, ...,  0.0207428 ,
        0.00867545,  0.00285672]), array([ 0.05378212,  0.01994047,  0.03666057, ...,  0.09112556,
        0.02215141,  0.01434096]), array([ 0.14161055,  0.14635719,  0.1749262 , ...,  0.03148477,
        0.02349815,  0.02309164]), array([ 0.11262369,  0.10024635,  0.14424181, ...,  0.08426981,
        0.0486145 ,  0.026758  ]), array([ 0.03906336,  0.03750173,  0.13635017, ...,  0.13814893,
        0.07869883,  0.05370808]), array([ 0.04162636,  0.053963  ,  0.10114257, ...,  0.10504406,
        0.03798734,  0.01648739]), array([ 0.02912047,  0.05139863,  0.13072171, ...,  0.14025979,
        0.03743591,  0.01188805]), array([ 0.06857062,  0.09075094,  0.20136449, ...,  0.09126319,
        0.05310212,  0.05257662]), array([ 0.02797054,  0.01915037,  0.09330322, ...,  0.078432  ,
        0.02443184,  0.0265646 ]), array([ 0.1754544 ,  0.10865344,  0.23155207, ...,  0.10221248,
        0.07416729,  0.02623395]), array([ 0.0574084 ,  0.05975376,  0.17089832, ...,  0.11830615,
        0.05896331,  0.02676903])]

等等。

哎呀 - 答案很简单。在取出 HoG 功能之前,我必须先调整所有图像的大小,以便数组的大小都相同。

因此,在将每个图像附加到imgs列表之前,我添加了以下行:

curr_img = resize(curr_img, (50,100))

这解决了问题。

最新更新