gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:23:25 24 4
gpt4 key购买 nike

我有一个包含 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 图像。

我的目标是沿着 channel 轴(轴 = 2)连接它们。图像尺寸为 70x320x3。所以所需的输出是 46x70x320x9。

我需要等到所有 3 个图像都被识别,然后将它们附加到列表并将其提供给 numpy 数组。我失败了,因为我得到的输出尺寸是 46x138(对于来自附加的 3 张图像)x70x320x3 46x138x70x320x3 在连接之前。当使用 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)

引用链接 -

Need help combining two 3 channel images into 6 channel image Python

numpy: concatenate two arrays along the 3rd dimension

numpy concatenate multiple arrays arrays

numpy concatenate over dimension

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

最佳答案

这是一个带有虚拟数据的实现

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)

关于Python - Numpy 3D 数组 - 连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62263547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com