import cv2
def image_binarization(filepath,destpath):
pathDir = os.listdir(filepath)
for allDir in pathDir:
child = os.path.join(filepath,allDir)
dest = os.path.join(destpath,allDir)
if os.path.isfile(child):
image = cv2.imread(child)
n,s = os.path.splitext(dest)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
retval, dst = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY) #dst二值化图像
cv2.imwrite(dest,dst)
print(destpath)
print(allDir)
if __name__ =="__main__":
filepath = r'F:\lab' #需要二值化的图片
destpath = r'F:\lab_1' #二值化后的图片
image_binarization(filepath,destpath)
批量二值化
最新推荐文章于 2024-04-30 19:24:04 发布