python - 一次读取成对的图像文件

标签 python image opencv operating-system computer-vision

因此,我遍历目录并尝试读取一些图像文件,如下所示:

for root, dirs, files in os.walk(path):
    for file in files:
            im1 = cv2.imread(file)

如何一次读取一对一对图像?这样,在上面的代码中,我有类似以下内容:
im2 = cv2.imread(file)

其中的file代表紧随第一张图片之后的即时图片。

谢谢。

最佳答案

要处理文件对:

import os
import itertools

path = "SRC PATH"
for root, dirs, files in os.walk(path):
    for file1, file2 in itertools.izip_longest(files[::2], files[1::2]):
        im1 = cv2.imread(file1)
        if file2:
            im2 = cv2.imread(file2)

我使用 izip_longest 方法只是为了防止您的目录中只有奇数个文件。

关于python - 一次读取成对的图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48294464/

相关文章:

opencv - 在 ROI 内查找轮廓

c - 错误: Bad argument < Array should be CvMat or IplImage>

python - 评估时间序列的频率、持续时间和值

python - 如何在pyspark中计算groupBy后的唯一ID

javascript - 如何用html和javascript显示原始图像数据?

python - 在python中的openCV中绘制一个旋转框

python - 我如何在 opencv 3 中运行 CV_DIST_L2?

Python打印格式变音右对齐

html - 图像出现和消失与 CSS 悬停

javascript - Puppeteer:如何在嵌套选择器中获取 img src?