-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.py
130 lines (100 loc) · 4.67 KB
/
filters.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
119
120
121
122
123
124
125
126
127
128
129
130
# Goal is to achieve artistic effects, similar to filters
# found in Photoshop or GIMP.
import cv2
import numpy as np
import utils #contains general purpose math functions
## Designing object-oriented curve filters
class VFuncFilter(object):
"""A filter that applies a function to V (or all of BGR)"""
def __init__(self, vFunc = None, dtype = np.uint8):
length = np.iinfo(dtype).max + 1
self._vLookupArray = utils.createLookupArray(vFunc, length)
def apply(self, src, dst):
"""Apply the filter with a BGR or gray source/destination."""
srcFlatView = utils.createFlatView(src)
dstFlatView = utils.createFlatView(dst)
utils.applyLookupArray(self._vLookupArray, srcFlatView, dstFlatView)
class VCurveFilter(VFuncFilter):
"""A filter that applies to a curve to V (or all of BGR)"""
def __init__(self, vPoints, dtype=np.uint8):
VFuncFilter.__init__(self, utils.createCurvFunc(vPoints), dtype)
class BGRFuncFilter(object):
"""A filter that applies different functions to each of BGR."""
def __init__(self, vFunc = None, bFunc = None, gFunc = None, rFunc = None, dtype = np.uint8):
length = np.iinfo(dtype).max + 1
self._bLookupArray = utils.createLookupArray(utils.createCompositeFunc(bFunc, vFunc), length)
self._gLookupArray = utils.createLookupArray(utils.createCompositeFunc(gFunc, vFunc), length)
self._rLookupArray = utils.createLookupArray(utils.createCompositeFunc(rFunc, vFunc), length)
def apply(self, src, dst):
"""Apply the filter with a BGR source/destination"""
b, g, r = cv2.split(src)
utils.applyLookupArray(self._bLookupArray, b, b)
utils.applyLookupArray(self._gLookupArray, g, g)
utils.applyLookupArray(self._rLookupArray, r, r)
cv2.merge([b, g, r], dst)
class BGRCurveFilter(BGRFuncFilter):
"""A filter that applies different curves to each of BGR."""
def __init__(self, vPoints = None, bPoints = None, gPoints = None, rPoints = None, dtype = np.uint8):
BGRFuncFilter.__init__(self,
utils.createCurveFunc(vPoints),
utils.createCurveFunc(bPoints),
utils.createCurveFunc(gPoints),
utils.createCurveFunc(rPoints), dtype)
### EMULATING PHOTO FILMS ###
#Emulating Kodak Portra
class BGRPortraCurveFilter(BGRCurveFilter):
"""A filter that applies Portra-like curves to BGR"""
def __init__(self, dtype=np.uint8):
BGRCurveFilter.__init__(
self,
vPoints = [(0,0),(23,20),(157,173),(255,255)],
bPoints = [(0,0),(41,46),(231,228),(255,255)],
gPoints = [(0,0),(52,47),(189,196),(255,255)],
rPoints = [(0,0),(69,69),(213,218),(255,255)],
dtype = dtype
)
#Emulating Fuji Provia
class BGRProviaCurveFilter(BGRCurveFilter):
"""A filter that applies Portra-like curves to BGR"""
def __init__(self, dtype=np.uint8):
BGRCurveFilter.__init__(
self,
bPoints = [(0,0),(35,25),(205,227),(255,255)],
gPoints = [(0,0),(27,21),(196,207),(255,255)],
rPoints = [(0,0),(59,54),(202,210),(255,255)],
dtype = dtype
)
#Emulating Fuji Velvia
class BGRVelviaCurveFilter(BGRCurveFilter):
"""A filter that applies Velvia-like curves to BGR."""
def __init__(self, dtype = np.uint8):
BGRCurveFilter.__init__(
self,
vPoints = [(0,0),(128,118),(221,215),(255,255)],
bPoints = [(0,0),(25,21),(122,153),(165,206),(255,255)],
gPoints = [(0,0),(25,21),(95,102),(181,208),(255,255)],
rPoints = [(0,0),(41,28),(183,209),(255,255)],
dtype = dtype)
#Emulating cross-processing
class BGRCrossProcessCurveFilter(BGRCurveFilter):
"""A filter that applies cross-process-like curves to BGR."""
def __init__(self, dtype = np.uint8):
BGRCurveFilter.__init__(
self,
bPoints = [(0,20),(255,235)],
gPoints = [(0,0),(56,39),(208,226),(255,255)],
rPoints = [(0,0),(56,22),(211,255),(255,255)],
dtype = dtype)
### Highlighting Edges
def strokeEdges(src, dst, blurKsize = 7, edgeKsize = 5):
if blurKsize >= 3:
blurredSrc = cv2.medianBlur(src, blurKsize)
graySrc = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
else:
graySrc = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
cv2.Laplacian(graySrc, cv2.CV_8U, graySrc, ksize=edgeKsize)
normInvAlpha = (1.0/255) * (255 - graySrc)
channels = cv2.split(src)
for channel in channels:
channel[:] = channel * normInvAlpha
cv2.merge(channels, dst)