简体   繁体   English

从一个文件夹中提取图像,对其进行处理并使用 python、opencv 保存到另一个文件夹中

[英]Extract images from one folder, process it and save into another folder with python, opencv

I am trying to extract image files from one sub folder "input" (10th line) and process it and then save it into another subfolder "output" (10th last line), but the processed files are not being saved.我正在尝试从一个子文件夹“input”(第 10 行)中提取图像文件并对其进行处理,然后将其保存到另一个子文件夹“output”(最后一行第 10 行)中,但未保存已处理的文件。 But if I take all the image files from the same folder where the code is saved (not writing input in the extraction command in 10th line), then when I save it in the subfolder "output", I'm successful.但是,如果我从保存代码的同一文件夹中获取所有图像文件(不在第 10 行的提取命令中写入输入),那么当我将其保存在子文件夹“输出”中时,我就成功了。 Just do remember the note;只要记住笔记; in both cases the processed files are being displayed and are the same, just not being saved in one case.在这两种情况下,处理的文件都被显示并且是相同的,只是在一种情况下没有被保存。

import glob
import cv2
import numpy as np
import matplotlib.pyplot as plt

# initialize data and target as lists
data = []
target = []
# find all bmp files in the current folder
Files = glob.glob("input/*.bmp")

# go through all files and fit contour
for f in Files:
    img = 255-cv2.imread(f,0) #0 for grayscale
    white=cv2.imread("white.bmp")
    # add image to the list
    data.append(img)

    # make lines thicker
    kernel = np.ones((5,5),np.uint8)   
    img = cv2.dilate(img,kernel,iterations = 1)

    # contour application    
    ret,thresh = cv2.threshold(img,127,255,0)
    im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)

    # sort contours by area
    areas = [cv2.contourArea(cnt) for cnt in contours]
    idx = np.argsort(areas)[::-1]
    contours = np.asarray(contours)[idx]

    for cnt in contours:
        (x,y),radius = cv2.minEnclosingCircle(cnt)
        if radius < img.shape[0]-10 and radius > 20:
            cv2.circle(white,(int(x),int(y)),int(radius),0,4)
            break

    plt.imshow(white)
    #save the files to output folder with the name "Image_x"
    filename = "output/Image_%s" %f
    plt.colorbar()
    # live image display
    plt.draw()
    # need to add pause command, otherwise it does not work
    plt.pause(.01)
    # clear the figure to avoid memory issues
    plt.clf()
    #save these contours (outputs) as bmps with same file names
    cv2.imwrite(filename,white)

Let's do a small experiment.让我们做一个小实验。

>>> import os, glob
>>> os.system("tree ./matcher")
matcher
├── matcher2.py
├── matcher.cpp
├── matcher.py
└── question.md

0 directories, 4 files
0
>>> glob.glob("matcher/*.py")
['matcher/matcher2.py', 'matcher/matcher.py']

As you can see, the results of glob.glob("matcher/*.py") contain the root dir "matcher/".如您所见, glob.glob("matcher/*.py")包含根目录“matcher/”。 That so say, it's wrong to write filename = "output/Image_%s" %f .也就是说,写filename = "output/Image_%s" %f Change it to filename = "output/" + f.split("/")[-1] or so.将其更改为filename = "output/" + f.split("/")[-1]左右。

## source bmps in "input/"
## processed bmps to "output"
for f in glob.glob("input/*.bmp"):
    pass
    filename = "output/" + f.split("/")[-1]
    cv2.imwrite(filename, xxx)

It seems that we need to add the matcher2.py and matcher.py at first.看来我们需要先添加matcher2.py和matcher.py。 Otherwise, it has the following error.否则,会出现以下错误。

>>> import os, glob
>>> os.system("tree ./matcher")

sh: 1: tree: not found sh:1:树:未找到

32512 32512

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

相关问题 重命名多个图像并使用python保存到另一个文件夹 - Rename multiple images and save into another folder with python 从文件夹裁剪图像,处理图像,然后保存图像 - Crop images from a folder, process the images, and then save the images 如何创建一个 for 循环以将特定图像从一个文件夹提取到另一个文件夹? - How to create a for loop to extract specific images from a folder to another folder? Python将图像保存到文件夹中 - Python save images into the folder 如何在python中将备用图像从一个文件夹复制到另一个文件夹 - How to copy alternate images from one folder to another in python 如何使用 Python 将图像从一个文件夹复制到另一个文件夹? - How to copy images from one folder to another using Python? Python/Opencv 将多个图像保存到具有不同名称的文件夹中 - Python/Opencv save multiple images to folder with different names 如何将数据集的图像列表保存(写入)到新文件夹-openCV Python? - How to save (write) a list of images from a dataset into a new folder - openCV Python? 如何使用 imwrite 从一个文件夹中获取图像并使用 Opencv 将它们保存到另一个文件夹? - How to use imwrite taking images from one folder and saving them to another using Opencv? 逐个打开图像,绘制矩形,然后保存在另一个文件夹中 - Open images one by one, draw rectangle and then save in another folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM