首页 > 解决方案 > 无法使用 OpenCV 连接多个图像

问题描述

我有 408 张图像(256 x 256 像素)。我想将它们水平和垂直堆叠以创建一个大图像,但我在这样做时遇到了麻烦。文件被命名为 5-X-1.jpg 并且它们都在同一个目录中。

如果我能设法生成一行,我应该能够生成其余的。我已经设法用这些图像的路径制作了一个列表,但我无法cv2.imread读取这个数组并用cv2.hconcator水平堆叠它cv2.hstack。返回的错误是“无法将 'list' 类型的对象转换为 'filename' 的 'str'”。

这是我试图开始工作的代码:

PathList = [] # empty list
InitialTermCol = 0

for imgs in range(16):
    ImgRowStack = r'C:\Users\total\Desktop\Angry\AUTOPY\JapanSteal' + '\\5-' + str(InitialTermCol) + '-1.jpg'
    PathList.append(ImgRowStack)
    InitialTermCol = InitialTermCol + 1

images = cv2.imread(PathList)
hor = np.hstack(PathList)

cv2.imshow('Horizontal', hor)
cv2.waitKey()

标签: python-3.xopencv

解决方案


您应该遍历 Pathlist,以访问图像的路径。不读清单。路径列表的格式为 :Pathlist=["pathto1","pathto2","pathto3"] 。因此,对其进行迭代将为您提供图像的路径。

for d in PathList: \\ images = cv2.imread(d) \\ Fullimage = np.hstack((Fullimage,images))

如果这个答案对你有帮助,请采纳!


推荐阅读