简体   繁体   English

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

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

Here is the code that i'm trying to modify.这是我要修改的代码。 To read and save all the images at once in one folder, however I got error when I tried to save it一次读取所有图像并将其保存在一个文件夹中,但是当我尝试保存时出现错误


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)

It seems like there are multiple issues with this code.这段代码似乎有多个问题。

  1. i is not defined. i没有定义。
  2. images list has all the images appended and while processing you are making use of the empty list variable image_list images列表附加了所有图像,并且在处理时您正在使用空列表变量image_list

You are probably looking for a solution like this.您可能正在寻找这样的解决方案。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Pykinect和Open CV保存Kinect RGB和深度图像 - Save Kinect RGB and Depth Images using Pykinect and Open CV 如何将使用 matplotlib 和 imshow 生成的多个图像保存在一个文件夹中 - How to save multiple images generated using matplotlib & imshow in a folder 逐个打开图像,绘制矩形,然后保存在另一个文件夹中 - Open images one by one, draw rectangle and then save in another folder 如何在 python 中使用 cv2 从文件夹中导入和显示图像 - how to import and display images from a folder using cv2 in python 如何使用for循环从python中的一个文件夹中读取多个图像? - how to read multiple images from one folder in python using for loop? 使用python逐个打开文件夹中的图像? - Open images from a folder one by one using python? 使用 open cv 读取图像时遇到问题 - Having trouble reading the images using open cv OpenCV cv2.imwrite() 没有循环工作以将图像保存在文件夹中 - OpenCV cv2.imwrite() is not working in loop to save images in folder 打开简历2不会从超过1个文件夹中读取图像? - Open cv 2 wont read images from more then 1 folder? 如何在 python 中使用 selenium 从网站上抓取多个图像并将它们保存在特定文件夹中? - How do I scrape multiple images from a website and save them on a specific folder using selenium in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM