Skip to content

Commit

Permalink
Refactored code for loading variables from .mat files.
Browse files Browse the repository at this point in the history
  • Loading branch information
bllanos committed May 24, 2019
1 parent 4dde250 commit 046c12c
Show file tree
Hide file tree
Showing 20 changed files with 241 additions and 243 deletions.
9 changes: 1 addition & 8 deletions aberration_correction/CorrectByHyperspectralADMM.m
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,7 @@
] = loadDispersionModel(reverse_dispersion_model_filename, false);
end

model_variables_required = { 'sensor_map', 'channel_mode', 'bands' };
load(color_map_filename, model_variables_required{:});
if ~all(ismember(model_variables_required, who))
error('One or more of the required colour space conversion variables is not loaded.')
end

bands_color = bands;

[sensor_map, channel_mode, bands_color] = loadColorMap(color_map_filename);
if channel_mode
if has_dispersion && ...
((length(bands_color) ~= length(bands_dispersionfun)) ||...
Expand Down
20 changes: 2 additions & 18 deletions aberration_correction/CorrectByWarping.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,30 +169,14 @@
dispersion_data, bands_dispersionfun, transform_data...
] = loadDispersionModel(forward_dispersion_model_filename, true, true);

model_variables_required = { 'sensor_map', 'channel_mode', 'bands' };
load(color_map_filename, model_variables_required{:});
if ~all(ismember(model_variables_required, who))
error('One or more of the required colour space conversion variables is not loaded.')
end

bands_color = bands;

[sensor_map, channel_mode, bands_color] = loadColorMap(color_map_filename);
if channel_mode
if ((length(bands) ~= length(bands_dispersionfun)) ||...
any(bands(:) ~= bands_dispersionfun(:)))
error('When warping colour images, the same colour channels must be used by the model of dispersion.');
end
else
findSamplingOptions = [];
bands = [];
variables_required = { 'findSamplingOptions', 'bands' };
load(sampling_filename, variables_required{:});
if isempty(bands)
error('`bands` was not loaded from "%s".', sampling_filename)
end
if isempty(findSamplingOptions)
error('`findSamplingOptions` was not loaded from "%s".', sampling_filename)
end
[findSamplingOptions, bands] = loadVariables(sampling_filename, {'findSamplingOptions', 'bands'});
if disable_interpolation
color_weights = colorWeights(...
sensor_map, bands_color, bands, findSamplingOptions...
Expand Down
24 changes: 7 additions & 17 deletions aberration_data/BimaterialImages.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
% 'SonyColorMap.m', for example. The following variables are required:
% - 'sensor_map': A 2D array, where `sensor_map(i, j)` is the sensitivity
% of the i-th colour channel of the output sensor response images to the
% j-th spectral band of the synthetic hyperspectral images.
% j-th element of 'bands' (below).
% - 'channel_mode': A Boolean value indicating whether the input colour
% space is a set of colour channels (true) or a set of spectral bands
% (false). A value of `false` is required.
Expand Down Expand Up @@ -271,7 +271,8 @@
% ColorChecker spectral reflectances
colorChecker_table = readtable(reflectances_filename);
variable_names = colorChecker_table.Properties.VariableNames;
lambda_colorChecker = colorChecker_table.(variable_names{1});
bands = colorChecker_table.(variable_names{1});
n_bands = length(bands);
reflectances = colorChecker_table{:, 2:end};
n_patches = length(variable_names) - 1;

Expand All @@ -284,7 +285,7 @@
illuminant_temperature, lambda_illuminant, S_illuminant, lambda_illuminant...
);
else
lambda_illuminant = lambda_colorChecker;
lambda_illuminant = bands;
spd_illuminant = feval(illuminant_function_name, lambda_illuminant);
end

Expand All @@ -295,7 +296,7 @@

colorChecker_rgb = reflectanceToColor(...
lambda_illuminant, spd_illuminant,...
lambda_colorChecker, reflectances,...
bands, reflectances,...
lambda_xyzbar, xyzbar, findSamplingOptions.int_method...
);

Expand All @@ -318,24 +319,13 @@

