Created
March 21, 2018 06:12
-
-
Save KelSolaar/af15b44945ac9db7ce49d0159fe0087c to your computer and use it in GitHub Desktop.
Multi-Spectral to XYZ - Integration
This file contains hidden or 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
#%% | |
import glob | |
import numpy as np | |
import colour | |
from colour import (DEFAULT_SPECTRAL_SHAPE, STANDARD_OBSERVERS_CMFS, ones_spd, | |
read_image) | |
from colour.utilities import tsplit, tstack, warning | |
from colour.plotting import image_plot | |
tifs_images = glob.glob('/Users/kelsolaar/Downloads/MetaCow/SmTiffs/*.tif') | |
RGB = [read_image(tifs_image) for tifs_image in tifs_images] | |
RGB = tstack(RGB) | |
#%% | |
def multi_spectral_to_XYZ_integration( | |
a, | |
shape=DEFAULT_SPECTRAL_SHAPE, | |
cmfs=STANDARD_OBSERVERS_CMFS['CIE 1931 2 Degree Standard Observer'], | |
illuminant=ones_spd(STANDARD_OBSERVERS_CMFS[ | |
'CIE 1931 2 Degree Standard Observer'].shape)): | |
if cmfs.shape != shape: | |
warning('Aligning "{0}" cmfs shape to "{1}".'.format(cmfs.name, shape)) | |
cmfs = cmfs.copy().align(shape) | |
if illuminant.shape != shape: | |
warning('Aligning "{0}" illuminant shape to "{1}".'.format( | |
illuminant.name, shape)) | |
illuminant = illuminant.copy().align(shape) | |
S = illuminant.values | |
x_bar, y_bar, z_bar = tsplit(cmfs.values) | |
dw = cmfs.shape.interval | |
k = 100 / (np.sum(y_bar * S) * dw) | |
X_p = a * x_bar * S * dw | |
Y_p = a * y_bar * S * dw | |
Z_p = a * z_bar * S * dw | |
XYZ = k * np.sum(np.array([X_p, Y_p, Z_p]), axis=-1) | |
return np.rollaxis(XYZ, 0, 3) | |
XYZ = multi_spectral_to_XYZ_integration( | |
RGB, | |
colour.SpectralShape(380, 760, 5), | |
illuminant=colour.ILLUMINANTS_RELATIVE_SPDS['D65']) | |
image_plot(colour.XYZ_to_sRGB(XYZ / 100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MetaCow