如何使用open cv将多个图像保存在一个文件夹中?

How to save multiple images in one folder by using open cv?

这是我要修改的代码。一次读取所有图片并将其保存在一个文件夹中,但是在尝试保存时出现错误


import cv2
import glob        
import numpy as np

#empty lists
image_list=[]

images = []
for img in glob.glob(r"C:\Users\user\Documents\Dataset\Test\Abnormal_Resize\Abnormal_Crop\*.png"):
    n= cv2.imread(img)
    images.append(n)
    print (img)

#limit list to 236 elements
image_list = image_list[:100]

#Resizing the image for compatibility
for img in image_list:
    image = cv2.resize(image, (500, 600))

#The initial processing of the image
#image = cv2.medianBlur(image, 3)
image_bw = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

#The declaration of CLAHE 
#clipLimit -> Threshold for contrast limiting
clahe = cv2.createCLAHE(clipLimit = 5)
final_img = clahe.apply(image_bw) 

cv2.imwrite(path+r"C:\Users\user\Documents\Dataset\Test\Abnormal_Resize\Abnormal_Crop\Abnormal_Cntrst\contrast_"+str(i)+".png", final_img)

这段代码似乎存在多个问题。

  1. i 未定义。
  2. images 列表附加了所有图像,在处理时您正在使用空列表变量 image_list

您可能正在寻找这样的解决方案。

import cv2
import glob        
import numpy as np

input_path = r"<your_input_folder_path>/*.png"

# make sure below folder already exists
out_path = '<your_output_folder_path>/'

image_paths = list(glob.glob(input_path))
for i, img in enumerate(image_paths):
    image = cv2.imread(img)
    image = cv2.resize(image, (500, 600))
    #The initial processing of the image
    #image = cv2.medianBlur(image, 3)
    image_bw = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    #The declaration of CLAHE 
    #clipLimit -> Threshold for contrast limiting
    clahe = cv2.createCLAHE(clipLimit = 5)
    final_img = clahe.apply(image_bw) 

    cv2.imwrite(out_path + f'{str(i)}.png', final_img)