|
| 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) |
0 commit comments