SaDVIO
imgProcessing.h
Go to the documentation of this file.
1 #ifndef IMGPROCESSING_H
2 #define IMGPROCESSING_H
3 
4 #include "opencv2/highgui/highgui.hpp"
5 #include "opencv2/imgproc/imgproc.hpp"
6 #include <opencv2/imgproc.hpp>
7 
8 namespace isae {
9 namespace imgproc {
10 
14 inline void histogramEqualizationCLAHE(cv::Mat &image, float clahe_clip = 2) {
15  bool gray = image.channels() == 1;
16  if (gray)
17  cv::cvtColor(image, image, cv::COLOR_GRAY2BGR);
18 
19  cv::cvtColor(image, image, cv::COLOR_BGR2YCrCb);
20 
21  std::vector<cv::Mat> channels;
22  cv::split(image, channels);
23 
24  int tile_xsize = image.cols / 50;
25  int tile_ysize = image.rows / 50;
26 
27  cv::Ptr<cv::CLAHE> clahe =
28  cv::createCLAHE((clahe_clip < 1 ? 1 : clahe_clip),
29  cv::Size((tile_xsize < 1 ? 1 : tile_xsize), (tile_ysize < 1 ? 1 : tile_ysize)));
30  clahe->apply(channels[0], channels[0]);
31  cv::merge(channels, image);
32  cv::cvtColor(image, image, cv::COLOR_YCrCb2BGR);
33 
34  if (gray)
35  cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
36 }
37 
45 inline void AGCWD(const cv::Mat & src, cv::Mat & dst, double alpha)
46 {
47  int rows = src.rows;
48  int cols = src.cols;
49  int channels = src.channels();
50  int total_pixels = rows * cols;
51 
52  cv::Mat L;
53  cv::Mat HSV;
54  std::vector<cv::Mat> HSV_channels;
55  if (channels == 1) {
56  L = src.clone();
57  }
58  else {
59  cv::cvtColor(src, HSV, cv::COLOR_HSV2BGR_FULL);
60  cv::split(HSV, HSV_channels);
61  L = HSV_channels[2];
62  }
63 
64  int histsize = 256;
65  float range[] = { 0,256 };
66  const float* histRanges = { range };
67  cv::Mat hist;
68  calcHist(&L, 1, 0, cv::Mat(), hist, 1, &histsize, &histRanges, true, false);
69 
70  double total_pixels_inv = 1.0 / total_pixels;
71  cv::Mat PDF = cv::Mat::zeros(256, 1, CV_64F);
72  for (int i = 0; i < 256; i++) {
73  PDF.at<double>(i) = hist.at<float>(i) * total_pixels_inv;
74  }
75 
76  double pdf_min, pdf_max;
77  cv::minMaxLoc(PDF, &pdf_min, &pdf_max);
78  cv::Mat PDF_w = PDF.clone();
79  for (int i = 0; i < 256; i++) {
80  PDF_w.at<double>(i) = pdf_max * std::pow((PDF_w.at<double>(i) - pdf_min) / (pdf_max - pdf_min), alpha);
81  }
82 
83  cv::Mat CDF_w = PDF_w.clone();
84  double culsum = 0;
85  for (int i = 0; i < 256; i++) {
86  culsum += PDF_w.at<double>(i);
87  CDF_w.at<double>(i) = culsum;
88  }
89  CDF_w /= culsum;
90 
91  std::vector<uchar> table(256, 0);
92  for (int i = 1; i < 256; i++) {
93  table[i] = cv::saturate_cast<uchar>(255.0 * std::pow(i / 255.0, 1 - CDF_w.at<double>(i)));
94  }
95 
96  cv::LUT(L, table, L);
97 
98  if (channels == 1) {
99  dst = L.clone();
100  }
101  else {
102  cv::merge(HSV_channels, dst);
103  cv::cvtColor(dst, dst, cv::COLOR_HSV2BGR_FULL);
104  }
105 
106  return;
107 }
108 
112 inline double ZNCC(cv::Mat I0, cv::Mat I1) {
113 
114  // The patches must have the same size
115  if (I0.size() != I1.size()) {
116  return 1000;
117  }
118 
119  cv::Scalar mean0, stdev0, mean1, stdev1;
120  cv::meanStdDev(I0, mean0, stdev0);
121  cv::meanStdDev(I1, mean1, stdev1);
122 
123  double s = 0;
124  for (int i = 0; i < I0.rows; i++) {
125  for (int j = 0; j < I0.cols; j++) {
126  s = s + (I0.at<uint8_t>(i, j) - mean0[0]) * (I1.at<uint8_t>(i, j) - mean1[0]);
127  }
128  }
129 
130  return s / (I0.rows * I0.cols * stdev0[0] * stdev1[0]);
131 }
132 
133 } // namespace imgproc
134 } // namespace isae
135 
136 #endif // IMGPROCESSING_H
isae::imgproc::histogramEqualizationCLAHE
void histogramEqualizationCLAHE(cv::Mat &image, float clahe_clip=2)
Apply CLAHE to the image passed as parameter.
Definition: imgProcessing.h:14
isae
Definition: AFeature2D.h:8
isae::imgproc::ZNCC
double ZNCC(cv::Mat I0, cv::Mat I1)
Computes the Zero Normalized Cross Corelation between two images (usually small patches)
Definition: imgProcessing.h:112
isae::imgproc::AGCWD
void AGCWD(const cv::Mat &src, cv::Mat &dst, double alpha)
Apply adaptive gamma correction.
Definition: imgProcessing.h:45