forked from kerlomz/captcha_trainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretreatment.py
52 lines (41 loc) · 1.39 KB
/
pretreatment.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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author: kerlomz <kerlomz@gmail.com>
import cv2
class Pretreatment(object):
def __init__(self, origin):
self.origin = origin
def get(self):
return self.origin
def binarization(self, value, modify=False):
ret, _binarization = cv2.threshold(self.origin, value, 255, cv2.THRESH_BINARY)
if modify:
self.origin = _binarization
return _binarization
def median_blur(self, value, modify=False):
if not value:
return self.origin
value = value + 1 if value % 2 == 0 else value
_smooth = cv2.medianBlur(self.origin, value)
if modify:
self.origin = _smooth
return _smooth
def gaussian_blur(self, value, modify=False):
if not value:
return self.origin
value = value + 1 if value % 2 == 0 else value
_blur = cv2.GaussianBlur(self.origin, (value, value), 0)
if modify:
self.origin = _blur
return _blur
def preprocessing(image, binaryzation=-1, smooth=-1, blur=-1):
pretreatment = Pretreatment(image)
if binaryzation > 0:
pretreatment.binarization(binaryzation, True)
if smooth != -1:
pretreatment.median_blur(smooth, True)
if blur != -1:
pretreatment.gaussian_blur(blur, True)
return pretreatment.get()
if __name__ == '__main__':
pass