-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRGBtoLuminance.py
More file actions
117 lines (95 loc) · 3.56 KB
/
RGBtoLuminance.py
File metadata and controls
117 lines (95 loc) · 3.56 KB
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
# -------- Activate virtual environment -------------------------
import os
import ctypes
import sys
from pathlib import Path
def search_activation_path():
for i in range(5):
final_path = str(Path(__file__).parents[i]) + '\\env\\Scripts\\activate_this.py'
if os.path.exists(final_path):
return final_path
return ''
activate_path = search_activation_path()
if os.path.exists(activate_path):
exec(open(activate_path).read(), {'__file__': activate_path})
print(f'Aivia virtual environment activated\nUsing python: {activate_path}')
else:
error_mess = f'Error: {activate_path} was not found.\n\nPlease check that:\n' \
f' 1/ The \'FirstTimeSetup.py\' script was already run in Aivia,\n' \
f' 2/ The current python recipe is in one of the "\\PythonEnvForAivia\\" subfolders.'
ctypes.windll.user32.MessageBoxW(0, error_mess, 'Error', 0)
sys.exit(error_mess)
# ---------------------------------------------------------------
import os.path
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread, imsave
"""
Computes the luminance of an RGB image and returns that as a new channel.
Useful when images are saved as RGB (e.g. histopathology, photographs, etc.)
and the user desires to apply a recipe or pixel classifier to only one
channel, but wishes to retain the maximum amount of information from each.
The luminance has a 30% contribution from the red channel, 59%
contribution from the green channel, and 11% contribution from the
blue channel.
Requirements
------------
numpy (comes with Aivia installer)
scikit-image (comes with Aivia installer)
matplotlib
wxpython or PySide2 (for matplotlib to display charts)
Parameters
----------
Red : Aivia channel
Red channel.
Green : Aivia channel
Green channel.
Blue : Aivia channel
Blue channel.
Histogram : int (bool)
Boolean to determine whether to display the resulting luminance histogram.
0 : Do not display
1 : Display
Displaying the histogram halts the script until the Matplotlib popup is closed.
Returns
-------
Aivia channel
Result of the transform
"""
# [INPUT Name:blue_c Type:string DisplayName:'Blue Channel']
# [INPUT Name:green_c Type:string DisplayName:'Green Channel']
# [INPUT Name:red_c Type:string DisplayName:'Red Channel']
# [INPUT Name:histogram Type:int DisplayName:'Show Histogram (0=no, 1=yes)' Default:0 Min:0 Max:1]
# [OUTPUT Name:gray_c Type:string DisplayName:'Luminance']
def run(params):
red_c = params['red_c']
blue_c = params['blue_c']
green_c = params['green_c']
gray_c = params['gray_c']
show_histogram = int(params['histogram'])
if not os.path.exists(red_c):
print(f'Error: {red_c} does not exist')
return;
if not os.path.exists(blue_c):
print(f'Error: {blue_c} does not exist')
return;
if not os.path.exists(green_c):
print(f'Error: {green_c} does not exist')
return;
red_data = imread(red_c)
blue_data = imread(blue_c)
green_data = imread(green_c)
gray_data = np.empty_like(red_data)
print(f'Red: {red_data.nbytes}')
print(f'Blue: {blue_data.nbytes}')
print(f'Green: {green_data.nbytes}')
print(f'Gray: {gray_data.nbytes}')
gray_data = (0.3*red_data + 0.59*green_data + 0.11*blue_data).astype(red_data.dtype)
if show_histogram == 1:
ax = plt.hist(gray_data.ravel(), bins=256)
plt.show()
imsave(gray_c, gray_data)
if __name__ == '__main__':
params = {}
run(params)
# v1.01: - New virtual env code for auto-activation