1 import cv2
2 import numpy as np
3 import os
4
5 imgdir=r'D:/XXX/00'#原图片文件夹
6 outdir = r'D:/XXX/11'#输出的文件夹
7
8 def Threshold(imgpath):
9 img=cv2.imread(imgpath)
10 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
11 img255 = np.zeros_like(gray, dtype='uint8')
12 for i in range(gray.shape[0]):
13 for j in range(gray.shape[1]):
14 if gray[i, j] > 190: #自己定
15 img255[i, j] = 255
16 return img255
17
18 filelist=os.listdir(imgdir)
19 for item in filelist:
20 if item.endswith('_predict.png'):#这里网络输出的文件名,格式为'0_predict.png'
21 imgpath = imgdir + os.sep + item
22 #print(imgpath)
23 dst=Threshold(imgpath)
24 outfilepath=os.path.join(outdir, os.path.basename(item))
25 cv2.imwrite(outfilepath, dst)
26
27