![](/img/trans.png)
[英]Converting a class function to iterate through multiple images with OpenCV and Python
[英]How to Process multiple Images through Opencv in Python
我在一个文件夹中有多个图像,我想处理这些图像并对它们应用一些 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 循环并遍历该目录并将此过程应用于目录中的每个图像:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.