我有一组图像(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]
]
其中l可以读取表单示例
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)
答案 0 :(得分:0)
您想要使用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)
答案 1 :(得分:0)
1
2
3
4
5