【问题标题】:How to Process multiple Images through Opencv in Python如何在 Python 中通过 Opencv 处理多个图像
【发布时间】:2019-03-28 08:10:43
【问题描述】:

我在一个文件夹中有多个图像,我想对其进行处理并向它们应用一些 opencv 函数。

我正在尝试查找文件夹中存在的每个图像的轮廓。
我可以一次处理一个。

单张图片代码

img = cv2.imread('abc.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

_, thresh = cv2.threshold(gray,85,155,cv2.THRESH_BINARY_INV)

thresh = cv2.GaussianBlur(thresh,(11,11),0)

_, contours, _ = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

final = cv2.drawContours(img,contours, -1, (0,255,0), 3)

cv2.imshow('Output', final)
cv2.waitKey(0)
cv2.destroyAllWindows()  

我想要的是对文件夹中存在的多个图像应用这些操作。

【问题讨论】:

  • 使用 for 循环。例如os.listdir

标签: python opencv image-processing computer-vision


【解决方案1】:

您可以编写一个 for 循环并遍历该目录并将此过程应用于目录中的每个图像:

for image in os.listdir('path_ti_images_folder'):
    img = cv2.imread(os.path.join('path_to_images_folder', image))
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    _, thresh = cv2.threshold(gray,85,155,cv2.THRESH_BINARY_INV)

    thresh = cv2.GaussianBlur(thresh,(11,11),0)

    _, contours, _ = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    final = cv2.drawContours(img,contours, -1, (0,255,0), 3)

    cv2.imshow('Output', final)
    cv2.waitKey(0)
    cv2.destroyAllWindows() 

【讨论】:

  • 感谢os 我也将文件保存在另一个文件夹中,谢谢。
  • 将它变成一个函数并在需要时调用它也是一个好习惯。
  • 很好,会做到的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多