# 二值化图像
|
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)
|