forked from yeungchenwa/FontDiffuser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcut_pics_single.py
118 lines (97 loc) · 4.57 KB
/
cut_pics_single.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import argparse
import os
import cv2
import shutil
import numpy as np
def main(opt):
# # 膨胀腐蚀大小 即将笔画膨胀 避免将偏旁单独分割 根据图片请况自行设置
# rect_size = 8
# # 字体小于该值忽略 20*20
# ignore_min_size = 80
# # 字体大于该值忽略 100*100
# ignore_max_size = 150
# # 图片选取偏移
# offset_param = 48
rect_size = opt.rect_size
ignore_min_size = opt.ignore_min_size
ignore_max_size = opt.ignore_max_size
offset_param = opt.offset_param
# 需要切分的图片 input/input.jpg
input_file = opt.input_path
# 输出文件夹 output
output_path = opt.output_path + os.path.sep
if not os.path.exists(output_path):
os.makedirs(output_path)
else:
shutil.rmtree(output_path)
os.makedirs(output_path)
# img = cv2.imread(input_file)
img = cv2.imdecode(np.fromfile(input_file, dtype=np.uint8), cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化 根据图片情况调节灰度阈值 第二个参数灰度值表示小于该值就将其变为0
ret, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
cv2.imwrite(output_path + "thresh.jpg", thresh)
thresh_rgb = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
# 待切分的图片 img原图 gray灰度图 thresh二值化后的灰度图 thresh_rgb二值化后转RGB
result = thresh.copy()
# 膨胀腐蚀 将字体笔画扩大15像素
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (rect_size, rect_size))
eroded = cv2.erode(thresh, kernel)
cv2.imwrite(output_path + "eroded.jpg", eroded)
# 轮廓检测
contours, hierarchy = cv2.findContours(eroded, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 膨胀间距
spacing = rect_size // 2 - 1
cut_frame_color = (0, 255, 0)
font_frame_color = (255, 0, 0)
token = 1
for c in contours:
x, y, w, h = cv2.boundingRect(c)
if w < ignore_min_size or h < ignore_min_size or w > ignore_max_size or h > ignore_max_size:
continue
font_w = w - spacing - spacing
font_h = h - spacing - spacing
frame_size = font_w if font_w > font_h else font_h
# 正方形切割偏移量
x_offset = int((frame_size - font_w) / 2)
y_offset = int((frame_size - font_h) / 2)
start_x = x + spacing - x_offset - offset_param
# start_y = y + spacing - y_offset - offset_param
start_y = 0
end_x = x + w - spacing + x_offset + offset_param
# end_y = y + h - spacing + y_offset + offset_param
end_y = h
# 字体框线
cv2.rectangle(thresh_rgb, (x + spacing, y + spacing), (x + w - spacing, y + h - spacing), font_frame_color, 1)
# 切割框线
cv2.rectangle(thresh_rgb, (start_x, start_y), (end_x, end_y), cut_frame_color, 1)
# 切割
temp = result[start_y:end_y, start_x:end_x]
path = opt.output_path + os.path.sep + "result" + os.path.sep
if not os.path.exists(path):
os.makedirs(path)
cv2.imwrite(path + str(token) + "_" + str(x) + "_" + str(y) + ".png", temp)
token += 1
break
cv2.imwrite(output_path + "result.jpg", thresh_rgb)
if __name__ == '__main__':
"""单独图片切割
python cut_pics_single.py --input 'input/test/宕.png' --rect_size 15 --ignore_min_size 85 --ignore_max_size 100 --offset_param 1
python cut_pics_single.py --input 'input/test/册.png' --rect_size 15 --ignore_min_size 85 --ignore_max_size 100 --offset_param 1
python cut_pics_single.py --input 'input/test/了.png' --rect_size 15 --ignore_min_size 85 --ignore_max_size 100 --offset_param 1
"""
parser = argparse.ArgumentParser()
parser.add_argument('--input', dest='input_path', default='from/from4.jpg',
help='输入图片路径')
parser.add_argument('--output', dest='output_path', default='./pic',
help='输出的图片位置')
parser.add_argument('--rect_size', dest='rect_size', default=15, type=int,
help='膨胀腐蚀大小')
parser.add_argument('--ignore_min_size', dest='ignore_min_size', default=80, type=int,
help='字体小于该值忽略')
parser.add_argument('--ignore_max_size', dest='ignore_max_size', default=100, type=int,
help='字体大于该值忽略')
parser.add_argument('--offset_param', dest='offset_param', default=1, type=int,
help='图片选取偏移,选取图片扩大范围')
opt = parser.parse_args()
main(opt)