Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpRetinex.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Convert image types.
32 */
33/* Retinex_.java Using ImageJ Gaussian Filter
34 * Retinex filter algorithm based on the plugin for GIMP.
35 *
36 * @author Jimenez-Hernandez Francisco <jimenezf@fi.uaemex.mx>
37 * Developed at Birmingham University, School of Dentistry. Supervised by
38 * Gabriel Landini
39 * @version 1.0
40 *
41 * 8 July 2010
42 *
43 * This version uses ImageJ Gaussian blurring instead of GIMP's linear
44 *Gaussian because there is a bug in GIMP's implementation that shifts the
45 *results of the blurring to the right of the image when using more than 3
46 *scales.
47 *
48 * Based on:
49 * MSRCR Retinex
50 * (Multi-Scale Retinex with Color Restoration)
51 * 2003 Fabien Pelisson <Fabien.Pelisson@inrialpes.fr>
52 * Retinex GIMP plug-in
53 *
54 * Copyright (C) 2009 MAO Y.B
55 * 2009. 3. 3
56 * Visual Information Processing (VIP) Group, NJUST
57 *
58 * D. J. Jobson, Z. Rahman, and G. A. Woodell. A multi-scale
59 * Retinex for bridging the gap between color images and the
60 * human observation of scenes. IEEE Transactions on Image Processing,
61 * 1997, 6(7): 965-976
62 *
63 * This program is free software; you can redistribute it and/or modify
64 * it under the terms of the GNU General Public License as published by
65 * the Free Software Foundation; either version 2 of the License, or
66 * (at your option) any later version.
67 *
68 * This program is distributed in the hope that it will be useful,
69 * but WITHOUT ANY WARRANTY; without even the implied warranty of
70 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71 * GNU General Public License for more details.
72 *
73 * You should have received a copy of the GNU General Public License
74 * along with this program; if not, write to the Free Software
75 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
76 *
77*/
78
83
84#include <functional>
85#include <numeric>
86
87#include <visp3/core/vpImageFilter.h>
88#include <visp3/core/vpMath.h>
89#include <visp3/imgproc/vpImgproc.h>
90
91#define MAX_RETINEX_SCALES 8
92namespace VISP_NAMESPACE_NAME
93{
94std::vector<double> retinexScalesDistribution(int scaleDiv, int level, int scale)
95{
96 std::vector<double> scales(MAX_RETINEX_SCALES);
97 const int val_2 = 2;
98 if (scaleDiv == 1) {
99 scales[0] = scale / 2.0;
100 }
101 else if (scaleDiv == val_2) {
102 scales[0] = scale / 2.0;
103 scales[1] = scale;
104 }
105 else {
106 double size_step = scale / static_cast<double>(scaleDiv);
107 int i;
108
109 switch (level) {
110 case RETINEX_UNIFORM:
111 for (i = 0; i < scaleDiv; ++i) {
112 scales[static_cast<size_t>(i)] = 2.0 + (i * size_step);
113 }
114 break;
115
116 case RETINEX_LOW:
117 size_step = std::log(scale - 2.0) / static_cast<double>(scaleDiv);
118 for (i = 0; i < scaleDiv; ++i) {
119 scales[static_cast<size_t>(i)] = 2.0 + std::pow(10.0, (i * size_step) / std::log(10.0));
120 }
121 break;
122
123 case RETINEX_HIGH:
124 size_step = std::log(scale - 2.0) / static_cast<double>(scaleDiv);
125 for (i = 0; i < scaleDiv; ++i) {
126 scales[static_cast<size_t>(i)] = scale - std::pow(10.0, (i * size_step) / std::log(10.0));
127 }
128 break;
129
130 default:
131 break;
132 }
133 }
134
135 return scales;
136}
137
138// See: http://imagej.net/Retinex and
139// https://docs.gimp.org/en/plug-in-retinex.html
140void MSRCR(vpImage<vpRGBa> &I, int v_scale, int scaleDiv, int level, double dynamic, int v_kernelSize)
141{
142 // Calculate the scales of filtering according to the number of filter and
143 // their distribution.
144 std::vector<double> retinexScales = retinexScalesDistribution(scaleDiv, level, v_scale);
145
146 // Filtering according to the various scales.
147 // Summarize the results of the various filters according to a specific
148 // weight(here equivalent for all).
149 double weight = 1.0 / static_cast<double>(scaleDiv);
150
151 std::vector<vpImage<double> > doubleRGB(3);
152 std::vector<vpImage<double> > doubleResRGB(3);
153 unsigned int size = I.getSize();
154
155 int kernelSize = v_kernelSize;
156 if (kernelSize == -1) {
157 // Compute the kernel size from the input image size
158 kernelSize = static_cast<int>(std::min<unsigned int>(I.getWidth(), I.getHeight()) / 2.0);
159 const int moduloForOddityCheck = 2;
160 kernelSize = (kernelSize - (kernelSize % moduloForOddityCheck)) + 1;
161 }
162
163 const int nbChannels = 3;
164 const int id0 = 0, id1 = 1, id2 = 2;
165 for (int channel = 0; channel < nbChannels; ++channel) {
166 doubleRGB[static_cast<size_t>(channel)] = vpImage<double>(I.getHeight(), I.getWidth());
167 doubleResRGB[static_cast<size_t>(channel)] = vpImage<double>(I.getHeight(), I.getWidth());
168
169 for (unsigned int cpt = 0; cpt < size; ++cpt) {
170 // Shift the pixel values by 1 to avoid problem with log(0)
171 switch (channel) {
172 case 0:
173 doubleRGB[static_cast<size_t>(channel)].bitmap[cpt] = I.bitmap[cpt].R + 1.0;
174 break;
175
176 case 1:
177 doubleRGB[static_cast<size_t>(channel)].bitmap[cpt] = I.bitmap[cpt].G + 1.0;
178 break;
179
180 case id2:
181 doubleRGB[static_cast<size_t>(channel)].bitmap[cpt] = I.bitmap[cpt].B + 1.0;
182 break;
183
184 default:
185 break;
186 }
187 }
188
189 for (int sc = 0; sc < scaleDiv; ++sc) {
190 vpImage<double> blurImage;
191 double sigma = retinexScales[static_cast<size_t>(sc)];
192 vpImageFilter::gaussianBlur(doubleRGB[static_cast<size_t>(channel)], blurImage, static_cast<unsigned int>(kernelSize), sigma);
193
194 for (unsigned int cpt = 0; cpt < size; ++cpt) {
195 // Summarize the filtered values.
196 // In fact one calculates a ratio between the original values and the
197 // filtered values.
198 doubleResRGB[static_cast<size_t>(channel)].bitmap[cpt] +=
199 weight * (std::log(doubleRGB[static_cast<size_t>(channel)].bitmap[cpt]) - std::log(blurImage.bitmap[cpt]));
200 }
201 }
202 }
203
204 std::vector<double> dest(size * nbChannels);
205 const double gain = 1.0, alpha = 128.0, offset = 0.0;
206
207 for (unsigned int cpt = 0; cpt < size; ++cpt) {
208 double logl = std::log(static_cast<double>(I.bitmap[cpt].R + I.bitmap[cpt].G + I.bitmap[cpt].B + 3.0));
209
210 dest[cpt * nbChannels] = (gain * (std::log(alpha * doubleRGB[id0].bitmap[cpt]) - logl) * doubleResRGB[id0].bitmap[cpt]) + offset;
211 dest[(cpt * nbChannels) + id1] =
212 (gain * (std::log(alpha * doubleRGB[id1].bitmap[cpt]) - logl) * doubleResRGB[id1].bitmap[cpt]) + offset;
213 dest[(cpt * nbChannels) + id2] =
214 (gain * (std::log(alpha * doubleRGB[id2].bitmap[cpt]) - logl) * doubleResRGB[id2].bitmap[cpt]) + offset;
215 }
216
217 double sum = std::accumulate(dest.begin(), dest.end(), 0.0);
218 double mean = sum / dest.size();
219
220 std::vector<double> diff(dest.size());
221
222#if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
223 std::transform(dest.begin(), dest.end(), diff.begin(), std::bind(std::minus<double>(), std::placeholders::_1, mean));
224#else
225 std::transform(dest.begin(), dest.end(), diff.begin(), std::bind2nd(std::minus<double>(), mean));
226#endif
227
228 double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
229 double stdev = std::sqrt(sq_sum / dest.size());
230
231 double mini = mean - (dynamic * stdev);
232 double maxi = mean + (dynamic * stdev);
233 double range = maxi - mini;
234
235 if (vpMath::nul(range)) {
236 range = 1.0;
237 }
238
239 for (unsigned int cpt = 0; cpt < size; ++cpt) {
240 I.bitmap[cpt].R = vpMath::saturate<unsigned char>((255.0 * (dest[(cpt * nbChannels) + id0] - mini)) / range);
241 I.bitmap[cpt].G = vpMath::saturate<unsigned char>((255.0 * (dest[(cpt * nbChannels) + id1] - mini)) / range);
242 I.bitmap[cpt].B = vpMath::saturate<unsigned char>((255.0 * (dest[(cpt * nbChannels) + id2] - mini)) / range);
243 }
244}
245
246void retinex(vpImage<vpRGBa> &I, int scale, int scaleDiv, int level, const double dynamic, int kernelSize)
247{
248 // Assert scale
249 const int minScale = 16, maxScale = 250;
250 if ((scale < minScale) || (scale > maxScale)) {
251 std::cerr << "Scale must be between the interval [" << minScale << " - " << maxScale << "]" << std::endl;
252 return;
253 }
254
255 // Assert scaleDiv
256 const int minScaleDiv = 1, maxScaleDiv = 8;
257 if ((scaleDiv < minScaleDiv) || (scaleDiv > maxScaleDiv)) {
258 std::cerr << "Scale division must be between the interval [" << minScaleDiv << " - " << maxScaleDiv << "]" << std::endl;
259 return;
260 }
261
262 if ((I.getWidth() * I.getHeight()) == 0) {
263 return;
264 }
265
266 MSRCR(I, scale, scaleDiv, level, dynamic, kernelSize);
267}
268
269void retinex(const vpImage<vpRGBa> &I1, vpImage<vpRGBa> &I2, int scale, int scaleDiv, int level, double dynamic,
270 int kernelSize)
271{
272 I2 = I1;
273 retinex(I2, scale, scaleDiv, level, dynamic, kernelSize);
274}
275
276} // namespace
static void gaussianBlur(const vpImage< ImageType > &I, vpImage< OutputType > &GI, unsigned int size=7, FilterType sigma=0., bool normalize=true, const vpImage< bool > *p_mask=nullptr)
Definition of the vpImage class member functions.
Definition vpImage.h:131
Type * bitmap
points toward the bitmap
Definition vpImage.h:135
static Tp saturate(unsigned char v)
Definition vpMath.h:306
static bool nul(double x, double threshold=0.001)
Definition vpMath.h:453
VISP_EXPORT void retinex(VISP_NAMESPACE_ADDRESSING vpImage< VISP_NAMESPACE_ADDRESSING vpRGBa > &I, int scale=240, int scaleDiv=3, int level=RETINEX_UNIFORM, double dynamic=1.2, int kernelSize=-1)
void MSRCR(vpImage< vpRGBa > &I, int v_scale, int scaleDiv, int level, double dynamic, int v_kernelSize)
std::vector< double > retinexScalesDistribution(int scaleDiv, int level, int scale)
Definition vpRetinex.cpp:94
@ RETINEX_HIGH
Enhances the bright regions of the image.
Definition vpImgproc.h:59
@ RETINEX_LOW
Enhances dark regions of the image.
Definition vpImgproc.h:58
@ RETINEX_UNIFORM
Tends to treat all image intensities similarly.
Definition vpImgproc.h:57