【问题标题】:How to save multiple images in one folder by using open cv?如何使用 open cv 将多个图像保存在一个文件夹中?
【发布时间】:2021-05-19 19:01:44
【问题描述】:

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


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)

【问题讨论】:

  • 错误是什么?

标签: python opencv image-preprocessing


【解决方案1】:

此代码似乎存在多个问题。

  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)

【讨论】:

  • 谢谢,我可以运行这个。但是,我仍然无法一次将所有图像保存在我的文件夹中。此代码仅将一张图像保存到我的新文件夹路径中。我必须放 i+1 吗?
  • 并非如此,它应该按原样工作。你能告诉我到底是什么行为吗?也许是一些截图?
猜你喜欢
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
相关资源
最近更新 更多