项目

一般

简介

Threshold.py

匿名用户, 2023-08-26 09:42

 
1
# This is a sample Python script.
2

    
3
# Press Shift+F10 to execute it or replace it with your code.
4
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
5
import argparse
6
import os
7
import cv2
8
import numpy as np
9

    
10

    
11
# 通过numpy读取中文路径图
12
def oimage_read_from_chinese_path(ppath,ThresholdType,Threshold,MaxValue):
13
    image_numpy_data = cv2.imdecode(np.fromfile(ppath, dtype=np.uint8), 0)
14

    
15
    if ThresholdType == 1:
16
        ret, thresh1 = cv2.threshold(image_numpy_data, Threshold, MaxValue, cv2.THRESH_BINARY)
17
        return thresh1
18
    elif ThresholdType == 2:
19
        ret, thresh2 = cv2.threshold(image_numpy_data, Threshold, MaxValue, cv2.THRESH_BINARY_INV)
20
        return thresh2
21
    elif ThresholdType == 3:
22
        ret, thresh3 = cv2.threshold(image_numpy_data, Threshold, MaxValue, cv2.THRESH_TRUNC)
23
        return thresh3
24
    elif ThresholdType == 4:
25
        ret, thresh4 = cv2.threshold(image_numpy_data, Threshold, MaxValue, cv2.THRESH_TOZERO)
26
        return thresh4
27
    elif ThresholdType == 5:
28
        ret, thresh5 = cv2.threshold(image_numpy_data, Threshold, MaxValue, cv2.THRESH_TOZERO_INV)
29
        return thresh5
30

    
31

    
32
def print_hi(imgpath,ThresholdType,Threshold,MaxValue):
33
    # Use a breakpoint in the code line below to debug your script.
34
    img_path = oimage_read_from_chinese_path(imgpath,ThresholdType,Threshold,MaxValue)
35

    
36
    cv2.imwrite("OutImage.bmp", img_path)
37

    
38

    
39
# Press the green button in the gutter to run the script.
40
if __name__ == '__main__':
41
    parser = argparse.ArgumentParser()
42
    parser.add_argument("--ImagePath", type=str, required=True)
43
    parser.add_argument("--ThresholdType", type=int, required=True)
44
    parser.add_argument("--Threshold", type=int, required=True)
45
    parser.add_argument("--MaxValue", type=int, default=255)
46
    args = parser.parse_args()
47
    print_hi(args.ImagePath,args.ThresholdType,args.Threshold,args.MaxValue)
48

    
49
# See PyCharm help at https://www.jetbrains.com/help/pycharm/