Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
- Removed passing around of trivial parameters, calculating them as-needed instead.
  • Loading branch information
bllanos committed Jun 20, 2017
1 parent 0f794c3 commit 98ebeaf
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 141 deletions.
49 changes: 20 additions & 29 deletions CreateProbeDetectionModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,23 @@
% created from 'probe_color_distributions_gaussian' using
% `mlDiscreteClassifier`. A uniform hue distribution is assumed for the
% image background colours.
% - 'probe_color_distribution_increment': A scalar equal to the spacing
% between the samples of hue values in the range [0, 1] at which the
% hue density estimators have been evaluated to produce
% 'probe_color_distributions_*'. To find the approximate value of the
% i-th estimator at a query value 'x' in the range [0, 1], use:
%
% Additionally, the output file contains the values of all parameters in
% the first section of the script below, for reference. (Specifically,
% those listed in `parameters_list`, which should be updated if the set of
% parameters is changed.)
%
% ## Notes
% - To find the approximate value of the i-th hue estimator at a query
% value 'x', where 'x' must be in the range of hues ([0, 1]), use:
%
% ```
% queryDiscretized1DFunction(...
% x, probe_color_distributions_*(:, i),...
% probe_color_distribution_increment...
% hueSamplingParams(probe_color_distributions_*(:, i))...
% )
% ```
%
% Additionally, the output file contains the values of all parameters in
% the first section of the script below, for reference. (Specifically,
% those listed in `parameters_list`, which should be updated if the set of
% parameters is changed.)
%
% ## References
% - T. Gevers and H. Stokman. "Robust Histogram Construction from Color
% Invariants for Object Recognition". IEEE Transactions on Pattern
Expand Down Expand Up @@ -418,17 +417,11 @@
G = I_double(:, :, 2);
B = I_double(:, :, 3);
for i = 1:n_colors
[...
probe_color_distributions_kernel(:, i),...
~...
] = hueVariableKernelDensityEstimator(...
probe_color_distributions_kernel(:, i) = hueVariableKernelDensityEstimator(...
H, R, G, B, probe_color_masks(:, :, i),...
rgb_sigma_polyfit, probe_color_distribution_resolution...
);
[...
probe_color_distributions_gaussian(:, i),...
probe_color_distribution_increment...
] = hueGaussianDensityEstimator(...
probe_color_distributions_gaussian(:, i) = hueGaussianDensityEstimator(...
H, probe_color_masks(:, :, i), probe_color_distribution_resolution...
);
end
Expand All @@ -439,35 +432,34 @@
legend_names{i} = sprintf('Probe color %d', i);
end
plotHueDensityEstimator(...
probe_color_distribution_increment, probe_color_distributions_kernel, legend_names...
probe_color_distributions_kernel, legend_names...
);
title('Hue variable kernel density estimators for colors on the probe')
plotHueDensityEstimator(...
probe_color_distribution_increment, probe_color_distributions_gaussian, legend_names...
probe_color_distributions_gaussian, legend_names...
);
title('Hue Gaussian density estimators for colors on the probe')
end

%% Create a hue classifier

h_inc = hueSamplingParams( probe_color_distribution_resolution );
probe_color_classifier_kernel = mlDiscreteClassifier(...
probe_color_distributions_kernel, probe_color_distribution_increment,...
'periodic'...
probe_color_distributions_kernel, h_inc, 'periodic'...
);

probe_color_classifier_gaussian = mlDiscreteClassifier(...
probe_color_distributions_gaussian, probe_color_distribution_increment,...
'periodic'...
probe_color_distributions_gaussian, h_inc, 'periodic'...
);

if plot_hue_classifiers
plotHueClassifier(...
probe_color_distribution_increment, probe_color_classifier_kernel,...
probe_color_classifier_kernel,...
n_colors...
);
title('Hue variable kernel distribution classifier for colors on the probe')
plotHueClassifier(...
probe_color_distribution_increment, probe_color_classifier_gaussian,...
probe_color_classifier_gaussian,...
n_colors...
);
title('Hue Gaussian distribution classifier for colors on the probe')
Expand All @@ -479,8 +471,7 @@
'probe_color_distributions_kernel',...
'probe_color_classifier_kernel',...
'probe_color_distributions_gaussian',...
'probe_color_classifier_gaussian',...
'probe_color_distribution_increment'...
'probe_color_classifier_gaussian'...
} ];
uisave(save_variables_list,'probeDetectionModel')
disp('Reminder: The output model is specific to the probe, camera, and camera parameters.')
7 changes: 2 additions & 5 deletions DetectProbe.m
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,8 @@

