【问题标题】:Thresholding multiple images in Python using OpenCv使用 OpenCv 在 Python 中对多个图像进行阈值处理
【发布时间】:2020-07-05 10:10:03
【问题描述】:

我想将整个图像文件夹二值化并保存。 我已经找到了将单个图像二值化并将其存储在同一文件夹中的代码:

import cv2
im_gray = cv2.imread('blurredimg1.png', cv2.IMREAD_GRAYSCALE)
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
thresh = 127
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite('bw_image.png', im_bw)

这是输出

The image

Here's the file in the folder

现在,我想一次性使用整个集合的阈值。 我该怎么做?

【问题讨论】:

标签: python opencv machine-learning image-processing image-thresholding


【解决方案1】:

这里是:

from glob import glob import cv2 img_mask = r'C:\Users\Bsi\Desktop\PFE\Mine\*.png' img_names = glob(img_mask) for fn in img_names: print('processing %s...' % fn,) im_gray = cv2.imread(fn, 0) (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) thresh = 127 im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]

【讨论】:

    【解决方案2】:

    以下内容在我的 Mac 上的 Python/OpenCV 中经过测试并适用于 Unix 路径语法。而且我不是 Windows 用户。因此,您需要为您的操作系统适当修改路径并将我的“/”更改为“\”,它们在路径中特别显示

    您需要定义要放置输出目录的路径以保存创建的图像。我使用了 in_dir 和 out_dir。 out_dir 目录需要已经存在。

    所以类似于以下内容。我在循环外获得 OTSU 阈值并从模糊图像中保存阈值。然后我通过你的 img_mask 遍历输入目录中的所有图像。我使用保存的阈值对每个图像进行阈值处理,然后在循环内将文件写入磁盘。

    from glob import glob
    import os
    import cv2
    
    # read your one blurred image and convert to gray
    im_gray = cv2.imread('test/lena.png', 0)
    
    # threshold it with OTSU thresholding and get the threshold value
    thresh, im_bw = cv2.threshold(im_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    print(thresh)
    
    # define the paths to your input images and to where you want to put the output images    
    in_dir = 'test'
    out_dir = 'test2'
    
    # read the input image file names with paths into a list
    infiles = in_dir + '/*.png'
    img_names = glob(infiles)
    print(img_names)
    
    # loop over each input image in a for loop
    for fn in img_names:
        print('processing %s...' % fn)
    
        # read an input image as gray
        im_gray = cv2.imread(fn, 0)
    
        # threshold it with your saved threshold
        im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
    
        # write the result to disk in the previously created output directory
        name = os.path.basename(fn)
        outfile = out_dir + '/' + name
        cv2.imwrite(outfile, im_bw)
    


    【讨论】:

    • 不工作:我在最后一行收到错误:TypeError: Expected Ptr<:umat> for argument 'img'
    • from glob import glob import cv2 img_mask = r'C:\Users\Bsi\Desktop\PFE\Mine*.png' out_dir = r'C:\Users\Bsi\Desktop\PFE\Mine \NEWDIR' img_names = glob(img_mask) for fn in img_names: print('processing %s...' % fn,) im_gray = cv2.imread(fn, 0) thresh=127 im_gray = cv2.threshold(im_gray, thresh , 255, cv2.THRESH_BINARY) cv2.imwrite(r'out_dir\fn', im_gray)
    • 等一下。 output_dir/fn 的格式错误。我正在纠正和测试。
    • 好的!我还在等你的评论
    • 抱歉,拖了我这么久。我不得不调试,因为我不小心在 Window 语法中留下了反斜杠 (),我需要在我的 Mac 上使用正斜杠。我还忘记了您的输入名称包括输入目录路径,我需要删除路径以仅获取图像的名称并将它们写入 out_dir。请参阅我的答案中的更改代码。为您的系统和操作系统更改文件名和路径。
    【解决方案3】:

    您可以使用两个 for 循环来执行此操作。我遇到了同样的问题。

    for i, val in enumerate(images_gray):
        ret,thresh1 = cv2.threshold(images_gray[i],110,255,cv2.THRESH_BINARY)
        ret,thresh2 = cv2.threshold(images_gray[i],70,255,cv2.THRESH_BINARY_INV)
        ret,thresh3 = cv2.threshold(images_gray[i],127,255,cv2.THRESH_TRUNC)
        ret,thresh4 = cv2.threshold(images_gray[i],77,255,cv2.THRESH_TOZERO)
        ret,thresh5 = cv2.threshold(images_gray[i],127,255,cv2.THRESH_TOZERO_INV)
        titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
        images = [images_gray[i], thresh1, thresh2, thresh3, thresh4, thresh5]
        for k in range(6):
            plt.rcParams["figure.figsize"] = (20,10)
            plt.subplot(45,6,k+1),plt.imshow(images_gray[i],'gray', vmin=0,vmax=255)
            plt.title(titles[k])
            k+=1
        plt.xticks([]),plt.yticks([])
        plt.show()
        i+=1
    

    我使用了 45 张图片。并想展示一个 6 阈值的比较。

    【讨论】:

    • import glob import cv2 import numpy as np images= [cv2.imread(file) for file in glob.glob("C:/Users/st/Image/*.jpg")]跨度>
    猜你喜欢
    • 2014-11-30
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多