OTB  10.0.0
Orfeo Toolbox
otbMosaicFunctors.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2024 Centre National d'Etudes Spatiales (CNES)
4  * Copyright (C) 2016-2019 IRSTEA
5  *
6  * This file is part of Orfeo Toolbox
7  *
8  * https://www.orfeo-toolbox.org/
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #ifndef MODULES_REMOTE_MOSAIC_INCLUDE_OTBMOSAICFUNCTORS_H_
24 #define MODULES_REMOTE_MOSAIC_INCLUDE_OTBMOSAICFUNCTORS_H_
25 
26 #include <cmath>
27 #include <array>
28 #include "vnl/vnl_matrix.h"
29 #include "vcl_legacy_aliases.h"
30 #include "vcl_compiler.h"
31 
32 namespace otb
33 {
34 
35 namespace Functor
36 {
37 
44 template <class TInput, class TOutput>
45 class RGB2LAB
46 {
47 public:
49  {
50  M.set_size(3, 3);
51  M[0][0] = 0.3811;
52  M[0][1] = 0.5783;
53  M[0][2] = 0.0406;
54  M[1][0] = 0.1967;
55  M[1][1] = 0.7244;
56  M[1][2] = 0.0790;
57  M[2][0] = 0.0241;
58  M[2][1] = 0.1288;
59  M[2][2] = 0.8531;
61 
62  D1.set_size(3, 3);
63  D1.fill(0.0);
64  D1[0][0] = 1.0 / vcl_sqrt(3.0);
65  D1[1][1] = 1.0 / vcl_sqrt(6.0);
66  D1[2][2] = 1.0 / vcl_sqrt(2.0);
67 
68  D2.set_size(3, 3);
69  D2.fill(1.0);
70  D2[1][2] = -2.0;
71  D2[2][1] = -1.0;
72  D2[2][2] = 0.0;
73  }
74 
76  {
77  }
78 
79  bool operator!=(const RGB2LAB&) const
80  {
81  return false;
82  }
83 
84  bool operator==(const RGB2LAB& other) const
85  {
86  return !(*this != other);
87  }
88 
89  inline TOutput operator()(const TInput& A) const
90  {
91  TOutput output;
92 
93  output.SetSize(3);
94  if (A[0] == 0 && A[1] == 0 && A[2] == 0)
95  {
96  output.Fill(0);
97  return output;
98  }
99 
100  // RGB
101  vnl_matrix<double> rgb(3, 1);
102  rgb[0][0] = A[0];
103  rgb[1][0] = A[1];
104  rgb[2][0] = A[2];
105 
106  // LMS
107  vnl_matrix<double> lms(3, 1);
108  lms = M * rgb;
109 
110  // LMS (log10)
111  const double log10 = vcl_log(10);
112  lms[0][0] = vcl_log(lms[0][0]) / log10;
113  lms[1][0] = vcl_log(lms[1][0]) / log10;
114  lms[2][0] = vcl_log(lms[2][0]) / log10;
115 
116  // LAB
117  vnl_matrix<double> lab(3, 1);
118  lab = D1 * (D2 * lms);
119 
120  output[0] = lab[0][0];
121  output[1] = lab[1][0];
122  output[2] = lab[2][0];
123 
124  return output;
125  }
126 
127  size_t OutputSize(const std::array<size_t, 1>&) const
128  {
129  return 3;
130  }
131 
132 private:
133  vnl_matrix<double> M;
134  vnl_matrix<double> D1;
135  vnl_matrix<double> D2;
136 };
137 
146 template <class TInput, class TOutput>
147 class LAB2RGB
148 {
149 public:
151  {
152  M.set_size(3, 3);
153  M[0][0] = 4.4687;
154  M[0][1] = -3.5887;
155  M[0][2] = 0.1197;
156  M[1][0] = -1.2197;
157  M[1][1] = 2.3831;
158  M[1][2] = -0.1626;
159  M[2][0] = 0.0579;
160  M[2][1] = -0.2584;
161  M[2][2] = 1.1934;
163 
164  D1.set_size(3, 3);
165  D1.fill(0.0);
166  D1[0][0] = 1.0 / vcl_sqrt(3.0);
167  D1[1][1] = 1.0 / vcl_sqrt(6.0);
168  D1[2][2] = 1.0 / vcl_sqrt(2.0);
169 
170  D2.set_size(3, 3);
171  D2.fill(1.0);
172  D2[1][2] = -1.0;
173  D2[2][1] = -2.0;
174  D2[2][2] = 0.0;
175  }
176 
178  {
179  }
180 
181  bool operator!=(const LAB2RGB&) const
182  {
183  return false;
184  }
185 
186  bool operator==(const LAB2RGB& other) const
187  {
188  return !(*this != other);
189  }
190 
191  inline TOutput operator()(const TInput& A) const
192  {
193  TOutput output;
194 
195  output.SetSize(3);
196 
197  if (A[0] == 0 && A[1] == 0 && A[2] == 0)
198  {
199  output.Fill(0);
200  return output;
201  }
202  // LAB
203  vnl_matrix<double> lab(3, 1);
204  lab[0][0] = A[0];
205  lab[1][0] = A[1];
206  lab[2][0] = A[2];
207 
208  // LMS
209  vnl_matrix<double> lms(3, 1);
210  lms = D2 * (D1 * lab);
211  lms[0][0] = vcl_pow(10.0, lms[0][0]);
212  lms[1][0] = vcl_pow(10.0, lms[1][0]);
213  lms[2][0] = vcl_pow(10.0, lms[2][0]);
214 
215  // RGB
216  vnl_matrix<double> rgb(3, 1);
217  rgb = M * lms;
218 
219  output[0] = rgb[0][0];
220  output[1] = rgb[1][0];
221  output[2] = rgb[2][0];
222 
223  return output;
224  }
225 
226  inline size_t OutputSize(const std::array<size_t, 1>&) const
227  {
228  return 3;
229  }
230 
231 private:
232  vnl_matrix<double> M;
233  vnl_matrix<double> D1;
234  vnl_matrix<double> D2;
235 };
236 
246 template <class TInput, class TOutput>
247 class IsNoData
248 {
249 public:
250  IsNoData(const typename TInput::ValueType input_nodata = 0): nodata(input_nodata){};
251 
252  ~IsNoData() = default;
253 
254  TOutput operator()(const TInput& A) const
255  {
256  for (unsigned int i = 0; i < A.Size(); i++)
257  {
258  if (!std::isnan(A[i]) && A[i] != nodata)
259  {
260  return 0;
261  }
262  }
263  return 255;
264  }
265 
266 private:
267  typename TInput::ValueType nodata = 0;
268 };
269 
270 } // namespace functor
271 } // namespace otb
272 #endif /* MODULES_REMOTE_MOSAIC_INCLUDE_OTBMOSAICFUNCTORS_H_ */
IsNoData(const typename TInput::ValueType input_nodata=0)
TOutput operator()(const TInput &A) const
TInput::ValueType nodata
Base class for converting LAB into RGB color space (Ruderman et al.)
vcl_size_t OutputSize(const std::array< vcl_size_t, 1 > &) const
bool operator!=(const LAB2RGB &) const
vnl_matrix< double > M
bool operator==(const LAB2RGB &other) const
TOutput operator()(const TInput &A) const
vnl_matrix< double > D2
vnl_matrix< double > D1
Base class for converting RGB into LAB color space (Ruderman et al.)
vnl_matrix< double > D1
vnl_matrix< double > M
vnl_matrix< double > D2
vcl_size_t OutputSize(const std::array< vcl_size_t, 1 > &) const
bool operator!=(const RGB2LAB &) const
TOutput operator()(const TInput &A) const
bool operator==(const RGB2LAB &other) const
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.