【问题标题】:data structure to store and manipulate efficienty pixels :存储和操作高效像素的数据结构:
【发布时间】:2017-03-31 15:08:43
【问题描述】:

我有一组图像(1000 张图像)。每一个的维度为 3072。 每个图像都有这样的表示: 例如图片 1:

array([255, 78, 48, ..., 190, 230, 178], dtype=uint8)

我想将其存储在矩阵中,这样每行代表图像的一个向量 (3072)。这意味着我最后得到一个 (1000,3072) 的矩阵 这是我尝试过的

matrix_of_images= []
for img in images:
    data.append(img)
    data.append(img2)

但是附加列表给了我一个难以操作的结构,因为我想将它存储在 csv 文件中然后调用图像的一部分。

[array([255, 255, 255, ..., 255, 255, 255], dtype=uint8), array([0, 0, 0, ..., 0, 0, 0], dtype=uint8), array([0, 0, 0, ..., 0, 0, 0], dtype=uint8), array([255, 255, 255, ..., 255, 255, 255], dtype=uint8), array([255, 255, 255, ...,   0,   0,   0], dtype=uint8), array([  0,   0,   0, ..., 255, 255, 255], dtype=uint8), array([255, 255, 255, ...,   0,   0,   0], dtype=uint8), array([  0,   0,   0, ..., 255, 255, 255], dtype=uint8), array([255, 255, 255, ...,   0,   0,   0], dtype=uint8), array([  0,   0,   0, ..., 255, 255, 255], dtype=uint8)]

我正在寻找类似的东西 X=

[

[23,56, 78,....,45,156],
[60,56, 104,....,145,157],
[78,45, 7,....,0,15],
[45,56, 178,....,5,200]

]

其中我可以阅读表单示例

X[1] #  second image
 [60,56, 104,....,145,157]
X[1][2] # third pixel of second image
104

一种易于存储在 csv 文件中的结构,其中每个像素在一列中。

编辑: 每次迭代要添加的向量是img1img2

for i in range(1,500):

    #get coordinates
    #coords=npa[i,:]
    coords=npa.iloc[[i]]
    charac=characs[i-1]
    if (charac== "'/'"):
        charac= "'slash'"

    charac = charac.strip('"\'')
    #actual cropping of the image (easy with numpy)
    #img_charac=img[int(coords[2]):int(coords[4]),int(coords[3]):int(coords[5])]
    img_charac = img[int(coords[4]):int(coords[5]), int(coords[2]):int(coords[3])]
    #cv2.imwrite(path_save_cropped_images + str(charac) + "_" + str(i) + "_" + str(img_charac.shape) + ".png",  img_charac)

    #resize
    img_charac_resized=cv2.resize(img_charac, (32, 32), interpolation=cv2.INTER_NEAREST)
    #cv2.imwrite(path_save_resized_images + str(charac) + "_" + str(i) + "_" + str(img_charac_resized.shape) + ".png",img_charac_resized)
    #img_charac = cv2.resize(img_charac, (32, 32))

    #switch images
    img_charac_switched = 255 - img_charac_resized
    #cv2.imwrite(path_save_switched_pixels+ str(charac) +"_switched"+ "_" + str(i) + "_" + str(img_charac_switched.shape) + ".png",img_charac_switched)

    img1 = img_charac_resized.reshape((-1, 1))
    img1 = img1.T
    img1= img1.flatten()
    img1_label=charac

    img2=img_charac_switched.reshape((-1,1))
    img2=img2.T
    img2=img2.flatten()
    img2_label = charac
    #x=switch(charac)
    #saving the image

    #dataset

    #cv2.imwrite(path_dataset+ str(charac) + "_switched" + "_" + str(i) + "_" + str(img_charac_switched.shape) + ".png",img_charac_switched)
    #cv2.imwrite(path_dataset + str(charac) + "_" + str(i) + "_" + str(img_charac_resized.shape) + ".png",  img_charac_resized)


    #images = [img1,img2]
    img_arr = np.stack(img1, axis=0)
    img_arr = np.stack(img2, axis=0)
    #data.append(img1)
    #data.append(img2)

    #print (img_arr.shape)
    #print(i)



print(img_arr)
print(img_arr.shape)

【问题讨论】:

  • 我编辑了我的答案以响应您的更改。今后,请确保问题完全反映了您所面临的问题,并且代码完整。例如,您没有明确说明您试图一次附加两个列表,也没有明确定义变量imagesdata 是什么。不过,我希望您发现我的回答很有用,因为使用 numpy.stack 而不是 append 解决了您需要解决的根本问题。

标签: arrays python-3.x pandas numpy dataframe


【解决方案1】:
images=[]

for i in range(1,500):




    img1= img1.flatten()


    img2=img2.flatten()
    #saving the image

    images.append(img1)
    images.append(img2)


img_arr = np.stack(images, axis=0)
print(len(images))
print(img_arr)
print(img_arr.shape)

【讨论】:

    【解决方案2】:

    您想使用numpy.stack。指定axis=0垂直堆叠:

    import numpy as np
    n = 1000
    images = [np.random.random(3072) for _ in range(n)]
    img_arr = np.stack(images,axis=0)
    
    >>> img_arr.shape
    (1000, 3072)
    

    对于您的代码:

    images = []
    for i in range(500):
        # create img1 and img2
        images.extend([img1,img2])
    img_arr = np.stack(images,axis=0)
    

    【讨论】:

    • 我需要在我的代码中的 for 循环内我有 img1 和 img2 以按顺序添加到我的 numpy.stack
    • 在每个 for 循环迭代中,依次添加 img1 和 img2。这意味着在每次循环迭代后,我的 img_arr 将增加 2,直到在 for 循环结束时达到 1000。希望更清楚
    • 不,不清楚。你已经在images 中有一个np.array 对象列表。您所要做的就是将已有的列表堆叠起来。
    • 请看我的更新我分享了代码块,我在每次迭代中提取两个子图像:img1 和 img2。在每次 for 循环迭代中,我将 img1 和 img2 的向量添加到我的堆栈列表中,这意味着在每个 1 结束时 for 循环迭代我的 numpy.堆栈将增加 2 并且在我的 for 循环结束时我得到形状 (1000,3072) 但在每次迭代中它增加 (2,3072)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多