Python - Numpy 3D 数组 - 连接问题



我有一个包含 46 个条目的 txt 文件,如下所示 -

2020-05-24T10:57:12.743606#[0.0, 0.0, 0.0653934553265572, 0.0, 1.0, 0.0]
2020-05-24T10:57:12.806380#[0.0, 0.0, 0.0, 0.0, 1.0, 0.0]
2020-05-24T10:57:12.869022#[0.0, 0.0, 0.0, 0.0, 1.0, 0.0]

第一个参数是拍摄的相机图像的时间戳。对于每个时间戳,有 3 个 RGB 图像。

我的目标是沿通道轴(轴 = 2(连接它们。图像尺寸为 70x320x3。因此,所需的输出为 46x70x320x9。

我需要等到所有 3 张图像都被识别出来,然后将它们附加到列表中并将其提供给 numpy 数组。我失败了,因为我得到的输出尺寸是 46x138(对于附加的 3 张图像(x70x320x346x138x70x320x3连接前。使用axis =2 or 3实现串联不起作用

我怎样才能得到46x70x320x9

法典-

with open("train.txt", 'r') as f:
data = f.readlines()[:]
images = []
image_concat = []
labels = []
for row in data:
for camera in ['center', 'left', 'right']:
img_id, label = row.strip("n").split("#")
img_path = os.path.join(IMG_PATH, '{}-{}.jpg'.format(camera, img_id))
image = cv2.imread(img_path)
images.append(image)
if camera == 'right':
image_concat.append(images)
X_data = np.array(image_concat)
print(X_data.shape)

参考链接 -

需要帮助将两个 3 通道图像组合成 6 通道图像 Python

numpy:沿第 3 维连接两个数组

numpy 连接多个数组数组

Numpy 在维度上连接

请帮忙。任何帮助将不胜感激。谢谢。

这是一个包含虚拟数据的实现

collect = []
for i in range(46):
#create dummy arrays, simulate list of 3 RGB images
a = [np.zeros((70,320,3)) for b in range(3)]
# a[0].shape: (70,320,3) 
#concatenate along axis 2
b = np.concatenate(a, axis=2)
# b.shape: (70,320,9)
#create new axis in position zero
b = b[np.newaxis, ...]
# b.shape : (1,70,320,9)
collect.append(b)
output = np.concatenate(collect, axis=0)
output.shape
(46, 70, 320, 9)

编辑:

# IIUC:
# left camera makes 70,320,3 at time t
# right camera makes 70,320,3 at time t
# center camera makes 70,320,3 at time t
# these need to be concatenated to 70,320,9
# if so, you can use a dictionary
#initialise dict
collected_images = {}
for timepoint, row in enumerate(data):
#at every timepoint, initialise dict entry
collected_images[timepoint] = []
for camera in ['center', 'left', 'right']:
image = cv2.imread('path/to/image')
collected_images[timepoint].append(image)
# now you have all images in a dictionary
# to generate the array, you can
output = []
for key, val in collected_iamges.items():
temp = np.concatenate(val, axis=2)
output.append(temp[np.newaxis, ...])
output = np.concatenate(output, axis=0)

在@warped的第一个答案之后,我发现文本文件中的输出列表是问题所在。它一口气倾倒了所有线路。经过几次尝试,我最终使用了csv.reader,这使事情变得容易得多。之后,只是扩展了@warped的第二个答案并完成了任务。

with open('train.txt', 'r') as f:
lines = f.readlines()
data = csv.reader(lines, delimiter = "#")
for count, index in enumerate(data):
img_id = index[0]
label = [float(item) for item in index[1][1:-1].split(",")]

从这里开始的标签解决方案 - Python - 将字符串列表转换为浮点数 - 方括号和小数点导致问题

在它基本上使用答案之后。

这个链接帮助我选择了csv阅读器 - Python没有正确读取文本文件?

最新更新