wangzhibo
2022-03-24 a01eb79d5bd40decaaa4693534de77efdf330bb8
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
 
#将图片转换为模板
__author__ = '上海色宁'
 
import os
import sys
import cv2
import numpy as np
import uuid
import getopt
 
 
def start_check_argv_and_convert(argv):
    """
        通过 getopt模块 来识别参数demo,  http://blog.csdn.net/ouyang_peng/
    """
    input_dir = ""
    dest_dir = ""
    
    try:
        """
            options, args = getopt.getopt(args, shortopts, longopts=[])
 
            参数args:一般是sys.argv[1:]。过滤掉sys.argv[0],它是执行脚本的名字,不算做命令行参数。
            参数shortopts:短格式分析串。例如:"hp:i:",h后面没有冒号,表示后面不带参数;p和i后面带有冒号,表示后面带参数。
            参数longopts:长格式分析串列表。例如:["help", "ip=", "port="],help后面没有等号,表示后面不带参数;ip和port后面带冒号,表示后面带参数。
            返回值options是以元组为元素的列表,每个元组的形式为:(选项串, 附加参数),如:('-i', '192.168.0.1')
            返回值args是个列表,其中的元素是那些不含'-'或'--'的参数。
        """
        opts, args = getopt.getopt(argv, "hi:d:", ["help", "input_dir=","dest_dir="])
    except getopt.GetoptError:
        print('Error: 1_convert_image_into_template.py -i <input_dir> -d <dest_dir>')
        print('   or: 1_convert_image_into_template.py --input_dir=<numbers_image> --dest_dir=<template_image>')
        sys.exit(2)
 
    # 处理 返回值options是以元组为元素的列表。
    if len(opts) != 2 :
        print('Error: Parameters not enough!')
        print('Error: 1_convert_image_into_template.py -i <input_dir> -d <dest_dir>')
        print('   or: 1_convert_image_into_template.py --input_dir=<numbers_image> --dest_dir=<template_image>')
        sys.exit()
    for opt, arg in opts:
        if opt in ("-h", "--h","-help","--help"):
            print('Error: 1_convert_image_into_template.py -i <input_dir> -d <dest_dir>')
            print('   or: 1_convert_image_into_template.py --input_dir=<numbers_image> --dest_dir=<template_image>')
            sys.exit()
        elif opt in ("-i", "--input_dir"):
            input_dir = arg
        elif opt in ("-d", "--dest_dir"):
            dest_dir = arg
    print('input_dir:', input_dir)
    print('dest_dir:', dest_dir)
    
    if not os.path.exists(input_dir):
        print( input_dir+ " is  not exist!!!")
        sys.exit()
    if not os.path.exists(dest_dir):
        print( dest_dir+ " is  not exist, we are going to creat it !!!")
        os.makedirs(dest_dir)
 
    # 打印 返回值args列表,即其中的元素是那些不含'-'或'--'的参数。
    #for i in range(0, len(args)):
    #   print('参数 %s 为:%s' % (i + 1, args[i]))
    
    convert_into_template(input_dir,dest_dir)
    
 
def convert_into_template(input_dir,dest_dir):
        
    # 转换图片为匹配模板
    filename = os.listdir(input_dir)    # 获取文件夹文件
    for file in filename:
        img = cv2.imread(input_dir + "/" + file)    # 读取图片
        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 灰度处理
        # img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        cv2.imshow('GrayThings',img_gray)
        # plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
 
 
        t,dst1 = cv2.threshold(img,255,0,cv2.THRESH_BINARY)
        HSV = cv2.cvtColor(dst1, cv2.COLOR_BGR2HSV)
        H, S, V = cv2.split(HSV)
        LowerYellow = np.array([20, 20, 20])
        UpperYellow = np.array([30, 255, 255])
        mask = cv2.inRange(HSV, LowerYellow, UpperYellow)
        YellowThings = cv2.bitwise_and(dst1, dst1, mask=mask)
        cv2.imshow('YellowThings',YellowThings)
        cv2.imwrite(dest_dir+"/t_"+file, YellowThings)
        
 
#程序入口
if __name__ == '__main__':
 
    # 这个的-i  -d  
    start_check_argv_and_convert(sys.argv[1:])