Skip to content

Commit 8485a06

Browse files
committed
pip
1 parent d8f96aa commit 8485a06

File tree

11 files changed

+1788
-0
lines changed

11 files changed

+1788
-0
lines changed

build/lib/pcv/__init__.py

Whitespace-only changes.

build/lib/pcv/helpers.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
3+
import cv2
4+
5+
def downsize(img, ratio):
6+
''' downsize 'img' by 'ratio'. '''
7+
return cv2.resize(img,
8+
tuple(dim // ratio for dim in reversed(img.shape[:2])),
9+
interpolation = cv2.INTER_AREA)
10+
11+
def channel_options(img, rank=False):
12+
''' Create a composite image of img in all of opencv's colour channels
13+
14+
|img| -> | blue | green | red |
15+
| hue | saturation | value |
16+
| hue2 | luminosity | saturation2 |
17+
| lightness | green-red | blue-yellow |
18+
| lightness2 | u | v |
19+
20+
'rank' is a boolean? specifying whether to also return a ranking of each
21+
channel by variability/sharpness/contrast/other? !NOT YET IMPLEMENTED!
22+
TODO
23+
-> make a string maybe, with several options available, or select
24+
multiple options in a list and get back an array or dataframe or
25+
something
26+
-> important to make nicely stackable to run on video and determine
27+
statistics on the best option for a given use case
28+
29+
'''
30+
B,G,R = cv2.split(img)
31+
H,S,V = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
32+
H2,L2,S2 = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HLS))
33+
L,a,b = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2LAB))
34+
L3,u,v = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2LUV))
35+
channels = (((B, 'blue'), (G, 'green'), (R, 'red')),
36+
((H, 'hue'), (S, 'saturation'), (V, 'value')),
37+
((H2, 'hue2'), (L2, 'luminosity'), (S2, 'saturation2')),
38+
((L, 'lightness'), (a, 'green-red'), (b, 'blue-yellow')),
39+
((L3,'lightness2'), (u, 'u'), (v, 'v')))
40+
out = []
41+
for row in channels:
42+
img_row = []
43+
for img, name in row:
44+
cv2.putText(img, name, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
45+
0.6, 255, 1)
46+
img_row.append(img)
47+
out.append(cv2.hconcat(img_row))
48+
return cv2.vconcat(out)

build/lib/pcv/interact.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
3+
import cv2
4+
5+
6+
waitKey = lambda ms : cv2.waitKey(ms) & 0xFF
7+
8+
9+
class DoNothing:
10+
''' A context manager that does nothing. '''
11+
def __init__(self): pass
12+
def __enter__(self): return self
13+
def __exit__(self, *args): pass
14+
15+
16+
class MouseCallback:
17+
''' A context manager for temporary mouse callbacks. '''
18+
def __init__(self, window, handler, param=None,
19+
restore=lambda *args: None, restore_param=None):
20+
''' Initialise with the window, handler, and restoration command.
21+
22+
'window' is the name of the window to set the callback for.
23+
'handler' is the function for handling the callback, which should take
24+
x, y, flags ('&'ed EVENT_FLAG bits), and an optional param passed
25+
in from the callback handler.
26+
'param' is any Python object that should get passed to the handler
27+
on each call - useful for tracking state.
28+
'restore' is the function to restore as the handler on context exit.
29+
'restore_param' is the handler param to restore on context exit.
30+
31+
'''
32+
self.window = window
33+
self.handler = handler
34+
self.param = param
35+
self.restore = restore
36+
self.restore_param = restore_param
37+
38+
def __enter__(self):
39+
cv2.setMouseCallback(self.window, self.handler, self.param)
40+
return self
41+
42+
def __exit__(self, *args):
43+
cv2.setMouseCallback(self.window, self.restore, self.restore_param)

0 commit comments

Comments
 (0)