Debian Codegraph @

src/filter/ndvi/ndvi.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Brian Matherly ([email protected])
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #ifdef HAVE_CAIRO
19 #include <cairo.h>
20 #endif
21 #include "frei0r.hpp"
22 #include "frei0r/math.h"
23 #include "gradientlut.hpp"
24 #include <string>
25 #include <stdlib.h>
26
27 /**
28 This filter calculates NDVI or VI values from near-infrared + visible video.
29 The index values are mapped to a false color image.
30 */
31
32 static inline double N2P(double ndvi) {
33 // Convert an index value (-1.0 to 1.0) to position (0.0 to 1.0).
34 return (ndvi + 1.0) / 2.0;
35 }
36
37 static unsigned int ColorIndex(const std::string& str) {
38 // Convert a color initial to a component index.
39 if(str == "r" || str == "R") {
40 return 0;
41 } else if(str == "g" || str == "G") {
42 return 1;
43 } else { // "b"
44 return 2;
45 }
46 }
47
48 class Ndvi : public frei0r::filter
49 {
50 public:
51 Ndvi(unsigned int width, unsigned int height);
52 virtual void update(double time,
53 uint32_t* out,
54 const uint32_t* in);
55
56 private:
57 void initLut();
58 double getComponent(uint8_t *sample, unsigned int chan, double offset, double scale);
59 void setColor(uint8_t *sample, double index);
60 void drawLegend(uint32_t *out);
61 void drawRect( uint32_t* out, uint8_t r, uint8_t g, uint8_t b, unsigned int x, unsigned int y, unsigned int w, unsigned int h );
62 void drawGradient( uint32_t* out, unsigned int x, unsigned int y, unsigned int w, unsigned int h );
63 void drawText( uint32_t* out, std::string text, unsigned int x, unsigned int y, unsigned int textHeight );
64
65 double paramLutLevels;
66 std::string paramColorMap;
67 double paramVisScale;
68 double paramVisOffset;
69 double paramNirScale;
70 double paramNirOffset;
71 std::string paramVisChan;
72 std::string paramNirChan;
73 std::string paramIndex;
74 std::string paramLegend;
75 unsigned int lutLevels;
76 std::string colorMap;
77 GradientLut gradient;
78 };
79
80 Ndvi::Ndvi(unsigned int width, unsigned int height)
81 : paramLutLevels(256.0 / 1000.0)
82 , paramColorMap("grayscale")
83 , paramVisScale(0.1)
84 , paramVisOffset(0.5)
85 , paramNirScale(0.1)
86 , paramNirOffset(0.5)
87 , paramVisChan("b")
88 , paramNirChan("r")
89 , paramIndex("ndvi")
90 , paramLegend("off")
91 , lutLevels(0)
92 , colorMap("")
93 , gradient()
94 {
95 register_param(paramColorMap, "Color Map",
96 "The color map to use. One of 'earth', 'grayscale', 'heat' or 'rainbow'.");
97 register_param(paramLutLevels, "Levels",
98 "The number of color levels to use in the false image (divided by 1000).");
99 register_param(paramVisScale, "VIS Scale",
100 "A scaling factor to be applied to the visible component (divided by 10).");
101 register_param(paramVisOffset, "VIS Offset",
102 "An offset to be applied to the visible component (mapped to [-100%, 100%].");
103 register_param(paramNirScale, "NIR Scale",
104 "A scaling factor to be applied to the near-infrared component (divided by 10).");
105 register_param(paramNirOffset, "NIR Offset",
106 "An offset to be applied to the near-infrared component (mapped to [-100%, 100%].");
107 register_param(paramVisChan, "Visible Channel",
108 "The channel to use for the visible component. One of 'r', 'g', or 'b'.");
109 register_param(paramNirChan, "NIR Channel",
110 "The channel to use for the near-infrared component. One of 'r', 'g', or 'b'.");
111 register_param(paramIndex, "Index Calculation",
112 "The index calculation to use. One of 'ndvi' or 'vi'.");
113 register_param(paramLegend, "Legend",
114 "Control legend display. One of 'off' or 'bottom'.");
115 }
116
117 void Ndvi::update(double time,
118 uint32_t* out,
119 const uint32_t* in) {
120 uint8_t *inP = (uint8_t*)in;
121 uint8_t *outP = (uint8_t*)out;
122 double visScale = paramVisScale * 10.0;
123 double visOffset = (paramVisOffset * 510) - 255;
124 double nirScale = paramNirScale * 10.0;
125 double nirOffset = (paramNirOffset * 510) - 255;
126 unsigned int visChan = ColorIndex(paramVisChan);
127 unsigned int nirChan = ColorIndex(paramNirChan);
128
129 initLut();
130
131 if (paramIndex == "vi") {
132 for (unsigned int i = 0; i < size; i++) {
133 double vis = getComponent(inP, visChan, visOffset, visScale);
134 double nir = getComponent(inP, nirChan, nirOffset, nirScale);
135 double vi = (nir - vis) / 255.0;
136 setColor(outP, vi);
137 inP += 4;
138 outP += 4;
139 }
140 } else { // ndvi
141 for (unsigned int i = 0; i < size; i++) {
142 double vis = getComponent(inP, visChan, visOffset, visScale);
143 double nir = getComponent(inP, nirChan, nirOffset, nirScale);
144 double ndvi = (nir - vis) / (nir + vis);
145 setColor(outP, ndvi);
146 inP += 4;
147 outP += 4;
148 }
149 }
150
151 if( paramLegend == "bottom" ) {
152 drawLegend(out);
153 }
154 }
155
156 void Ndvi::initLut() {
157 // Only update the LUT if a parameter has changed.
158 unsigned int paramLutLevelsInt = paramLutLevels * 1000.0 + 0.5;
159 if (paramLutLevelsInt < 2) paramLutLevelsInt = 2;
160 if (paramLutLevelsInt > 1000) paramLutLevelsInt = 1000;
161 if (lutLevels == paramLutLevelsInt &&
162 colorMap == paramColorMap) {
163 return;
164 } else {
165 lutLevels = paramLutLevelsInt;
166 colorMap = paramColorMap;
167 }
168
169 gradient.setDepth(lutLevels);
170
171 if(colorMap == "earth") {
172 GradientLut::Color water = {0x30, 0x70, 0xd0};
173 GradientLut::Color desert = {0xd0, 0xc0, 0x90};
174 GradientLut::Color grass = {0x00, 0xc0, 0x20};
175 GradientLut::Color forrest = {0x00, 0x30, 0x00};
176 gradient.fillRange( N2P(-1.0), water, N2P(-0.2), water );
177 gradient.fillRange( N2P(-0.2), water, N2P(-0.1), desert );
178 gradient.fillRange( N2P(-0.1), desert, N2P( 0.1), desert );
179 gradient.fillRange( N2P( 0.1), desert, N2P( 0.4), grass );
180 gradient.fillRange( N2P( 0.4), grass, N2P( 1.0), forrest );
181 } else if(colorMap == "heat") {
182 GradientLut::Color n10 = {0x00, 0x00, 0x00};
183 GradientLut::Color n08 = {0x10, 0x10, 0x70};
184 GradientLut::Color n06 = {0x10, 0x20, 0xf0};
185 GradientLut::Color n04 = {0x10, 0x60, 0xf0};
186 GradientLut::Color n02 = {0x20, 0xa0, 0xc0};
187 GradientLut::Color zer = {0x20, 0xb0, 0x20};
188 GradientLut::Color p02 = {0x90, 0xf0, 0x10};
189 GradientLut::Color p04 = {0xf0, 0xb0, 0x10};
190 GradientLut::Color p06 = {0xf0, 0xa0, 0x10};
191 GradientLut::Color p08 = {0xf0, 0x50, 0x10};
192 GradientLut::Color p10 = {0xff, 0x00, 0x00};
193 gradient.fillRange( N2P(-1.0), n10, N2P(-0.8), n08 );
194 gradient.fillRange( N2P(-0.8), n08, N2P(-0.6), n06 );
195 gradient.fillRange( N2P(-0.6), n06, N2P(-0.4), n04 );
196 gradient.fillRange( N2P(-0.4), n04, N2P(-0.2), n02 );
197 gradient.fillRange( N2P(-0.2), n02, N2P( 0.0), zer );
198 gradient.fillRange( N2P( 0.0), zer, N2P( 0.2), p02 );
199 gradient.fillRange( N2P( 0.2), p02, N2P( 0.4), p04 );
200 gradient.fillRange( N2P( 0.4), p04, N2P( 0.6), p06 );
201 gradient.fillRange( N2P( 0.6), p06, N2P( 0.8), p08 );
202 gradient.fillRange( N2P( 0.8), p08, N2P( 1.0), p10 );
203 } else if(colorMap == "rainbow") {
204 GradientLut::Color violet = {0x7f, 0x00, 0xff};
205 GradientLut::Color blue = {0x00, 0x00, 0xff};
206 GradientLut::Color green = {0x00, 0xff, 0x00};
207 GradientLut::Color yellow = {0xff, 0xff, 0x00};
208 GradientLut::Color orange = {0xff, 0x7f, 0x00};
209 GradientLut::Color red = {0xff, 0x00, 0x00};
210 gradient.fillRange( N2P(-1.0), violet, N2P(-0.6), blue );
211 gradient.fillRange( N2P(-0.6), blue, N2P(-0.2), green );
212 gradient.fillRange( N2P(-0.2), green, N2P( 0.2), yellow );
213 gradient.fillRange( N2P( 0.2), yellow, N2P( 0.6), orange );
214 gradient.fillRange( N2P( 0.6), orange, N2P( 1.0), red );
215 } else { // grayscale
216 GradientLut::Color black = {0x00, 0x00, 0x00};
217 GradientLut::Color white = {0xff, 0xff, 0xff};
218 gradient.fillRange( N2P(-1.0), black, N2P( 1.0), white );
219 }
220 }
221
222 inline double Ndvi::getComponent(uint8_t *sample, unsigned int chan, double offset, double scale)
223 {
224 double c = sample[chan];
225 c = (c + offset) * scale;
226 c = CLAMP(c, 0.0, 255.0);
227 return c;
228 }
229
230 inline void Ndvi::setColor(uint8_t *sample, double index)
231 {
232 double pos = N2P(index);
233 const GradientLut::Color& falseColor = gradient[pos];
234 sample[0] = falseColor.r;
235 sample[1] = falseColor.g;
236 sample[2] = falseColor.b;
237 sample[3] = 0xff;
238 }
239
240 void Ndvi::drawLegend(uint32_t* out)
241 {
242 unsigned int legendHeight = height / 20;
243
244 // Black border above legend
245 unsigned int borderHeight = legendHeight / 15;
246 unsigned int borderY = height - legendHeight;
247 drawRect( out, 0, 0, 0, 0, borderY, width, borderHeight );
248
249 // Gradient
250 unsigned int gradientHeight = legendHeight - borderHeight;
251 unsigned int gradientY = height - gradientHeight;
252 drawGradient( out, 0, gradientY, width, gradientHeight );
253
254 // Text
255 unsigned int textHeight = gradientHeight * 8 / 10;
256 unsigned int textY = height - ( gradientHeight - textHeight ) / 2;
257 unsigned int textX = width / 25;
258 if (paramIndex == "vi") {
259 drawText( out, "0", textX, textY , textHeight );
260 drawText( out, "VI", width / 2, textY , textHeight );
261 drawText( out, "1", width - textX, textY , textHeight );
262 } else { // ndvi
263 drawText( out, "-1", textX, textY , textHeight );
264 drawText( out, "NDVI", width / 2, textY , textHeight );
265 drawText( out, "1", width - textX, textY , textHeight );
266 }
267 }
268
269 void Ndvi::drawRect( uint32_t* out, uint8_t r, uint8_t g, uint8_t b, unsigned int x, unsigned int y, unsigned int w, unsigned int h )
270 {
271 for (unsigned int i = 0; i < h; i++) {
272 uint8_t *sample = (uint8_t*)(out + ((i + y) * width) + x);
273 for (unsigned int j = 0; j < w; j++) {
274 sample[0] = r;
275 sample[1] = g;
276 sample[2] = b;
277 sample += 4;
278 }
279 }
280 }
281
282 void Ndvi::drawGradient( uint32_t* out, unsigned int x, unsigned int y, unsigned int w, unsigned int h )
283 {
284 for (unsigned int i = 0; i < w; i++) {
285 double pos = (double)i / (double)w;
286 const GradientLut::Color& falseColor = gradient[pos];
287 uint8_t *sample = (uint8_t*)(out + (y * width) + x + i);
288 for (unsigned int j = 0; j < h; j++) {
289 sample[0] = falseColor.r;
290 sample[1] = falseColor.g;
291 sample[2] = falseColor.b;
292 sample += width * 4;
293 }
294 }
295 }
296
297 void Ndvi::drawText( uint32_t* out, std::string text, unsigned int x, unsigned int y, unsigned int textHeight )
298 {
299 #ifdef HAVE_CAIRO
300 int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, width);
301 cairo_surface_t* surface = cairo_image_surface_create_for_data((unsigned char*)out,
302 CAIRO_FORMAT_ARGB32,
303 width,
304 height,
305 stride);
306 cairo_t *cr = cairo_create(surface);
307 cairo_text_extents_t te;
308 cairo_font_extents_t fe;
309
310 cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
311 cairo_set_font_size(cr, textHeight);
312 cairo_text_extents(cr, text.c_str(), &te);
313 cairo_font_extents(cr, &fe);
314
315 // Center text on x
316 x -= te.width / 2;
317 // Align bottom of text on y (assume no characters go below baseline)
318 y -= (fe.height - fe.ascent + 1) / 2;
319
320 cairo_move_to(cr, x, y);
321 cairo_text_path(cr, text.c_str());
322 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
323 cairo_fill_preserve(cr);
324 cairo_set_source_rgb(cr, 0, 0, 0);
325 cairo_set_line_width(cr, textHeight / 20);
326 cairo_stroke (cr);
327 cairo_surface_destroy (surface);
328 cairo_destroy(cr);
329 #endif
330 }
331
332 frei0r::construct<Ndvi> plugin("NDVI filter",
333 "This filter creates a false image from a visible + infrared source.",
334 "Brian Matherly",
335 0,2,
336 F0R_COLOR_MODEL_RGBA8888);