用于存储和操作高效像素的数据结构:



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

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

l希望将其存储在矩阵中,以便每条线表示图像的一个向量 (3072)。 这意味着 l 在末尾得到一个矩阵 (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]
]

其中 l 可以读取表单示例

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)
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)

你想使用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)

最新更新