Optimized Belief Propagation (CPU and GPU)
DisparityMap.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
27 #include "DisparityMap.h"
28 
29 template<class T>
30 requires std::is_arithmetic_v<T>
31 void DisparityMap<T>::SaveDisparityMap(const std::string& disparity_map_file_path, unsigned int scale_factor) const {
32  //declare and allocate the space for the disparity map image to save
33  BpImage<char> disparity_image(this->width_height_);
34 
35  //go though every pixel in the disparity map and compute the intensity value to use in the "disparity map image"
36  //by multiplying the pixel disparity value by scale factor
37  std::ranges::transform(this->PointerToPixelsStart(), this->PointerToPixelsStart() + this->TotalPixels(),
38  disparity_image.PointerToPixelsStart(),
39  [scale_factor](const T& current_pixel) -> char {
40  return (char)(((float)current_pixel)*((float)scale_factor) + 0.5f);
41  });
42 
43  disparity_image.SaveImageAsPgm(disparity_map_file_path);
44 }
45 
46 //TODO: look into case where no known disparity in ground truth disparity map
47 template<class T>
48 requires std::is_arithmetic_v<T>
50  const DisparityMap& disparity_map_to_compare,
51  const beliefprop::DisparityMapEvaluationParams& evaluation_parameters) const
52 {
53  //initialize output evaluation with evaluation parameters
54  DisparityMapEvaluation output_evaluation;
55  output_evaluation.InitializeWithEvalParams(evaluation_parameters);
56 
57  //initialize total disparity difference across all pixels with and without max for disparity difference to 0
58  std::array<float, 2> total_disp_abs_diff_no_max_w_max{0, 0};
59 
60  //go through each disparity map output pixel and evaluate output
61  //against corresponding output in disparity map to compare
62  for (unsigned int i = 0; i < this->TotalPixels(); i++) {
63  //get disparity difference between disparity maps at pixel i
64  const T disp_map_val = (this->pixels_.get())[i];
65  const T disp_map_compare_val = disparity_map_to_compare.PixelAtPoint(i);
66  const T abs_diff = std::abs(disp_map_val - disp_map_compare_val);
67 
68  //add disparity difference at pixel to total disparity difference across pixels
69  //with and without max disparity difference
70  total_disp_abs_diff_no_max_w_max[0] += abs_diff;
71  total_disp_abs_diff_no_max_w_max[1] += std::min(abs_diff, evaluation_parameters.max_diff_cap);
72 
73  //increment number of pixels that differ greater than threshold for each threshold
74  std::ranges::for_each(evaluation_parameters.output_diff_thresholds,
75  [abs_diff, &output_evaluation](const auto threshold) {
76  if (abs_diff > threshold) {
77  output_evaluation.num_sig_diff_pixels_at_thresholds_[threshold]++;
78  }
79  });
80  }
81 
82  output_evaluation.average_disp_abs_diff_no_max_w_max_[0] =
83  total_disp_abs_diff_no_max_w_max[0] / this->TotalPixels();
84  output_evaluation.average_disp_abs_diff_no_max_w_max_[1] =
85  total_disp_abs_diff_no_max_w_max[1] / this->TotalPixels();
86 
87  //need to cast unsigned ints to float to get proportion of pixels that differ by more than threshold
88  std::ranges::transform(output_evaluation.num_sig_diff_pixels_at_thresholds_,
89  std::inserter(output_evaluation.prop_sig_diff_pixels_at_thresholds_,
90  output_evaluation.prop_sig_diff_pixels_at_thresholds_.end()),
91  [this](const auto& sig_diff_pixel_at_threshold) -> std::pair<float, float> {
92  const auto& [threshold, num_sig_diff_pixels_thresh] = sig_diff_pixel_at_threshold;
93  return {threshold, ((float)num_sig_diff_pixels_thresh) / ((float)(this->TotalPixels()))};
94  });
95 
96  return output_evaluation;
97 }
98 
99 template class DisparityMap<float>;
Declares child class of BpImage to define disparity map image that is output from bp processing.
Class to define images that are used in bp processing.
Definition: BpImage.h:56
void SaveImageAsPgm(const std::string &filename) const
Definition: BpImage.h:108
T PixelAtPoint(const std::array< unsigned int, 2 > &point_xy) const
Definition: BpImage.h:89
T * PointerToPixelsStart() const
Definition: BpImage.h:85
Class to store disparity map evaluation results. Specifically comparison between two disparity maps s...
void InitializeWithEvalParams(const beliefprop::DisparityMapEvaluationParams &eval_params)
Initialize evaluation results with evaluation parameters.
Child class of BpImage to define disparity map image that is output from bp processing.
Definition: DisparityMap.h:45
DisparityMapEvaluation OutputComparison(const DisparityMap &disparity_map_to_compare, const beliefprop::DisparityMapEvaluationParams &evaluation_parameters) const
void SaveDisparityMap(const std::string &disparity_map_file_path, unsigned int scale_factor=1) const
bp_single_thread_imp::image< uchar > * threshold(bp_single_thread_imp::image< T > *src, int t)
Definition: imutil.h:51
T abs(const T &x)
Definition: misc.h:45
Struct to store parameters for evaluation of disparity map from stereo processing.
const std::vector< float > output_diff_thresholds
Difference thresholds for comparing disparity maps.