【发布时间】:2020-05-17 00:59:27
【问题描述】:
好的,这是一个很长的问题。问题来了:
问题:所以我将图像(3d 数组)传输到一个 numpy 数组中。我正在尝试获取一个包含我所有图像的 4d 数组,但由于某种原因,它会打印出一个包含 3d np.arrays 的 1d np.array。
建议的解决方案: - 图像有不同的尺寸。情况确实如此,但在调整它们的大小时没有帮助。
首先我要传输每张图片:
cv2.imdecode(np.frombuffer(image_bytes, np.uint8), -1) 将我的图像从字节码转换为 3d np 数组。
def transferPictures():
x_dataset = []
y_dataset = []
x_decoded = []
shoeNames = os.listdir(SHOES_DIRECTORY)
print(len(shoeNames))
for shoes in shoeNames[:20]:
shoeDirectoriesPath = os.path.join(SHOES_DIRECTORY, shoes)
if(os.path.isdir(shoeDirectoriesPath)):
eachPicture = os.listdir(shoeDirectoriesPath)
for pic in eachPicture:
print(pic)
picPath = os.path.join(shoeDirectoriesPath, pic)
f = open(picPath, 'rb')
image_bytes = f.read()
#This is to convert the image from byte format to a 3d np array:
decoded = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), -1)
x_dataset.append(decoded)
y_dataset.append(pic)
return [x_dataset, y_dataset]
并将我的 x_dataset 列表(包含我所有的图像)转换为 np.array():
dataset = transferPictures()
x_dataset=np.array(dataset[0])
我得到一个包含 3d np.arrays 的 1d x_dataset np.array:
print("This is the type of the array of images, x_dataset: "+ str(type(x_dataset)))
print("This is the type of each image, x_dataset[0]: "+ str(type(x_dataset[0])))
print("This is the dimension of the array of images, x_dataset: "+ str(np.shape(x_dataset)))
print("This is the dimension of each image, x_dataset[0]: "+ str(np.shape(x_dataset[0])))
打印:
This is the type of the array of images, x_dataset: <class 'numpy.ndarray'>
This is the type of each image, x_dataset[0]: <class 'numpy.ndarray'>
This is the dimension of the array of images, x_dataset: (130,)
This is the dimension of each image, x_dataset[0]: (289, 200, 3)
为什么np.shape(x_dataset) 返回 (130,) 和 np.shape(x_dataset[0]) 返回 (289, 200, 3)?
1) np.shape(x_dataset) 不应该返回 (130, 289, 200, 3) 吗?
2) 如何获得形状为 (130, 289, 200, 3) 的 4d x_dataset?
编辑:
V。 Ayrat 认为这可能是因为每张图像的形状不同。有些图像实际上与其他图像具有不同的尺寸。所以我调整了所有图像的大小以具有相同的形状。但是,问题似乎仍然存在:
查找最小图像的尺寸:
minx = 1000
miny = 1000
minz = 1000
for image in x_dataset:
if np.shape(image)[0] < minx:
minx = np.shape(image)[0]
if np.shape(image)[1] < miny:
miny = np.shape(image)[1]
if np.shape(image)[2] < minz:
minz = np.shape(image)[2]
print(minx)
print(miny)
print(minz)
打印:
288
200
3
调整图片大小:
for i in range(0,len(x_dataset)):
x_dataset[i] = np.resize(x_dataset[i], (minx,miny,minz))
图像具有所有相同的尺寸,但检查尺寸 x_dataset 仍然返回 (130,)。让它返回一个 4d 数组的任何其他想法?
编辑 2: 100% 确保每张图片的尺寸相同:
minx = 1000
miny = 1000
minz = 1000
maxx = 0
maxy = 0
maxz = 0
for image in x_dataset:
if np.shape(image)[0] < minx:
minx = np.shape(image)[0]
if np.shape(image)[1] < miny:
miny = np.shape(image)[1]
if np.shape(image)[2] < minz:
minz = np.shape(image)[2]
if np.shape(image)[0] > maxx:
maxx = np.shape(image)[0]
if np.shape(image)[1] > maxy:
maxy = np.shape(image)[1]
if np.shape(image)[2] > maxz:
maxz = np.shape(image)[2]
print()
print(minx)
print(miny)
print(minz)
print(maxx)
print(maxy)
print(maxz)
打印:
288
200
3
288
200
3
并重新检查类型:
print(type(x_dataset))
print(type(x_dataset[0]))
print(np.shape(x_dataset))
打印:
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
(130,)
这真是令人困惑......
【问题讨论】: