pyxllib 0.3.197__py3-none-any.whl → 3.201.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyxllib/__init__.py +14 -21
- pyxllib/algo/__init__.py +8 -8
- pyxllib/algo/disjoint.py +54 -54
- pyxllib/algo/geo.py +537 -541
- pyxllib/algo/intervals.py +964 -964
- pyxllib/algo/matcher.py +389 -389
- pyxllib/algo/newbie.py +166 -166
- pyxllib/algo/pupil.py +629 -629
- pyxllib/algo/shapelylib.py +67 -67
- pyxllib/algo/specialist.py +241 -241
- pyxllib/algo/stat.py +494 -494
- pyxllib/algo/treelib.py +145 -149
- pyxllib/algo/unitlib.py +62 -66
- pyxllib/autogui/__init__.py +5 -5
- pyxllib/autogui/activewin.py +246 -246
- pyxllib/autogui/all.py +9 -9
- pyxllib/autogui/autogui.py +846 -852
- pyxllib/autogui/uiautolib.py +362 -362
- pyxllib/autogui/virtualkey.py +102 -102
- pyxllib/autogui/wechat.py +827 -827
- pyxllib/autogui/wechat_msg.py +421 -421
- pyxllib/autogui/wxautolib.py +84 -84
- pyxllib/cv/__init__.py +5 -5
- pyxllib/cv/expert.py +267 -267
- pyxllib/cv/imfile.py +159 -159
- pyxllib/cv/imhash.py +39 -39
- pyxllib/cv/pupil.py +9 -9
- pyxllib/cv/rgbfmt.py +1525 -1525
- pyxllib/cv/slidercaptcha.py +137 -137
- pyxllib/cv/trackbartools.py +251 -251
- pyxllib/cv/xlcvlib.py +1040 -1040
- pyxllib/cv/xlpillib.py +423 -423
- pyxllib/data/echarts.py +236 -240
- pyxllib/data/jsonlib.py +85 -89
- pyxllib/data/oss.py +72 -72
- pyxllib/data/pglib.py +1111 -1127
- pyxllib/data/sqlite.py +568 -568
- pyxllib/data/sqllib.py +297 -297
- pyxllib/ext/JLineViewer.py +505 -505
- pyxllib/ext/__init__.py +6 -6
- pyxllib/ext/demolib.py +251 -246
- pyxllib/ext/drissionlib.py +277 -277
- pyxllib/ext/kq5034lib.py +12 -12
- pyxllib/ext/qt.py +449 -449
- pyxllib/ext/robustprocfile.py +493 -497
- pyxllib/ext/seleniumlib.py +76 -76
- pyxllib/ext/tk.py +173 -173
- pyxllib/ext/unixlib.py +821 -827
- pyxllib/ext/utools.py +345 -351
- pyxllib/ext/webhook.py +124 -119
- pyxllib/ext/win32lib.py +40 -40
- pyxllib/ext/wjxlib.py +91 -88
- pyxllib/ext/wpsapi.py +124 -124
- pyxllib/ext/xlwork.py +9 -9
- pyxllib/ext/yuquelib.py +1110 -1105
- pyxllib/file/__init__.py +17 -17
- pyxllib/file/docxlib.py +757 -761
- pyxllib/file/gitlib.py +309 -309
- pyxllib/file/libreoffice.py +165 -165
- pyxllib/file/movielib.py +144 -148
- pyxllib/file/newbie.py +10 -10
- pyxllib/file/onenotelib.py +1469 -1469
- pyxllib/file/packlib/__init__.py +330 -330
- pyxllib/file/packlib/zipfile.py +2441 -2441
- pyxllib/file/pdflib.py +422 -426
- pyxllib/file/pupil.py +185 -185
- pyxllib/file/specialist/__init__.py +681 -685
- pyxllib/file/specialist/dirlib.py +799 -799
- pyxllib/file/specialist/download.py +193 -193
- pyxllib/file/specialist/filelib.py +2825 -2829
- pyxllib/file/xlsxlib.py +3122 -3131
- pyxllib/file/xlsyncfile.py +341 -341
- pyxllib/prog/__init__.py +5 -5
- pyxllib/prog/cachetools.py +58 -64
- pyxllib/prog/deprecatedlib.py +233 -233
- pyxllib/prog/filelock.py +42 -42
- pyxllib/prog/ipyexec.py +253 -253
- pyxllib/prog/multiprogs.py +940 -940
- pyxllib/prog/newbie.py +451 -451
- pyxllib/prog/pupil.py +1208 -1197
- pyxllib/prog/sitepackages.py +33 -33
- pyxllib/prog/specialist/__init__.py +348 -391
- pyxllib/prog/specialist/bc.py +203 -203
- pyxllib/prog/specialist/browser.py +497 -497
- pyxllib/prog/specialist/common.py +347 -347
- pyxllib/prog/specialist/datetime.py +198 -198
- pyxllib/prog/specialist/tictoc.py +240 -240
- pyxllib/prog/specialist/xllog.py +180 -180
- pyxllib/prog/xlosenv.py +110 -108
- pyxllib/stdlib/__init__.py +17 -17
- pyxllib/stdlib/tablepyxl/__init__.py +10 -10
- pyxllib/stdlib/tablepyxl/style.py +303 -303
- pyxllib/stdlib/tablepyxl/tablepyxl.py +130 -130
- pyxllib/text/__init__.py +8 -8
- pyxllib/text/ahocorasick.py +36 -39
- pyxllib/text/airscript.js +754 -744
- pyxllib/text/charclasslib.py +121 -121
- pyxllib/text/jiebalib.py +267 -267
- pyxllib/text/jinjalib.py +27 -32
- pyxllib/text/jsa_ai_prompt.md +271 -271
- pyxllib/text/jscode.py +922 -922
- pyxllib/text/latex/__init__.py +158 -158
- pyxllib/text/levenshtein.py +303 -303
- pyxllib/text/nestenv.py +1215 -1215
- pyxllib/text/newbie.py +300 -300
- pyxllib/text/pupil/__init__.py +8 -8
- pyxllib/text/pupil/common.py +1121 -1121
- pyxllib/text/pupil/xlalign.py +326 -326
- pyxllib/text/pycode.py +47 -47
- pyxllib/text/specialist/__init__.py +8 -8
- pyxllib/text/specialist/common.py +112 -112
- pyxllib/text/specialist/ptag.py +186 -186
- pyxllib/text/spellchecker.py +172 -172
- pyxllib/text/templates/echart_base.html +10 -10
- pyxllib/text/templates/highlight_code.html +16 -16
- pyxllib/text/templates/latex_editor.html +102 -102
- pyxllib/text/vbacode.py +17 -17
- pyxllib/text/xmllib.py +741 -747
- pyxllib/xl.py +42 -39
- pyxllib/xlcv.py +17 -17
- pyxllib-3.201.1.dist-info/METADATA +296 -0
- pyxllib-3.201.1.dist-info/RECORD +125 -0
- {pyxllib-0.3.197.dist-info → pyxllib-3.201.1.dist-info}/licenses/LICENSE +190 -190
- pyxllib/ext/old.py +0 -663
- pyxllib-0.3.197.dist-info/METADATA +0 -48
- pyxllib-0.3.197.dist-info/RECORD +0 -126
- {pyxllib-0.3.197.dist-info → pyxllib-3.201.1.dist-info}/WHEEL +0 -0
pyxllib/cv/trackbartools.py
CHANGED
@@ -1,251 +1,251 @@
|
|
1
|
-
#!/usr/bin/env python3
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
# @Author : 陈坤泽
|
4
|
-
# @Email : 877362867@qq.com
|
5
|
-
# @Date : 2020/09/30 10:45
|
6
|
-
|
7
|
-
"""
|
8
|
-
这个文件默认不加载到cv里,需要使用的时候再导入
|
9
|
-
|
10
|
-
from pyxlib.cv.debugtools import *
|
11
|
-
"""
|
12
|
-
|
13
|
-
import math
|
14
|
-
import time
|
15
|
-
|
16
|
-
import cv2
|
17
|
-
import numpy as np
|
18
|
-
|
19
|
-
from pyxllib.prog.specialist import TicToc
|
20
|
-
from pyxllib.cv.expert import xlcv
|
21
|
-
|
22
|
-
|
23
|
-
class TrackbarTool:
|
24
|
-
""" 滑动条控件组
|
25
|
-
"""
|
26
|
-
|
27
|
-
def __init__(self, winname=None, *, imgproc=None, img=None, flags=1, verbose=0, delay=100):
|
28
|
-
"""
|
29
|
-
Args:
|
30
|
-
winname: 窗口名可以不输入,可以从func获取,或者有默认窗口名
|
31
|
-
imgproc: 有时候明确要对指定的函数进行可视化分析
|
32
|
-
img: 图片可以不输入,可以内置一个图片
|
33
|
-
flags: 0 窗口可以调节大小,1 窗口按照图片大小固定
|
34
|
-
verbose: 0 静默模式,1 显示每次运行时间,2 显示每次运行时间及参数值
|
35
|
-
delay: 有时候改动滑动条,并不想立即生效,可以设置延时,这里单位是ms
|
36
|
-
即如果delay时间段内已经有执行过回调函数,会避免重复执行
|
37
|
-
"""
|
38
|
-
|
39
|
-
if winname is None:
|
40
|
-
if imgproc:
|
41
|
-
winname = imgproc.__name__
|
42
|
-
else:
|
43
|
-
winname = 'TrackbarTool'
|
44
|
-
|
45
|
-
if img is None:
|
46
|
-
img = np.zeros([500, 100, 3], dtype='uint8')
|
47
|
-
else:
|
48
|
-
img = xlcv.read(img)
|
49
|
-
|
50
|
-
self.winname = winname
|
51
|
-
self.img = img
|
52
|
-
cv2.namedWindow(winname, flags)
|
53
|
-
cv2.imshow(winname, self.img)
|
54
|
-
|
55
|
-
self.imgproc = imgproc
|
56
|
-
self.trackbar_names = {}
|
57
|
-
|
58
|
-
self.verbose = verbose
|
59
|
-
|
60
|
-
self.delay = delay
|
61
|
-
self.last_run_time = 0
|
62
|
-
|
63
|
-
@classmethod
|
64
|
-
def from_album(cls, obj, **kwargs):
|
65
|
-
""" albumentations的类功能 """
|
66
|
-
|
67
|
-
def imgproc(img, **kws):
|
68
|
-
f = obj(**kws, p=1) # p是概率,这里做可视化,是百分百执行
|
69
|
-
return f(image=img)['image']
|
70
|
-
|
71
|
-
return cls(**kwargs, imgproc=imgproc, winname=obj.__name__)
|
72
|
-
|
73
|
-
def imshow(self, img=None):
|
74
|
-
""" 刷新显示的图片 """
|
75
|
-
if img is None:
|
76
|
-
img = self.img
|
77
|
-
cv2.imshow(self.winname, img)
|
78
|
-
|
79
|
-
def create_trackbar(self, name, count, value=0, *, vmaps=None, on_change=None):
|
80
|
-
""" 创建一个滑动条
|
81
|
-
|
82
|
-
:param name: 滑动条名称
|
83
|
-
:param int count: 上限值
|
84
|
-
:param int value: 初始值
|
85
|
-
:param list[func(int)] vmaps: 滑动条只能设置整数值,但有些参数不一定是整数,可以写个自定义的映射函数
|
86
|
-
:param on_change: 自定义,额外扩展的回调函数
|
87
|
-
:return:
|
88
|
-
"""
|
89
|
-
if on_change is None:
|
90
|
-
on_change = self._on_change
|
91
|
-
self.trackbar_names[name] = {'count': count, 'value': value, 'vmaps': vmaps}
|
92
|
-
cv2.createTrackbar(name, self.winname, value, count, on_change)
|
93
|
-
|
94
|
-
def default_run(self, **kwargs):
|
95
|
-
""" 默认执行器,这个在类继承后,基本都是要自定义成自己的功能的
|
96
|
-
|
97
|
-
kwargs里存储了当前所有滑动条的取值
|
98
|
-
如果有设置vmap,
|
99
|
-
"""
|
100
|
-
pass
|
101
|
-
|
102
|
-
def _on_change(self, x):
|
103
|
-
""" 默认的回调函数,x是对应滑动条当前的取值
|
104
|
-
|
105
|
-
如果需要对每个特定滑动条做特殊定制操作,可以create_trackbar传入特殊on_change
|
106
|
-
这里的_on_change是所有滑动条统一回调版本,不区分是哪个滑动条改动了
|
107
|
-
"""
|
108
|
-
# 1 记录运行时间
|
109
|
-
cur_time = time.time() * 1000
|
110
|
-
if cur_time - self.last_run_time < self.delay:
|
111
|
-
return
|
112
|
-
else:
|
113
|
-
self.last_run_time = cur_time
|
114
|
-
|
115
|
-
# 2 生成、解析滑动条的参数值
|
116
|
-
kwargs = {}
|
117
|
-
for k, v in self.trackbar_names.items():
|
118
|
-
x = cv2.getTrackbarPos(k, self.winname)
|
119
|
-
if v['vmaps']:
|
120
|
-
for kk, func in v['vmaps'].items():
|
121
|
-
kwargs[kk] = func(x)
|
122
|
-
else:
|
123
|
-
kwargs[k] = x
|
124
|
-
|
125
|
-
# 3 执行绑定的功能
|
126
|
-
tt = TicToc()
|
127
|
-
if self.imgproc:
|
128
|
-
try:
|
129
|
-
dst = self.imgproc(self.img, **kwargs)
|
130
|
-
self.imshow(dst)
|
131
|
-
except Exception as e:
|
132
|
-
if 'missing required argument' in e.args[0]:
|
133
|
-
pass
|
134
|
-
else:
|
135
|
-
raise e
|
136
|
-
else:
|
137
|
-
self.default_run(**kwargs)
|
138
|
-
|
139
|
-
# 4 是否显示运行信息
|
140
|
-
if self.verbose == 1:
|
141
|
-
tt.toc()
|
142
|
-
elif self.verbose == 2:
|
143
|
-
tt.toc(kwargs)
|
144
|
-
|
145
|
-
def __getitem__(self, item):
|
146
|
-
""" 可以通过 Trackbars 来获取滑动条当前值
|
147
|
-
|
148
|
-
:param item: 滑动条名称
|
149
|
-
扩展:也支持获取vmaps后对应的名称及值
|
150
|
-
:return: 当前取值
|
151
|
-
"""
|
152
|
-
if item in self.trackbar_names:
|
153
|
-
return cv2.getTrackbarPos(item, self.winname)
|
154
|
-
else:
|
155
|
-
for k, v in self.trackbar_names.items():
|
156
|
-
if v['vmaps'] and item in v['vmaps'].keys():
|
157
|
-
x = cv2.getTrackbarPos(k, self.winname)
|
158
|
-
return v['vmaps']['item'](x)
|
159
|
-
|
160
|
-
|
161
|
-
class ErodeTool(TrackbarTool):
|
162
|
-
r""" 腐蚀 erode
|
163
|
-
|
164
|
-
HoughLinesPTool('handwriting/C100001448573-002.png')
|
165
|
-
"""
|
166
|
-
|
167
|
-
def __init__(self, img, winname='ErodeTool', flags=1, *, verbose=2):
|
168
|
-
super().__init__(winname, img=img, flags=flags, verbose=verbose)
|
169
|
-
self.create_trackbar('size_x', 100, 15)
|
170
|
-
self.create_trackbar('size_y', 100, 15)
|
171
|
-
|
172
|
-
def default_run(self, **kwargs):
|
173
|
-
""" 默认执行器
|
174
|
-
"""
|
175
|
-
size = kwargs['size_x'], kwargs['size_y']
|
176
|
-
element = cv2.getStructuringElement(cv2.MORPH_RECT, size)
|
177
|
-
dst = cv2.erode(self.img, element)
|
178
|
-
self.imshow(dst)
|
179
|
-
|
180
|
-
|
181
|
-
class HoughLinesPTool(TrackbarTool):
|
182
|
-
r""" 霍夫线段检测
|
183
|
-
|
184
|
-
HoughLinesPTool('handwriting/C100001448573-002.png')
|
185
|
-
"""
|
186
|
-
|
187
|
-
def __init__(self, img, winname='HoughLinesPTool', flags=1, *, verbose=2):
|
188
|
-
super().__init__(winname, img=img, flags=flags, verbose=verbose)
|
189
|
-
|
190
|
-
# 1 增加控件
|
191
|
-
# 以像素为单位的距离精度
|
192
|
-
self.create_trackbar('rho*10', 100, 10, vmaps={'rho': lambda x: x / 10})
|
193
|
-
# 以弧度为单位的角度精度
|
194
|
-
self.create_trackbar('theta*pi/360', 10, 2, vmaps={'theta': lambda x: x * math.pi / 360})
|
195
|
-
# 累加平面的阈值参数
|
196
|
-
self.create_trackbar('threshold', 200, 80)
|
197
|
-
# 最低线段长度
|
198
|
-
self.create_trackbar('minLineLength', 200, 50) # 如果要显示全,这里名称最好只有10个字符,也可能是跟图片尺寸有关,有待进一步观察
|
199
|
-
# 允许将同一行点与点之间连接起来的最大的距离
|
200
|
-
self.create_trackbar('maxLineGap', 100, 10)
|
201
|
-
|
202
|
-
# 2 图片预处理,这里暂时用自适应二值化,后面可以把该部分也变成可调试项
|
203
|
-
if self.img.ndim == 3:
|
204
|
-
gray_img = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
|
205
|
-
else:
|
206
|
-
gray_img = self.img
|
207
|
-
self.binary_img = cv2.adaptiveThreshold(gray_img, 255, 0, 1, 11, 3)
|
208
|
-
|
209
|
-
def default_run(self, **kwargs):
|
210
|
-
""" 默认执行器
|
211
|
-
"""
|
212
|
-
lines = cv2.HoughLinesP(self.binary_img, kwargs['rho'], kwargs['theta'],
|
213
|
-
kwargs['threshold'], kwargs['minLineLength'], kwargs['maxLineGap'])
|
214
|
-
if lines is None: lines = np.array([])
|
215
|
-
# 处理用二值化,显示用原图
|
216
|
-
self.imshow(xlcv.lines(self.img, lines))
|
217
|
-
|
218
|
-
|
219
|
-
def test_adaptiveThreshold(img=None):
|
220
|
-
""" 滑动条工具使用方法的示例 """
|
221
|
-
# 1 图片可以传入路径,这里做一个随机图片
|
222
|
-
if img is None:
|
223
|
-
img = np.random.randint(0, 255, [500, 200], dtype='uint8')
|
224
|
-
else:
|
225
|
-
img = xlcv.read(img, 0) # 确保是单通道灰度图
|
226
|
-
|
227
|
-
# 2 指定要分析的函数
|
228
|
-
t = TrackbarTool(imgproc=cv2.adaptiveThreshold, img=img, verbose=2, delay=100)
|
229
|
-
t.create_trackbar('maxValue', 255, 255) # 添加滑动条:名称,最大值,默认值
|
230
|
-
# 创建滑动条过程中,可能会报错,因为参数还不全,指定的imgproc还无法运行,可以先不用管
|
231
|
-
t.create_trackbar('adaptiveMethod', 10, 0)
|
232
|
-
t.create_trackbar('thresholdType', 10, 1)
|
233
|
-
t.create_trackbar('blockSize', 100, 11)
|
234
|
-
t.create_trackbar('C', 20, 3)
|
235
|
-
# t.imshow(img2) # 临时显式其他图片处理效果。如果需要持久修改,可以更改self.img的值
|
236
|
-
cv2.waitKey()
|
237
|
-
|
238
|
-
|
239
|
-
def test_CoarseDropout(img=None):
|
240
|
-
""" 做albumentations库里类功能的示例写法 """
|
241
|
-
import albumentations as A
|
242
|
-
|
243
|
-
if img is None:
|
244
|
-
img = np.random.randint(0, 255, [500, 200], dtype='uint8')
|
245
|
-
|
246
|
-
t = TrackbarTool.from_album(A.CoarseDropout, img=img, verbose=2)
|
247
|
-
t.create_trackbar('max_holes', 50, 8)
|
248
|
-
t.create_trackbar('max_height', 50, 8)
|
249
|
-
t.create_trackbar('max_width', 50, 8)
|
250
|
-
t.create_trackbar('fill_value', 255, 0)
|
251
|
-
cv2.waitKey()
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# @Author : 陈坤泽
|
4
|
+
# @Email : 877362867@qq.com
|
5
|
+
# @Date : 2020/09/30 10:45
|
6
|
+
|
7
|
+
"""
|
8
|
+
这个文件默认不加载到cv里,需要使用的时候再导入
|
9
|
+
|
10
|
+
from pyxlib.cv.debugtools import *
|
11
|
+
"""
|
12
|
+
|
13
|
+
import math
|
14
|
+
import time
|
15
|
+
|
16
|
+
import cv2
|
17
|
+
import numpy as np
|
18
|
+
|
19
|
+
from pyxllib.prog.specialist import TicToc
|
20
|
+
from pyxllib.cv.expert import xlcv
|
21
|
+
|
22
|
+
|
23
|
+
class TrackbarTool:
|
24
|
+
""" 滑动条控件组
|
25
|
+
"""
|
26
|
+
|
27
|
+
def __init__(self, winname=None, *, imgproc=None, img=None, flags=1, verbose=0, delay=100):
|
28
|
+
"""
|
29
|
+
Args:
|
30
|
+
winname: 窗口名可以不输入,可以从func获取,或者有默认窗口名
|
31
|
+
imgproc: 有时候明确要对指定的函数进行可视化分析
|
32
|
+
img: 图片可以不输入,可以内置一个图片
|
33
|
+
flags: 0 窗口可以调节大小,1 窗口按照图片大小固定
|
34
|
+
verbose: 0 静默模式,1 显示每次运行时间,2 显示每次运行时间及参数值
|
35
|
+
delay: 有时候改动滑动条,并不想立即生效,可以设置延时,这里单位是ms
|
36
|
+
即如果delay时间段内已经有执行过回调函数,会避免重复执行
|
37
|
+
"""
|
38
|
+
|
39
|
+
if winname is None:
|
40
|
+
if imgproc:
|
41
|
+
winname = imgproc.__name__
|
42
|
+
else:
|
43
|
+
winname = 'TrackbarTool'
|
44
|
+
|
45
|
+
if img is None:
|
46
|
+
img = np.zeros([500, 100, 3], dtype='uint8')
|
47
|
+
else:
|
48
|
+
img = xlcv.read(img)
|
49
|
+
|
50
|
+
self.winname = winname
|
51
|
+
self.img = img
|
52
|
+
cv2.namedWindow(winname, flags)
|
53
|
+
cv2.imshow(winname, self.img)
|
54
|
+
|
55
|
+
self.imgproc = imgproc
|
56
|
+
self.trackbar_names = {}
|
57
|
+
|
58
|
+
self.verbose = verbose
|
59
|
+
|
60
|
+
self.delay = delay
|
61
|
+
self.last_run_time = 0
|
62
|
+
|
63
|
+
@classmethod
|
64
|
+
def from_album(cls, obj, **kwargs):
|
65
|
+
""" albumentations的类功能 """
|
66
|
+
|
67
|
+
def imgproc(img, **kws):
|
68
|
+
f = obj(**kws, p=1) # p是概率,这里做可视化,是百分百执行
|
69
|
+
return f(image=img)['image']
|
70
|
+
|
71
|
+
return cls(**kwargs, imgproc=imgproc, winname=obj.__name__)
|
72
|
+
|
73
|
+
def imshow(self, img=None):
|
74
|
+
""" 刷新显示的图片 """
|
75
|
+
if img is None:
|
76
|
+
img = self.img
|
77
|
+
cv2.imshow(self.winname, img)
|
78
|
+
|
79
|
+
def create_trackbar(self, name, count, value=0, *, vmaps=None, on_change=None):
|
80
|
+
""" 创建一个滑动条
|
81
|
+
|
82
|
+
:param name: 滑动条名称
|
83
|
+
:param int count: 上限值
|
84
|
+
:param int value: 初始值
|
85
|
+
:param list[func(int)] vmaps: 滑动条只能设置整数值,但有些参数不一定是整数,可以写个自定义的映射函数
|
86
|
+
:param on_change: 自定义,额外扩展的回调函数
|
87
|
+
:return:
|
88
|
+
"""
|
89
|
+
if on_change is None:
|
90
|
+
on_change = self._on_change
|
91
|
+
self.trackbar_names[name] = {'count': count, 'value': value, 'vmaps': vmaps}
|
92
|
+
cv2.createTrackbar(name, self.winname, value, count, on_change)
|
93
|
+
|
94
|
+
def default_run(self, **kwargs):
|
95
|
+
""" 默认执行器,这个在类继承后,基本都是要自定义成自己的功能的
|
96
|
+
|
97
|
+
kwargs里存储了当前所有滑动条的取值
|
98
|
+
如果有设置vmap,
|
99
|
+
"""
|
100
|
+
pass
|
101
|
+
|
102
|
+
def _on_change(self, x):
|
103
|
+
""" 默认的回调函数,x是对应滑动条当前的取值
|
104
|
+
|
105
|
+
如果需要对每个特定滑动条做特殊定制操作,可以create_trackbar传入特殊on_change
|
106
|
+
这里的_on_change是所有滑动条统一回调版本,不区分是哪个滑动条改动了
|
107
|
+
"""
|
108
|
+
# 1 记录运行时间
|
109
|
+
cur_time = time.time() * 1000
|
110
|
+
if cur_time - self.last_run_time < self.delay:
|
111
|
+
return
|
112
|
+
else:
|
113
|
+
self.last_run_time = cur_time
|
114
|
+
|
115
|
+
# 2 生成、解析滑动条的参数值
|
116
|
+
kwargs = {}
|
117
|
+
for k, v in self.trackbar_names.items():
|
118
|
+
x = cv2.getTrackbarPos(k, self.winname)
|
119
|
+
if v['vmaps']:
|
120
|
+
for kk, func in v['vmaps'].items():
|
121
|
+
kwargs[kk] = func(x)
|
122
|
+
else:
|
123
|
+
kwargs[k] = x
|
124
|
+
|
125
|
+
# 3 执行绑定的功能
|
126
|
+
tt = TicToc()
|
127
|
+
if self.imgproc:
|
128
|
+
try:
|
129
|
+
dst = self.imgproc(self.img, **kwargs)
|
130
|
+
self.imshow(dst)
|
131
|
+
except Exception as e:
|
132
|
+
if 'missing required argument' in e.args[0]:
|
133
|
+
pass
|
134
|
+
else:
|
135
|
+
raise e
|
136
|
+
else:
|
137
|
+
self.default_run(**kwargs)
|
138
|
+
|
139
|
+
# 4 是否显示运行信息
|
140
|
+
if self.verbose == 1:
|
141
|
+
tt.toc()
|
142
|
+
elif self.verbose == 2:
|
143
|
+
tt.toc(kwargs)
|
144
|
+
|
145
|
+
def __getitem__(self, item):
|
146
|
+
""" 可以通过 Trackbars 来获取滑动条当前值
|
147
|
+
|
148
|
+
:param item: 滑动条名称
|
149
|
+
扩展:也支持获取vmaps后对应的名称及值
|
150
|
+
:return: 当前取值
|
151
|
+
"""
|
152
|
+
if item in self.trackbar_names:
|
153
|
+
return cv2.getTrackbarPos(item, self.winname)
|
154
|
+
else:
|
155
|
+
for k, v in self.trackbar_names.items():
|
156
|
+
if v['vmaps'] and item in v['vmaps'].keys():
|
157
|
+
x = cv2.getTrackbarPos(k, self.winname)
|
158
|
+
return v['vmaps']['item'](x)
|
159
|
+
|
160
|
+
|
161
|
+
class ErodeTool(TrackbarTool):
|
162
|
+
r""" 腐蚀 erode
|
163
|
+
|
164
|
+
HoughLinesPTool('handwriting/C100001448573-002.png')
|
165
|
+
"""
|
166
|
+
|
167
|
+
def __init__(self, img, winname='ErodeTool', flags=1, *, verbose=2):
|
168
|
+
super().__init__(winname, img=img, flags=flags, verbose=verbose)
|
169
|
+
self.create_trackbar('size_x', 100, 15)
|
170
|
+
self.create_trackbar('size_y', 100, 15)
|
171
|
+
|
172
|
+
def default_run(self, **kwargs):
|
173
|
+
""" 默认执行器
|
174
|
+
"""
|
175
|
+
size = kwargs['size_x'], kwargs['size_y']
|
176
|
+
element = cv2.getStructuringElement(cv2.MORPH_RECT, size)
|
177
|
+
dst = cv2.erode(self.img, element)
|
178
|
+
self.imshow(dst)
|
179
|
+
|
180
|
+
|
181
|
+
class HoughLinesPTool(TrackbarTool):
|
182
|
+
r""" 霍夫线段检测
|
183
|
+
|
184
|
+
HoughLinesPTool('handwriting/C100001448573-002.png')
|
185
|
+
"""
|
186
|
+
|
187
|
+
def __init__(self, img, winname='HoughLinesPTool', flags=1, *, verbose=2):
|
188
|
+
super().__init__(winname, img=img, flags=flags, verbose=verbose)
|
189
|
+
|
190
|
+
# 1 增加控件
|
191
|
+
# 以像素为单位的距离精度
|
192
|
+
self.create_trackbar('rho*10', 100, 10, vmaps={'rho': lambda x: x / 10})
|
193
|
+
# 以弧度为单位的角度精度
|
194
|
+
self.create_trackbar('theta*pi/360', 10, 2, vmaps={'theta': lambda x: x * math.pi / 360})
|
195
|
+
# 累加平面的阈值参数
|
196
|
+
self.create_trackbar('threshold', 200, 80)
|
197
|
+
# 最低线段长度
|
198
|
+
self.create_trackbar('minLineLength', 200, 50) # 如果要显示全,这里名称最好只有10个字符,也可能是跟图片尺寸有关,有待进一步观察
|
199
|
+
# 允许将同一行点与点之间连接起来的最大的距离
|
200
|
+
self.create_trackbar('maxLineGap', 100, 10)
|
201
|
+
|
202
|
+
# 2 图片预处理,这里暂时用自适应二值化,后面可以把该部分也变成可调试项
|
203
|
+
if self.img.ndim == 3:
|
204
|
+
gray_img = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
|
205
|
+
else:
|
206
|
+
gray_img = self.img
|
207
|
+
self.binary_img = cv2.adaptiveThreshold(gray_img, 255, 0, 1, 11, 3)
|
208
|
+
|
209
|
+
def default_run(self, **kwargs):
|
210
|
+
""" 默认执行器
|
211
|
+
"""
|
212
|
+
lines = cv2.HoughLinesP(self.binary_img, kwargs['rho'], kwargs['theta'],
|
213
|
+
kwargs['threshold'], kwargs['minLineLength'], kwargs['maxLineGap'])
|
214
|
+
if lines is None: lines = np.array([])
|
215
|
+
# 处理用二值化,显示用原图
|
216
|
+
self.imshow(xlcv.lines(self.img, lines))
|
217
|
+
|
218
|
+
|
219
|
+
def test_adaptiveThreshold(img=None):
|
220
|
+
""" 滑动条工具使用方法的示例 """
|
221
|
+
# 1 图片可以传入路径,这里做一个随机图片
|
222
|
+
if img is None:
|
223
|
+
img = np.random.randint(0, 255, [500, 200], dtype='uint8')
|
224
|
+
else:
|
225
|
+
img = xlcv.read(img, 0) # 确保是单通道灰度图
|
226
|
+
|
227
|
+
# 2 指定要分析的函数
|
228
|
+
t = TrackbarTool(imgproc=cv2.adaptiveThreshold, img=img, verbose=2, delay=100)
|
229
|
+
t.create_trackbar('maxValue', 255, 255) # 添加滑动条:名称,最大值,默认值
|
230
|
+
# 创建滑动条过程中,可能会报错,因为参数还不全,指定的imgproc还无法运行,可以先不用管
|
231
|
+
t.create_trackbar('adaptiveMethod', 10, 0)
|
232
|
+
t.create_trackbar('thresholdType', 10, 1)
|
233
|
+
t.create_trackbar('blockSize', 100, 11)
|
234
|
+
t.create_trackbar('C', 20, 3)
|
235
|
+
# t.imshow(img2) # 临时显式其他图片处理效果。如果需要持久修改,可以更改self.img的值
|
236
|
+
cv2.waitKey()
|
237
|
+
|
238
|
+
|
239
|
+
def test_CoarseDropout(img=None):
|
240
|
+
""" 做albumentations库里类功能的示例写法 """
|
241
|
+
import albumentations as A
|
242
|
+
|
243
|
+
if img is None:
|
244
|
+
img = np.random.randint(0, 255, [500, 200], dtype='uint8')
|
245
|
+
|
246
|
+
t = TrackbarTool.from_album(A.CoarseDropout, img=img, verbose=2)
|
247
|
+
t.create_trackbar('max_holes', 50, 8)
|
248
|
+
t.create_trackbar('max_height', 50, 8)
|
249
|
+
t.create_trackbar('max_width', 50, 8)
|
250
|
+
t.create_trackbar('fill_value', 255, 0)
|
251
|
+
cv2.waitKey()
|