[英]Python - Numpy 3D array - concatenate issues
我有一个包含 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。 所以所需的 output 是 46x70x320x9。
我需要等到所有 3 个图像都被识别出来,然后 append 将它们添加到列表中,并将其提供给 numpy 数组。 我失败了,因为我得到的 output 尺寸是 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)
参考链接——
需要帮助将两个 3 通道图像组合成 6 通道图像 Python
请帮忙。 任何帮助将不胜感激。 谢谢你。
这是一个带有虚拟数据的实现
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的第一个答案之后,我发现文本文件中的 output 列表是问题所在。 它将所有行都倾倒在一个 go 中。 经过几次尝试,我最终使用了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(",")]
Label 解决方案从这里 - Python - 将字符串列表转换为浮点数 - 方括号和小数点导致问题
之后它基本上是使用答案。
此链接帮助我选择 csv 阅读器 -- Python 无法正确读取文本文件?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.