[英]Thresholding multiple images in Python using OpenCv
I would like to binarize a whole folder of images and save them.我想将整个文件夹的图像二值化并保存。 I've already found a code that binarizes a single image and store it in the same folder:我已经找到了将单个图像二值化并将其存储在同一文件夹中的代码:
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)
Here's the output这是 output
Here's the file in the folder这是文件夹中的文件
Now, I would like to use the threshold on the entire set at once.现在,我想一次对整个集合使用阈值。 How can I do so?我该怎么做?
这里是 :
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]
The following is tested and works in Unix path syntax on my Mac in Python/OpenCV.以下内容在我的 Mac 上的 Python/OpenCV 中以 Unix 路径语法进行测试和工作。 And I am not a Windows user.而且我不是 Windows 用户。 So you will need to modify the paths appropriately for your OS and change my "/" to "\\" where they are specifically shown in the paths因此,您需要为您的操作系统适当地修改路径,并将我的“/”更改为“\\”,它们专门显示在路径中
You need to define the path to where you want to put the output directory to hold the created images.您需要定义要放置输出目录以保存创建的图像的路径。 I used in_dir and out_dir.我使用了 in_dir 和 out_dir。 the out_dir directory needs to exist already. out_dir 目录需要已经存在。
So something like the following.所以像下面这样。 Where I get the OTSU threshold outside the loop and save the threshold value from your blurred image.我在哪里获得循环外的 OTSU 阈值并从模糊图像中保存阈值。 Then I loop over all the images in the input directory via your img_mask.然后我通过你的 img_mask 遍历输入目录中的所有图像。 I threshold each image using the threshold that was saved and then write the file to disk inside the loop.我使用保存的阈值对每个图像进行阈值处理,然后将文件写入循环内的磁盘。
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)
You can use two for loops to do so.您可以使用两个 for 循环来执行此操作。 I was facing the same issue.我遇到了同样的问题。
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
I made use of 45 images.我使用了 45 张图像。 And wanted to show a 6 threshold comparison.并希望显示 6 个阈值比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.