%% Load the probe detection model
model_variables_required = {...
'probe_color_distribution_resolution',...
'probe',...
'probe_color_distributions_kernel',...
'probe_color_distribution_increment',...
'model_filename'...
};
load(detection_model_filename, model_variables_required{:});
Expand All @@ -244,7 +242,6 @@
else
probe_mask_initial = detectBoundingBoxes(...
I, probe,...
probe_color_distribution_resolution,...
probe_color_distributions_kernel, rgb_sigma_polyfit,...
detectBoundingBoxesParams, detectBoundingBoxesVerbose...
);
Expand All @@ -255,7 +252,7 @@
[...
interest_points_detected, probe_regions_bw_final_filtered...
] = detectWithinBoundingBox(...
I, probe, probe_mask_initial, probe_color_distribution_resolution,...
I, probe, probe_mask_initial,...
probe_color_distributions_kernel, rgb_sigma_polyfit,...
detectWithinBoundingBoxParams, detectWithinBoundingBoxVerbose...
);
Expand Down Expand Up @@ -300,7 +297,7 @@
[...
probe_colors_detected_left, probe_colors_detected_right...
] = labelDetectedColors(...
I, probe_color_distributions_kernel, probe_color_distribution_increment,...
I, probe_color_distributions_kernel,...
probe_regions_bw_final_filtered,...
model_from_image_detected_pca_space, model_from_image_detected,...
model_from_image_transform, model_from_image_detected_axes(1, :),...
Expand Down
12 changes: 2 additions & 10 deletions detectBoundingBoxes.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function [ mask ] = detectBoundingBoxes(...
I, probe,...
probe_color_distribution_resolution,...
probe_color_distributions, rgb_sigma_polyfit,...
params, varargin...
)
Expand All @@ -9,15 +8,13 @@
% ## Syntax
% mask = detectBoundingBoxes(...
% I, probe,...
% probe_color_distribution_resolution,...
% probe_color_distributions, rgb_sigma_polyfit,...
% params [, verbose]...
% )
%
% ## Description
% mask = detectBoundingBoxes(...
% I, probe,...
% probe_color_distribution_resolution,...
% probe_color_distributions, rgb_sigma_polyfit,...
% params [, verbose]...
% )
Expand All @@ -38,11 +35,6 @@
% Refer to the documentation of './CreateProbeDetectionModel.m' for
% details.
%
% probe_color_distribution_resolution -- Probe colour estimator sample count
% The number of equally-spaced samples in the range of hue values from 0
% (inclusive) to 1 (inclusive) at which the density estimators for probe
% colour bands have been evaluated.
%
% probe_color_distributions -- Probe colour estimators
% Discretized density estimators of image hue values corresponding to the
% different coloured bands on the probe, in the same order (starting from
Expand Down Expand Up @@ -106,7 +98,7 @@
%% Parse input arguments

nargoutchk(1,1);
narginchk(6,7);
narginchk(5,6);

if ~isempty(varargin)
verbose = varargin{1};
Expand Down Expand Up @@ -144,7 +136,7 @@
%% Find an initial bounding area for the probe

[ probe_regions_initial, probe_regions_bw_initial] = extractBinaryRegions(...
I, probe_color_distributions, probe_color_distribution_resolution,...
I, probe_color_distributions,...
rgb_sigma_polyfit,...
params.erosion_radius,...
[],...
Expand Down
22 changes: 9 additions & 13 deletions detectWithinBoundingBox.m
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
function [...
interest_points, probe_regions_bw_filtered...
] = detectWithinBoundingBox(...
I, probe, mask, probe_color_distribution_resolution,...
I, probe, mask,...
probe_color_distributions, rgb_sigma_polyfit,...
params, varargin...
)
%DETECTWITHINBOUNDINGBOX Find the probe within a bounding region
%
% ## Syntax
% [ interest_points ] = detectWithinBoundingBox(...
% I, probe, mask, probe_color_distribution_resolution,...
% I, probe, mask,...
% probe_color_distributions, rgb_sigma_polyfit,...
% params [, verbose]...
% )
% [...
% interest_points, probe_regions_bw_filtered...
% ] = detectWithinBoundingBox(...
% I, probe, mask, probe_color_distribution_resolution,...
% I, probe, mask,...
% probe_color_distributions, rgb_sigma_polyfit,...
% params [, verbose]...
% )
%
% ## Description
% [ interest_points ] = detectWithinBoundingBox(...
% I, probe, mask, probe_color_distribution_resolution,...
% I, probe, mask,...
% probe_color_distributions, rgb_sigma_polyfit,...
% params [, verbose]...
% )
Expand All @@ -33,7 +33,7 @@
% [...
% interest_points, probe_regions_bw_filtered...
% ] = detectWithinBoundingBox(...
% I, probe, mask, probe_color_distribution_resolution,...
% I, probe, mask,...
% probe_color_distributions, rgb_sigma_polyfit,...
% params [, verbose]...
% )
Expand All @@ -54,11 +54,6 @@
% channel, not three), where the nonzero pixels ideally define regions
% containing the colour bands of the probe.
%
% probe_color_distribution_resolution -- Probe colour estimator sample count
% The number of equally-spaced samples in the range of hue values from 0
% (inclusive) to 1 (inclusive) at which the density estimators for probe
% colour bands have been evaluated.
%
% probe_color_distributions -- Probe colour estimators
% Discretized density estimators of image hue values corresponding to the
% different coloured bands on the probe, in the same order (starting from
Expand Down Expand Up @@ -138,7 +133,7 @@
%% Parse input arguments

nargoutchk(1,2);
narginchk(7,8);
narginchk(6,7);

if ~isempty(varargin)
verbose = varargin{1};
Expand Down Expand Up @@ -173,7 +168,7 @@
%% Find final regions corresponding to probe colours

[ probe_regions_final, probe_regions_bw_final] = extractBinaryRegions(...
I, probe_color_distributions, probe_color_distribution_resolution,...
I, probe_color_distributions,...
rgb_sigma_polyfit,...
params.erosion_radius,...
mask,...
Expand Down Expand Up @@ -204,9 +199,10 @@

if display_regions_colored
probe_regions_bw_final_display = zeros(image_height, image_width, image_n_channels);
[~, color_distribution_resolution] = hueSamplingParams( probe_color_distributions(:, 1) );
for i = 1:n_colors
[ ~, peak_hue_index ] = max(probe_color_distributions(:, i));
peak_hue = (peak_hue_index - 1) / (probe_color_distribution_resolution - 1);
peak_hue = (peak_hue_index - 1) / (color_distribution_resolution - 1);
peak_rgb = hsv2rgb([peak_hue, 1, 1]);
probe_regions_bw_final_filtered_i = probe_regions_bw_filtered(:, :, i);
probe_regions_bw_final_display = probe_regions_bw_final_display +...
Expand Down
Loading

0 comments on commit 98ebeaf

Please sign in to comment.