PICCANTE  0.4
The hottest HDR imaging library!
mse.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_METRICS_MSE_HPP
19 #define PIC_METRICS_MSE_HPP
20 
21 #include <math.h>
22 
23 #include "../base.hpp"
24 #include "../image.hpp"
25 #include "../metrics/base.hpp"
26 
27 namespace pic {
28 
37 PIC_INLINE double MSE(Image *ori, Image *cmp, bool bLargeDifferences = false, METRICS_DOMAIN type = MD_LIN)
38 {
39  if(ori == NULL || cmp == NULL) {
40  return -2.0;
41  }
42 
43  if(!ori->isValid() || !cmp->isValid()) {
44  return -4.0;
45  }
46 
47  if(!ori->isSimilarType(cmp)) {
48  return -1.0;
49  }
50 
51  int size = ori->size();
52 
53 
54  int count = 0;
55 
56  float largeDifferences = C_LARGE_DIFFERENCESf;
57 
58  if(!bLargeDifferences) {
59  largeDifferences = FLT_MAX;
60  }
61 
62  double acc = 0.0;
63  for(int i = 0; i < size; i++) {
64  float o_val = changeDomain(ori->data[i], type);
65  float c_val = changeDomain(cmp->data[i], type);
66 
67  double delta = double(o_val - c_val);
68 
69  if(delta <= largeDifferences) {
70  acc += delta * delta;
71  count++;
72  }
73  }
74 
75  return acc / double(count);
76 }
77 
87 PIC_INLINE double MSE(Image *ori, Image *cmp, float gamma = 2.2f, float fstop = 0.0f, int nBit = 8)
88 {
89  if(ori == NULL || cmp == NULL) {
90  return -2.0;
91  }
92 
93  if(!ori->isValid() || !cmp->isValid()) {
94  return -4.0;
95  }
96 
97  if(!ori->isSimilarType(cmp)) {
98  return -1.0;
99  }
100 
101  float invGamma = 1.0f / gamma;
102  float exposure = powf(2.0f, fstop);
103 
104  int area = ori->width * ori->height;
105  int size = area * ori->channels;
106 
107  unsigned long long acc = 0;
108 
109  int nValues = (1 << nBit) - 1;
110  float nValuesf = float(nValues);
111 
112  for(int i = 0; i < size; i++) {
113  int oriLDR = int(nValuesf * (powf(ori->data[i] * exposure, invGamma)));
114  int cmpLDR = int(nValuesf * (powf(cmp->data[i] * exposure, invGamma)));
115 
116  oriLDR = CLAMPi(oriLDR, 0, nValues);
117  cmpLDR = CLAMPi(cmpLDR, 0, nValues);
118 
119  int delta = cmpLDR - oriLDR;
120 
121  acc += delta * delta;
122  }
123 
124  return (double(acc) / double(area));
125 }
126 
135 PIC_INLINE double RMSE(Image *ori, Image *cmp, bool bLargeDifferences = false, METRICS_DOMAIN type = MD_LIN)
136 {
137  return sqrt(MSE(ori, cmp, bLargeDifferences, type));
138 }
139 
140 } // end namespace pic
141 
142 #endif /* PIC_METRICS_MSE_HPP */
143 
int size() const
size computes the number of values.
Definition: image.hpp:481
float * data
data is the main buffer where pixel values are stored.
Definition: image.hpp:91
int channels
Definition: image.hpp:80
bool isSimilarType(const Image *img)
isSimilarType checks if the current image is similar to img; i.e. if they have the same width...
float changeDomain(float x, METRICS_DOMAIN type=MD_LIN)
changeDomain
Definition: base.hpp:38
PIC_INLINE double RMSE(Image *ori, Image *cmp, bool bLargeDifferences=false, METRICS_DOMAIN type=MD_LIN)
RMSE computes the root mean squared error (RMSE) between two images.
Definition: mse.hpp:135
PIC_INLINE double MSE(Image *ori, Image *cmp, bool bLargeDifferences=false, METRICS_DOMAIN type=MD_LIN)
MSE computes the mean square error (MSE) between two images.
Definition: mse.hpp:37
#define PIC_INLINE
Definition: base.hpp:33
The Image class stores an image as buffer of float.
Definition: image.hpp:60
#define CLAMPi(x, a, b)
Definition: math.hpp:81
Definition: bilateral_separation.hpp:25
bool isValid()
isValid checks if the current image is valid, which means if they have an allocated buffer or not...
METRICS_DOMAIN
Definition: base.hpp:30
const float C_LARGE_DIFFERENCESf
Definition: base.hpp:28
int width
Definition: image.hpp:80
int height
Definition: image.hpp:80
Definition: base.hpp:30