使用 python 和 opencv 从特定目录中读取多个图像,预先拥有并将它们保存在另一个目录中

Read multiple images from specific directory, prepossessed and save them another directory using python and opencv

我是 Python 和 OpenCV 的初学者。我想从特定目录中读取多个图像并使用 CLAHE(对比度受限自适应直方图均衡)对其进行预处理,最后将它们(预处理图像)保存到另一个目录。我试过了,但它适用于单个图像。我如何解决它?下面是我的代码-

import numpy as np
import cv2
import glob

img = cv2.imread('00000001_000.png',0)

#create a CLAHE object (Arguments are optional).
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(20,20))
cl1 = clahe.apply(img)
cv2.imwrite('clahe_21.jpg',cl1)

尝试列出所有图像的路径并遍历它们:

all_img = glob.glob('.')
other_dir = 'new_path'
for img_id, img_path in enumerate(all_img):
    img = cv2.imread(img_path,0)

    #create a CLAHE object (Arguments are optional).
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(20,20))
    cl1 = clahe.apply(img)
    cv2.imwrite(f'{other_dir}/clahe_21_{img_id}.jpg',cl1)