在特定文件夹中写入多个图像文件

Writing multiple image files In specific folder

我有一张图像,稍后我以特定格式阅读了这些图像,我必须找到轮廓并将所有轮廓图像写入指定的输出文件夹。然后我必须在文件名下创建子文件夹,并将分割后的图像写入相关文件夹。到目前为止,我能够读取指定格式的图像,而我能够找到轮廓但无法将所有这些轮廓图像写入特定文件夹。

import cv2
import os, os.path

print (cv2.__version__)

imageDir = "C:/Users/animesh.singh/Downloads/Assignment/" #specify your path here

image_path_list = []

valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] 

#specify your valid extensions here
valid_image_extensions = [item.lower() for item in valid_image_extensions]

for file in os.listdir(imageDir):

extension = os.path.splitext(file)[1]

    if extension.lower() not in valid_image_extensions:
        continue
    image_path_list.append(os.path.join(imageDir, file))

for imagePath in image_path_list:

    img = cv2.imread(imagePath)

    if img is None:
        continue

    cv2.imshow(imagePath, img)

    key = cv2.waitKey(0)
    if key == 27: # escape
        break

cv2.destroyAllWindows()


for imagePath in image_path_list:

    ret, thresh = cv2.threshold(img,0,255,cv2.THRESH_BINARY_INV)
    edges = cv2.Canny(img, 100, 200)

    cv2.imshow(imagePath,thresh)
    cv2.imshow(imagePath,edges)

    key = cv2.waitKey(0)

    cv2.destroyAllWindows()


contours, hierarchy = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:

      cv2.drawContours(img, [contour], 0, (0, 0, 255), 3)

cv2.imwrite('C:/Users/animesh.singh/Downloads/Assignment/contour_images/1 (103)_N_0_0_CARD_Contour_Output.jpg', img)

我要:

• 读取文件夹中的所有图像,它接受除 PDF 以外的所有图像格式。

• 查找轮廓并将所有轮廓图像写入指定的输出文件夹。

• 在filename下创建子文件夹,将分割后的图片写入相关文件夹。

输入图像:

1:

2:

3:

您的代码当前所做的是在图像上绘制一条轮廓线,然后将其保存。它仅对 image_path_list 中的最后一张图像执行此操作,并且由于 imwrite 是硬编码的,因此它始终只会产生 1 张图像。

这是您对每张图片应遵循的流程: - 新建一个文件夹 - 找到图像中的轮廓 - 对于每个轮廓取 boundingRect,包含轮廓的框 - 使用 boundingrect 的尺寸来创建子图像 - 用变量名保存子图像。

下面的代码显示了 1 张图像的这个过程,您可以在 for imagePath in image_path_list:

中实现它

结果:

代码:

    import cv2
    import numpy as np 
    import os
    from os import listdir

    # get the filenames in directory
    basepath = "C:/Users/Desktop/Images/"
    files = listdir(basepath)
    # declare valid filtypes
    valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] 

    for name in files:
            # check if it is a valid filetype
            filename, file_extension = os.path.splitext(name)
            if file_extension in valid_image_extensions:
                    # create a folder for this image
                    path = basepath+filename
                    if not os.path.exists(path):
                            os.makedirs(path)
                    # load image in grayscale
                    img = cv2.imread(basepath+name,0)
                    # threshold image
                    ret,mask = cv2.threshold(img,240,255,cv2.THRESH_BINARY_INV)
                    # find contours
                    ret, contours, hier = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

                    for i in range(len(contours)):
                            # get contour
                            cnt = contours[i]
                            # get the dimensions of the boundingRect
                            x,y,w,h = cv2.boundingRect(cnt)
                            # create a subimage based on boundingRect
                            sub_img = img[y:y+h,x:x+w]
                            # save image of contour with indexed name
                            cv2.imwrite(path+"\contour_"+str(i)+".jpg", sub_img)