%% Load colour space conversion data

model_variables_required = { 'sensor_map', 'channel_mode', 'bands' };
load(color_map_filename, model_variables_required{:});
if ~all(ismember(model_variables_required, who))
error('One or more of the required colour space conversion variables is not loaded.')
end
if channel_mode
error('The input space of the colour conversion data must be a spectral space, not a space of colour channels.')
end

bands_color = bands;
bands = lambda_colorChecker;
n_bands = length(bands);
[sensor_map, ~, bands_color] = loadColorMap(color_map_filename, false);

%% Calculate spectral radiances

[lambda_Rad, ~, Rad_normalized] = reflectanceToRadiance(...
lambda_illuminant, spd_illuminant,...
lambda_colorChecker, reflectances,...
bands, reflectances,...
bands_color, sensor_map.',...
normalization_channel, findSamplingOptions.int_method...
);
Expand Down
29 changes: 4 additions & 25 deletions data_analysis/ColorCorrection.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,40 +73,19 @@
n_images = n_spectral + n_color;

if n_spectral ~= 0
variables_required = { 'sensor_map', 'bands' };
load(color_map_filename, variables_required{:});
if ~all(ismember(variables_required, who))
error('One or more of the required colour space conversion variables is not loaded.')
end
bands_color = bands;
clear bands

variables_required = { 'findSamplingOptions', 'bands' };
load(sampling_filename, variables_required{:});
if ~all(ismember(variables_required, who))
error('One or more of the required sampling variables is not loaded.')
end
[sensor_map, ~, bands_color] = loadColorMap(color_map_filename);
[findSamplingOptions, bands] = loadVariables(sampling_filename, {'findSamplingOptions', 'bands'});
color_weights = colorWeights(...
sensor_map, bands_color, bands, findSamplingOptions...
);
end

if n_images ~= 0
if use_chromadapt
variables_required = {'wb_illum', 'wb_scale'};
load(wb_filename, variables_required{:});
if ~all(ismember(variables_required, who))
error('One or more of the required white balancing variables is not loaded.')
end
[wb_illum, wb_scale] = loadVariables(wb_filename, {'wb_illum', 'wb_scale'});
postfix = '_wb';
else
load(xyz_weights_filename, xyz_weights_variable);
if exist(xyz_weights_variable, 'var')
xyz_weights = eval(xyz_weights_variable);
end
if ~exist(xyz_weights_variable, 'var') || isempty(xyz_weights)
error('No raw colour to XYZ conversion data loaded.')
end
xyz_weights = loadVariables(xyz_weights_filename, xyz_weights_variable);
postfix = ['_' xyz_weights_variable];
end
end
Expand Down
10 changes: 1 addition & 9 deletions data_analysis/DispersionPSFs.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,7 @@
);
df_color = makeDispersionfun(dispersion_data, T_roi);

model_variables_required = { 'sensor_map', 'channel_mode', 'bands' };
load(color_map_filename, model_variables_required{:});
if ~all(ismember(model_variables_required, who))
error('One or more of the required colour space conversion variables is not loaded.')
end
if channel_mode
error('The input space of the colour conversion data must be a spectral space, not a space of colour channels.')
end
bands_color = bands;
[sensor_map, ~, bands_color] = loadColorMap(color_map_filename, false);

%% Load reflectance data

Expand Down
17 changes: 2 additions & 15 deletions data_analysis/ImageEvaluation.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@
end

if ~is_rgb
load(bands_filename, bands_variable);
if exist(bands_variable, 'var')
bands = eval(bands_variable);
end
if ~exist(bands_variable, 'var') || isempty(bands)
error('No wavelength band or colour channel index information loaded.')
end
bands = loadVariables(bands_filename, bands_variable);
end

if is_rgb
Expand Down Expand Up @@ -124,14 +118,7 @@
if isempty(spectral_weights_filenames{i})
spectral_weights = eye(length(bands));
else
spectral_weights = [];
load(spectral_weights_filenames{i}, spectral_weights_variables{i});
if exist(spectral_weights_variables{i}, 'var')
spectral_weights = eval(spectral_weights_variables{i});
end
if ~exist(spectral_weights_variables{i}, 'var') || isempty(spectral_weights)
error('No colour space conversion information loaded for image %d.', i)
end
spectral_weights = loadVariables(spectral_weights_filenames{i}, spectral_weights_variables{i});
end

