如何写几个图像而不重叠

问题描述 投票:-1回答:1

我正在尝试写轮廓图像,但我只得到一个图像作为输出。我有3个图像,我想写所有3图像轮廓输出。

我想在filename的名称下创建一个子文件夹,并将分段的图像写入相关的文件夹。

我试图实现os.path.split,但直到现在都没有成功

import sys
import glob,os
import cv2
class Imageprocessing:
    def readImages(inputFolder):
        ext = ['.png', '.jpg', '.gif', '.jpeg', '.tif', '.tiff']    # Add image formats here
        files = []
        path = inputFolder + "\\*.*"
        #print (path)
        files = glob.glob(path)
        #[files.extend(glob.glob(inputFolder + '*.' + e)) for e in ext]
        #print(files)
        imageFiles=[]
        for i in files:
            exten=os.path.splitext(i)[1]
            if exten in ext:
                imageFiles.append(i)
        return imageFiles

    def processImage(imageFiles):
        for imagePath in imageFiles:
            print(imagePath)
            image = cv2.imread(imagePath)
            cv2.imshow("1",image)
            cv2.waitKey()
            ret, thresh = cv2.threshold(image,0,255,cv2.THRESH_BINARY_INV)
            edges = cv2.Canny(thresh, 100, 200)
            #cv2.imshow('original_image',image)
            #cv2.imshow('2',thresh)
            #cv2.imshow('3',edges)
            #cv2.waitKey()
            #cv2.destroyAllWindows()
            contours,hierachy = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            for contour in contours:
                cv2.drawContours(image, [contour], -1, (0, 0, 255), 3)
            cv2.imwrite(sys.argv[1]+".jpg", image)

imageFiles = Imageprocessing.readImages(sys.argv[1])
Imageprocessing.processImage(imageFiles)

1:

1

2:

2

3:

3

python opencv image-processing split image-segmentation
1个回答
0
投票

如您所知,以下代码将“以文件名名称创建子文件夹并将分段图像写入相关文件夹”。

import cv2
import numpy as np
import glob,os,sys
class Imageprocessing:  
    def readImages(inputFolder):
        ext = ['.png', '.jpg', '.gif', '.jpeg', '.tif', '.tiff']
        files = []
        path = inputFolder + "/*.*"
        files = glob.glob(path)
        imageFiles=[]
        for i in files:
                exten=os.path.splitext(i)[1]
                if exten in ext:
                        imageFiles.append(i)
        return imageFiles

    def processImage(imageFiles):
        for imagePath in imageFiles:
            img_name = os.path.splitext(os.path.basename(imagePath))[0]
            new_folder = sys.argv[2]+'/'+img_name+'/'
            os.makedirs(new_folder, exist_ok=True)

            image = cv2.imread(imagePath)
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
            kernel = np.ones((10, 0), np.uint8)
            dilated_img = cv2.dilate(thresh, kernel, iterations=0)
            img, contours, hierarchy = cv2.findContours(dilated_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            sorted_ctrs = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0])

            for i, ctr in enumerate(sorted_ctrs):
                    x, y, w, h = cv2.boundingRect(ctr)
                    cv2.drawContours(image, [ctr], -1, (0, 0, 255), 3)
                    roi = image[y:y + h, x:x + w]
                    if w > 3 and h > 10:
                            cv2.imwrite(new_folder+'{}.png'.format(i), roi)

            cv2.imshow('contours', image)
            cv2.waitKey(0)

imageFiles = Imageprocessing.readImages(sys.argv[1])
Imageprocessing.processImage(imageFiles)
# press 'esc' to close the image window
# execute as : python file.py path\to\input path\to\output 
# each argument seperated by space

这将为指定为sys.argv[2]的位置处的每个文件名创建一个文件夹,作为带有轮廓的分段图像。

enter image description here enter image description here

您可以调整if w > 3 and h > 10:或内核中的'w'和'h',或'cv2.dilate'中迭代的值,以将此代码调整为图像文件中不同大小的字符。 (目前代码已调整为您提供的图像链接)

© www.soinside.com 2019 - 2024. All rights reserved.