admin
2023-03-29 7aaca048ffc7666b0eccf3d8b7ba212bae3c9e13
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
# 二值化图像
import cv2
 
 
def gray_img(img):
    result = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    return result
 
 
def is_col_empty(img, col, thresh_hold=0):
    for r in img:
        if r[col] > thresh_hold:
            return False
    return True
 
 
def is_col_full(img, col, thresh_hold_start=64, thresh_hold_end=255):
    rows, cols = img.shape
    for r in img:
        if r[col] < thresh_hold_start or r[col] > thresh_hold_end:
            return False
    return True
 
 
# 分离同花顺的代码
def clip_ths_code_area(img):
    img = gray_img(img)
    rows, cols = img.shape
    end_col = cols - 1
    for col in range(0, cols, 1):
        if is_col_full(img, col, 38, 38):
            # print("找到分隔:", col)
            end_col = col - 1
            break
    # 往前找数字分隔
    content_start = -1
    empty_start = -1
    empty_end = -1
    start_index = 0
    end_index = end_col
    for col in range(end_col, 0, -1):
        if not is_col_empty(img, col):
            if content_start < 0:
                content_start = col
            empty_start = -1
            empty_end = -1
        else:
            if empty_start < 0:
                empty_start = col
                empty_end = col
            else:
                empty_end = col
            if empty_start - empty_end > 2 and content_start > 0:
                start_index = col + (empty_start - empty_end + 1)
                end_index = content_start
                # print("代码范围:", start_index, end_index)
                break
    clip_img = img[0:rows, start_index:end_index]
    # cv2.imwrite("test1.png", clip_img)
    return clip_img
 
    # print(clip_img.shape)
    # ret1, p1 = cv2.threshold(src=clip_img, thresh=100, maxval=255, type=cv2.THRESH_BINARY)
    # cv2.imwrite("D:/workspace/GP/trade_desk/test3.png", p1)
 
 
if __name__ == "__main__":
    img = gray_img(cv2.imread("C:\\Users\\Administrator\\Desktop\\test.png"))
    cv2.imwrite("C:\\Users\\Administrator\\Desktop\\test_gray.png", img)