OTB  9.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-2022 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 <array>
27 #include "vnl/vnl_matrix.h"
28 #include "vcl_compiler.h"
29 
30 namespace otb
31 {
32 
33 namespace Functor
34 {
35 
42 template <class TInput, class TOutput>
43 class RGB2LAB
44 {
45 public:
47  {
48  M.set_size(3, 3);
49  M[0][0] = 0.3811;
50  M[0][1] = 0.5783;
51  M[0][2] = 0.0406;
52  M[1][0] = 0.1967;
53  M[1][1] = 0.7244;
54  M[1][2] = 0.0790;
55  M[2][0] = 0.0241;
56  M[2][1] = 0.1288;
57  M[2][2] = 0.8531;
59 
60  D1.set_size(3, 3);
61  D1.fill(0.0);
62  D1[0][0] = 1.0 / vcl_sqrt(3.0);
63  D1[1][1] = 1.0 / vcl_sqrt(6.0);
64  D1[2][2] = 1.0 / vcl_sqrt(2.0);
65 
66  D2.set_size(3, 3);
67  D2.fill(1.0);
68  D2[1][2] = -2.0;
69  D2[2][1] = -1.0;
70  D2[2][2] = 0.0;
71  }
72 
74  {
75  }
76 
77  bool operator!=(const RGB2LAB&) const
78  {
79  return false;
80  }
81 
82  bool operator==(const RGB2LAB& other) const
83  {
84  return !(*this != other);
85  }
86 
87  inline TOutput operator()(const TInput& A) const
88  {
89  TOutput output;
90 
91  output.SetSize(3);
92  if (A[0] == 0 && A[1] == 0 && A[2] == 0)
93  {
94  output.Fill(0);
95  return output;
96  }
97 
98  // RGB
99  vnl_matrix<double> rgb(3, 1);
100  rgb[0][0] = A[0];
101  rgb[1][0] = A[1];
102  rgb[2][0] = A[2];
103 
104  // LMS
105  vnl_matrix<double> lms(3, 1);
106  lms = M * rgb;
107 
108  // LMS (log10)
109  const double log10 = vcl_log(10);
110  lms[0][0] = vcl_log(lms[0][0]) / log10;
111  lms[1][0] = vcl_log(lms[1][0]) / log10;
112  lms[2][0] = vcl_log(lms[2][0]) / log10;
113 
114  // LAB
115  vnl_matrix<double> lab(3, 1);
116  lab = D1 * (D2 * lms);
117 
118  output[0] = lab[0][0];
119  output[1] = lab[1][0];
120  output[2] = lab[2][0];
121 
122  return output;
123  }
124 
125  size_t OutputSize(const std::array<size_t, 1>&) const
126  {
127  return 3;
128  }
129 
130 private:
131  vnl_matrix<double> M;
132  vnl_matrix<double> D1;
133  vnl_matrix<double> D2;
134 };
135 
144 template <class TInput, class TOutput>
145 class LAB2RGB
146 {
147 public:
149  {
150  M.set_size(3, 3);
151  M[0][0] = 4.4687;
152  M[0][1] = -3.5887;
153  M[0][2] = 0.1197;
154  M[1][0] = -1.2197;
155  M[1][1] = 2.3831;
156  M[1][2] = -0.1626;
157  M[2][0] = 0.0579;
158  M[2][1] = -0.2584;
159  M[2][2] = 1.1934;
161 
162  D1.set_size(3, 3);
163  D1.fill(0.0);
164  D1[0][0] = 1.0 / vcl_sqrt(3.0);
165  D1[1][1] = 1.0 / vcl_sqrt(6.0);
166  D1[2][2] = 1.0 / vcl_sqrt(2.0);
167 
168  D2.set_size(3, 3);
169  D2.fill(1.0);
170  D2[1][2] = -1.0;
171  D2[2][1] = -2.0;
172  D2[2][2] = 0.0;
173  }
174 
176  {
177  }
178 
179  bool operator!=(const LAB2RGB&) const
180  {
181  return false;
182  }
183 
184  bool operator==(const LAB2RGB& other) const
185  {
186  return !(*this != other);
187  }
188 
189  inline TOutput operator()(const TInput& A) const
190  {
191  TOutput output;
192 
193  output.SetSize(3);
194 
195  if (A[0] == 0 && A[1] == 0 && A[2] == 0)
196  {
197  output.Fill(0);
198  return output;
199  }
200  // LAB
201  vnl_matrix<double> lab(3, 1);
202  lab[0][0] = A[0];
203  lab[1][0] = A[1];
204  lab[2][0] = A[2];
205 
206  // LMS
207  vnl_matrix<double> lms(3, 1);
208  lms = D2 * (D1 * lab);
209  lms[0][0] = vcl_pow(10.0, lms[0][0]);
210  lms[1][0] = vcl_pow(10.0, lms[1][0]);
211  lms[2][0] = vcl_pow(10.0, lms[2][0]);
212 
213  // RGB
214  vnl_matrix<double> rgb(3, 1);
215  rgb = M * lms;
216 
217  output[0] = rgb[0][0];
218  output[1] = rgb[1][0];
219  output[2] = rgb[2][0];
220 
221  return output;
222  }
223 
224  inline size_t OutputSize(const std::array<size_t, 1>&) const
225  {
226  return 3;
227  }
228 
229 private:
230  vnl_matrix<double> M;
231  vnl_matrix<double> D1;
232  vnl_matrix<double> D2;
233 };
234 
235 } // namespace functor
236 } // namespace otb
237 #endif /* MODULES_REMOTE_MOSAIC_INCLUDE_OTBMOSAICFUNCTORS_H_ */
otb::Functor::RGB2LAB::M
vnl_matrix< double > M
Definition: otbMosaicFunctors.h:131
otb::Functor::RGB2LAB::~RGB2LAB
~RGB2LAB()
Definition: otbMosaicFunctors.h:73
otb::Functor::RGB2LAB::RGB2LAB
RGB2LAB()
Definition: otbMosaicFunctors.h:46
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::LAB2RGB::LAB2RGB
LAB2RGB()
Definition: otbMosaicFunctors.h:148
otb::Functor::RGB2LAB::OutputSize
vcl_size_t OutputSize(const std::array< vcl_size_t, 1 > &) const
Definition: otbMosaicFunctors.h:125
otb::Functor::RGB2LAB::D1
vnl_matrix< double > D1
Definition: otbMosaicFunctors.h:132
otb::Functor::LAB2RGB::OutputSize
vcl_size_t OutputSize(const std::array< vcl_size_t, 1 > &) const
Definition: otbMosaicFunctors.h:224
otb::Functor::LAB2RGB::M
vnl_matrix< double > M
Definition: otbMosaicFunctors.h:230
otb::Functor::RGB2LAB::D2
vnl_matrix< double > D2
Definition: otbMosaicFunctors.h:133
otb::Functor::RGB2LAB::operator()
TOutput operator()(const TInput &A) const
Definition: otbMosaicFunctors.h:87
otb::Functor::LAB2RGB::D2
vnl_matrix< double > D2
Definition: otbMosaicFunctors.h:232
otb::Functor::LAB2RGB
Base class for converting LAB into RGB color space (Ruderman et al.)
Definition: otbMosaicFunctors.h:145
otb::Functor::RGB2LAB
Base class for converting RGB into LAB color space (Ruderman et al.)
Definition: otbMosaicFunctors.h:43
otb::Functor::LAB2RGB::operator==
bool operator==(const LAB2RGB &other) const
Definition: otbMosaicFunctors.h:184
otb::Functor::RGB2LAB::operator!=
bool operator!=(const RGB2LAB &) const
Definition: otbMosaicFunctors.h:77
otb::Functor::RGB2LAB::operator==
bool operator==(const RGB2LAB &other) const
Definition: otbMosaicFunctors.h:82
otb::Functor::LAB2RGB::operator!=
bool operator!=(const LAB2RGB &) const
Definition: otbMosaicFunctors.h:179
otb::Functor::LAB2RGB::~LAB2RGB
~LAB2RGB()
Definition: otbMosaicFunctors.h:175
otb::Functor::LAB2RGB::D1
vnl_matrix< double > D1
Definition: otbMosaicFunctors.h:231
otb::Functor::LAB2RGB::operator()
TOutput operator()(const TInput &A) const
Definition: otbMosaicFunctors.h:189