【发布时间】: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 文件中的结构,其中每个像素在一列中。
编辑:
每次迭代要添加的向量是img1 和img2
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或data是什么。不过,我希望您发现我的回答很有用,因为使用numpy.stack而不是append解决了您需要解决的根本问题。
标签: arrays python-3.x pandas numpy dataframe