dp.evaluation.global_spectral.plot_color =...
Expand Down
9 changes: 1 addition & 8 deletions data_analysis/IntraChannelDispersion.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@

% Find the wavelengths bounding the high-sensitivity regions of the camera's
% colour channels
model_variables_required = { 'sensor_map', 'channel_mode', 'bands' };
load(color_map_filename, model_variables_required{:});
if ~all(ismember(model_variables_required, who))
error('One or more of the required camera sensitivity variables is not loaded.')
end
if channel_mode
error('The input space of the camera sensitivity data must be a spectral space, not a space of colour channels.')
end
[sensor_map, ~, bands] = loadColorMap(color_map_filename, false);

cutoff_wavelength_sensitivites = max(sensor_map, [], 2);
sensor_map_relative = sensor_map ./ repmat(cutoff_wavelength_sensitivites, 1, size(sensor_map, 2));
Expand Down
8 changes: 1 addition & 7 deletions data_analysis/ShowPatches.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,7 @@
n_images = length(image_prefixes);
n_groups = length(algorithm_groups);

load(xyz_weights_filename, xyz_weights_variable);
if exist(xyz_weights_variable, 'var')
xyz_weights = eval(xyz_weights_variable);
end
if ~exist(xyz_weights_variable, 'var') || isempty(xyz_weights)
error('No raw colour to XYZ conversion matrix loaded.')
end
xyz_weights = loadVariables(xyz_weights_filename, xyz_weights_variable);

% Find the class of the image data
I_filename = fullfile(input_directory, sprintf('%s%s%s', image_prefixes{1}, algorithm_groups{1}{1}, input_postfix));
Expand Down
8 changes: 1 addition & 7 deletions dispersion_model/RegistrationDispersion.m
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,7 @@
%% Load calibration data

if ~rgb_mode
load(bands_filename, bands_variable);
if exist(bands_variable, 'var')
bands = eval(bands_variable);
end
if ~exist(bands_variable, 'var') || isempty(bands)
error('No wavelengths loaded.')
end
bands = loadVariables(bands_filename, bands_variable);
n_bands = length(bands);

[~, reference_index] = min(abs(bands - reference_wavelength));
Expand Down
28 changes: 3 additions & 25 deletions evaluation/Choi2017Input.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,12 @@

%% Load wavelengths

load(bands_filename, bands_variable);
if exist(bands_variable, 'var')
bands_spectral = eval(bands_variable);
end
if ~exist(bands_variable, 'var') || isempty(bands_spectral)
error('No wavelength band information loaded.')
end

load(desired_bands_filename, desired_bands_variable);
if exist(desired_bands_variable, 'var')
desired_bands_spectral = eval(desired_bands_variable);
end
if ~exist(desired_bands_variable, 'var') || isempty(desired_bands_spectral)
error('No desired wavelength band information loaded.')
end
bands_spectral = loadVariables(bands_filename, bands_variable);
desired_bands_spectral = loadVariables(desired_bands_filename, desired_bands_variable);

%% Load sensor data

model_variables_required = { 'sensor_map', 'channel_mode', 'bands' };
load(color_map_filename, model_variables_required{:});
if ~all(ismember(model_variables_required, who))
error('One or more of the required colour space conversion variables is not loaded.')
end
if channel_mode
error('The input space of the colour conversion data must be a spectral space, not a space of colour channels.')
end

bands_color = bands;
[sensor_map, ~, bands_color] = loadColorMap(color_map_filename, false);

%% Load the illuminant

Expand Down
8 changes: 3 additions & 5 deletions evaluation/Choi2017Output.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@

%% Load conversion data

