【问题标题】:How to store an image in pandas dataframe column?如何将图像存储在熊猫数据框列中?
【发布时间】:2018-10-25 11:32:19
【问题描述】:

我有以下代码行。

v = chemcepterize_mol(mol, embed=10, res=0.2)

函数chemcepterize_mol 接受一些参数,例如mol, embed, res。 这个函数chemcepterize_mol返回一个值,我存储在v中,这是一个普通变量。

这个v其实是一个代表三维图像的向量。

v的形状如下。

print(v.shape) 
(100, 100, 4)

100X100 像素图像,4 通道。

当我展示这张图片时,它看起来像下面。

plt.imshow(v[:,:,:3])

到目前为止,它工作正常。但是我需要将这些类型的图像(3 维向量)存储在我的数据框中。这意味着,我需要调用这个函数chemcepterize_mol 几次,比如说 10 次以获得 10 张图像。

为此,我在下面编写了一个 for 循环。

v = np.ndarray(shape=(100,100, 4,1))
for x in range(10):

   v[x]=chemcepterize_mol(data["mol"][x],embed=10,res=0.2)

这里,data["mol"][x]是我的数据框datamol列的x条目。对于这些 data["mol"][x] 值中的每一个,chemcepterize_mol 会给我一个不同的 3 维向量,我需要存储它。

我声明了v = np.ndarray(shape=(100,100, 4,1)),以便在它的最后一个维度中,我可以存储我所有的向量(图像),但它给了我这个错误。

ValueError: could not broadcast input array from shape (100,100,4) into shape (100,4,1)

如何将所有向量存储在某个数组中,或者最好存储在单独的 pandas 数据框列中。

【问题讨论】:

    标签: python pandas multidimensional-array conv-neural-network array-broadcasting


    【解决方案1】:

    可以将图像包装在 python 列表中。这样就可以将其存储在 pandas DataFrame 中。

    import cv2
    import pandas as pd
    
    img1 = cv2.imread('img1.png')
    img2 = cv2.imread('img2.png')
    
    df = pd.DataFrame()
    df['img'] = [img] # Wrap image in python list
    
    # Add another row using the "dictionary way"
    d2 = {'img': [img2]}
    df2 = pd.DataFrame.from_records(d2)
    df.append(df2)
    

    【讨论】:

      猜你喜欢
      • 2018-02-16
      • 2022-11-13
      • 2018-03-20
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 2019-05-07
      相关资源
      最近更新 更多