代码之家  ›  专栏  ›  技术社区  ›  Sergio Lopez

以CSV格式保存阵列3D

  •  1
  • Sergio Lopez  · 技术社区  · 7 年前

    抱歉,我在csv中保存数据时遇到问题,我想保存的是一个3d图像数组,我可以保存它,读取csv时出现问题,因为它保存为字符串,所以我无法读取数据来创建图像,请尝试使用astype(“uint8”),因为这是我需要的格式,有人帮助我如何保存我需要的格式的数据,或在恢复数据时更改格式。

        new_dt=pd.read_csv('mypic2.csv')
    
        for x in range(len(a.index)):
            imagess=a.img[x]
            print(imagess)
            cv2.imshow('imagenew', imagess)
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Hiral Bhimani    7 年前

    现在还不清楚为什么要使用pandas数据帧来存储图像。我认为这让事情变得不必要的复杂。您可以直接以二进制格式存储numpy数组,并在以后的某个时刻再次加载它。

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    #create an image
    imar = np.array([[[1.,0.,1.],[0.,0.,1.],[1.,1.,0.]],
                     [[0.,1.,1.],[0.,1.,1.],[1.,2.,3.]],
                     [[0.,0.,1.],[1.,1.,1.],[1.,0.,0.]]]).transpose()
    plt.imsave('pic.png', imar)
    
    #create dataframe
    
    df = pd.DataFrame([[0,""]], columns=["Feature1","Feature2"])
    
    # read the image
    im = plt.imread('pic.png')
    
    plt.imshow(im)
    plt.show()
    
    #save the image array to binary file
    np.save('mypic.npy', im)
    # store name of image in dataframe
    df.iloc[0,1] = 'mypic.npy'
    #save dataframe
    df.to_csv("mydf.csv")
    del df
    
    #read dataframe from csv
    df = pd.read_csv("mydf.csv")
    # load the image from binary file, given the path from the Dataframe
    new_im= np.load(df["Feature2"][0])
    # show the loaded image
    plt.imshow(new_im)
    plt.show()
    
        2
  •  1
  •   Quang Hoang    7 年前

    opencv ,为什么不使用 cv2.imwrite ,它与您的 cv2.imshow

    开放式CV 图像正在使用 numpy.save 自从 开放式CV np.array .

    如果你必须把它保存在 csv

              width . height .  0  .  1  . ...
    image_1 .   w1  .   h1   .  f0 .  f1 . ...
    image_2 .   w2  .   h2   .  f0 .  f1 . ...
    

    其中每行包含图像的宽度和高度,后跟 image.flatten() . 然后可以将脚本修改为

    new_dt=pd.read_csv('mypic2.csv')
    
    for x in range(len(dt.index)):
        # recover image's dimension
        width = new_dt.iloc[x,0]
        height = new_dt.iloc[x,1]
    
        # load image and reshape to original dimension
        images = new_dt.iloc[x,2:].reshape(height, width, -1)
        print(imagess)
        cv2.imshow('imagenew', imagess)