variables_required = { 'spectral_weights_output', 'color_weights' };
load(conversion_filename, variables_required{:});
if ~all(ismember(variables_required, who))
error('One or more of the required colour/spectral space conversion variables is not loaded.')
end
[spectral_weights_output, color_weights] = loadVariables(...
conversion_filename, {'spectral_weights_output', 'color_weights'}...
);

%% Output images

Expand Down
10 changes: 1 addition & 9 deletions evaluation/CorrectChirpImage.m
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,7 @@

%% Load calibration data

model_variables_required = { 'sensor_map', 'channel_mode', 'bands' };
load(color_map_filename, model_variables_required{:});
if ~all(ismember(model_variables_required, who))
error('One or more of the required colour space conversion variables is not loaded.')
end
if channel_mode
error('The input space of the colour conversion data must be a spectral space, not a space of colour channels.')
end
bands_color = bands;
[sensor_map, ~, bands_color] = loadColorMap(color_map_filename, false);
n_channels = size(sensor_map, 1);

% Normalize spectral sensitivities to avoid out-of-range colour values.
Expand Down
28 changes: 4 additions & 24 deletions evaluation/EvaluateColorChecker.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
% 'SonyColorMap.m', for example. The following variables are required:
% - 'sensor_map': A 2D array, where `sensor_map(i, j)` is the sensitivity
% of the i-th colour channel of the output sensor response images to the
% j-th spectral band of the spectral images.
% j-th element of 'bands' (below).
% - 'channel_mode': A Boolean value indicating whether the input colour
% space is a set of colour channels (true) or a set of spectral bands
% (false). A value of `false` is required.
Expand Down Expand Up @@ -294,33 +294,13 @@
bands_qhyper = [];
color_weights = [];
if has_spectral
load(bands_filename, bands_variable);
if exist(bands_variable, 'var')
bands_estimated = eval(bands_variable);
end
if ~exist(bands_variable, 'var') || isempty(bands_estimated)
error('No wavelengths loaded.')
end
bands_estimated = loadVariables(bands_filename, bands_variable);
n_bands_estimated = length(bands_estimated);

load(qhyper_bands_filename, qhyper_bands_variable);
if exist(qhyper_bands_variable, 'var')
bands_qhyper = eval(qhyper_bands_variable);
end
if ~exist(qhyper_bands_variable, 'var') || isempty(bands_qhyper)
error('No wavelengths loaded.')
end
bands_qhyper = loadVariables(qhyper_bands_filename, qhyper_bands_variable);
n_bands_qhyper = length(bands_qhyper);

model_variables_required = { 'sensor_map', 'channel_mode', 'bands' };
load(color_map_filename, model_variables_required{:});
if ~all(ismember(model_variables_required, who))
error('One or more of the required colour space conversion variables is not loaded.')
end
if channel_mode
error('The input space of the colour conversion data must be a spectral space, not a space of colour channels.')
end
bands_color = bands;
[sensor_map, ~, bands_color] = loadColorMap(color_map_filename, false);
color_weights = colorWeights(...
sensor_map, bands_color, bands_estimated, findSamplingOptions...
);
Expand Down
16 changes: 2 additions & 14 deletions evaluation/PointSpectralEvaluation.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,24 +296,12 @@

bands_cell = cell(n_all, 1);
for i = 1:n_images
load(bands_filenames_rep{i}, bands_variable);
if exist(bands_variable, 'var')
bands_cell{i + n_measurement_sets} = eval(bands_variable);
end
if ~exist(bands_variable, 'var') || isempty(bands_cell{i + n_measurement_sets})
error('No wavelengths loaded from file "%s".', bands_filenames_rep{i})
end
bands_cell{i + n_measurement_sets} = loadVariables(bands_filenames_rep{i}, bands_variable);
end
bands = bands_cell;

if has_images
load(location_filename, location_variable);
if exist(location_variable, 'var')
patch_centers = eval(location_variable);
end
if ~exist(location_variable, 'var') || isempty(patch_centers)
error('No image patch center locations loaded from file "%s".', location_filename)
end
patch_centers = loadVariables(location_filename, location_variable);
end

%% Load all spectral measurements and spectral images
Expand Down
Loading

0 comments on commit 046c12c

Please sign in to comment.