zhangshiming
2022-03-25 ae7c39f6eb9989784f2d56c77cae944e967d6ef8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: utf-8 -*-
 
#将截取的与图片转换为灰度的图片
 
__author__ = '上海色宁'
 
import os
import sys
import cv2
import numpy as np
import uuid
import getopt
 
def convert_into_gray(input_dir,dest_dir):
        
    # 转换图片为匹配模板
    filename = os.listdir(input_dir)    # 获取文件夹文件
    for file in filename:
        img = cv2.imread(input_dir + "/" + file)    # 读取图片
        img = cv2.cvtColor(cv2.imread(input_dir + "/" + file), cv2.COLOR_BGR2GRAY);
        # 二值化
        #cv2.imshow('gray',img)
        ret, binary = cv2.threshold(img,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
        print("threshold value: %s"%ret)
        cv2.imwrite(dest_dir+"/"+file, binary)
 
        
 
#程序入口
if __name__ == '__main__':
 
    #直接给文件夹名(参数)
    convert_into_gray(sys.argv[1],sys.argv[2])