-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8f96aa
commit 8485a06
Showing
11 changed files
with
1,788 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import cv2 | ||
|
||
def downsize(img, ratio): | ||
''' downsize 'img' by 'ratio'. ''' | ||
return cv2.resize(img, | ||
tuple(dim // ratio for dim in reversed(img.shape[:2])), | ||
interpolation = cv2.INTER_AREA) | ||
|
||
def channel_options(img, rank=False): | ||
''' Create a composite image of img in all of opencv's colour channels | ||
|img| -> | blue | green | red | | ||
| hue | saturation | value | | ||
| hue2 | luminosity | saturation2 | | ||
| lightness | green-red | blue-yellow | | ||
| lightness2 | u | v | | ||
'rank' is a boolean? specifying whether to also return a ranking of each | ||
channel by variability/sharpness/contrast/other? !NOT YET IMPLEMENTED! | ||
TODO | ||
-> make a string maybe, with several options available, or select | ||
multiple options in a list and get back an array or dataframe or | ||
something | ||
-> important to make nicely stackable to run on video and determine | ||
statistics on the best option for a given use case | ||
''' | ||
B,G,R = cv2.split(img) | ||
H,S,V = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) | ||
H2,L2,S2 = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HLS)) | ||
L,a,b = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2LAB)) | ||
L3,u,v = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2LUV)) | ||
channels = (((B, 'blue'), (G, 'green'), (R, 'red')), | ||
((H, 'hue'), (S, 'saturation'), (V, 'value')), | ||
((H2, 'hue2'), (L2, 'luminosity'), (S2, 'saturation2')), | ||
((L, 'lightness'), (a, 'green-red'), (b, 'blue-yellow')), | ||
((L3,'lightness2'), (u, 'u'), (v, 'v'))) | ||
out = [] | ||
for row in channels: | ||
img_row = [] | ||
for img, name in row: | ||
cv2.putText(img, name, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, | ||
0.6, 255, 1) | ||
img_row.append(img) | ||
out.append(cv2.hconcat(img_row)) | ||
return cv2.vconcat(out) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import cv2 | ||
|
||
|
||
waitKey = lambda ms : cv2.waitKey(ms) & 0xFF | ||
|
||
|
||
class DoNothing: | ||
''' A context manager that does nothing. ''' | ||
def __init__(self): pass | ||
def __enter__(self): return self | ||
def __exit__(self, *args): pass | ||
|
||
|
||
class MouseCallback: | ||
''' A context manager for temporary mouse callbacks. ''' | ||
def __init__(self, window, handler, param=None, | ||
restore=lambda *args: None, restore_param=None): | ||
''' Initialise with the window, handler, and restoration command. | ||
'window' is the name of the window to set the callback for. | ||
'handler' is the function for handling the callback, which should take | ||
x, y, flags ('&'ed EVENT_FLAG bits), and an optional param passed | ||
in from the callback handler. | ||
'param' is any Python object that should get passed to the handler | ||
on each call - useful for tracking state. | ||
'restore' is the function to restore as the handler on context exit. | ||
'restore_param' is the handler param to restore on context exit. | ||
''' | ||
self.window = window | ||
self.handler = handler | ||
self.param = param | ||
self.restore = restore | ||
self.restore_param = restore_param | ||
|
||
def __enter__(self): | ||
cv2.setMouseCallback(self.window, self.handler, self.param) | ||
return self | ||
|
||
def __exit__(self, *args): | ||
cv2.setMouseCallback(self.window, self.restore, self.restore_param) |
Oops, something went wrong.