~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/imagery/iclass_statistics.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
   \file lib/imagery/iclass_statistics.c
 
3
 
 
4
   \brief Imagery library - functions for wx.iclass
 
5
 
 
6
   Computation based on training areas for supervised classification.
 
7
   Based on i.class module (GRASS 6).
 
8
 
 
9
   Computing statistical values (mean, min, max, ...) from given area
 
10
   perimeters for each band.
 
11
 
 
12
   Copyright (C) 1999-2007, 2011 by the GRASS Development Team
 
13
 
 
14
   This program is free software under the GNU General Public License
 
15
   (>=v2).  Read the file COPYING that comes with GRASS for details.
 
16
 
 
17
   \author David Satnik, Central Washington University (original author)
 
18
   \author Markus Neteler <neteler itc.it> (i.class module)
 
19
   \author Bernhard Reiter <bernhard intevation.de> (i.class module)
 
20
   \author Brad Douglas <rez touchofmadness.com>(i.class module)
 
21
   \author Glynn Clements <glynn gclements.plus.com> (i.class module)
 
22
   \author Hamish Bowman <hamish_b yahoo.com> (i.class module)
 
23
   \author Jan-Oliver Wagner <jan intevation.de> (i.class module)
 
24
   \author Anna Kratochvilova <kratochanna gmail.com> (rewriting for wx.iclass)
 
25
   \author Vaclav Petras <wenzeslaus gmail.com> (rewriting for wx.iclass)
 
26
 */
 
27
 
 
28
#include <math.h>
 
29
 
 
30
#include <grass/imagery.h>
 
31
#include <grass/glocale.h>
 
32
#include <grass/colors.h>
 
33
 
 
34
#include "iclass_local_proto.h"
 
35
 
 
36
/*!
 
37
   \brief Initialize statistics.
 
38
 
 
39
   \param[out] statistics pointer to statistics structure
 
40
   \param category category (class)
 
41
   \param name class name
 
42
   \param color class color
 
43
   \param nstd standard deviation
 
44
 */
 
45
void I_iclass_init_statistics(IClass_statistics * statistics, int category,
 
46
                              const char *name, const char *color, float nstd)
 
47
{
 
48
    G_debug(4, "init_statistics() category=%d, name=%s, color=%s, nstd=%f",
 
49
            category, name, color, nstd);
 
50
 
 
51
    statistics->cat = category;
 
52
    statistics->name = G_store(name);
 
53
    statistics->color = G_store(color);
 
54
    statistics->nstd = nstd;
 
55
 
 
56
    statistics->ncells = 0;
 
57
    statistics->nbands = 0;
 
58
 
 
59
    statistics->band_min = NULL;
 
60
    statistics->band_max = NULL;
 
61
    statistics->band_sum = NULL;
 
62
    statistics->band_mean = NULL;
 
63
    statistics->band_stddev = NULL;
 
64
    statistics->band_product = NULL;
 
65
    statistics->band_histo = NULL;
 
66
    statistics->band_range_min = NULL;
 
67
    statistics->band_range_max = NULL;
 
68
}
 
69
 
 
70
/*!
 
71
   \brief Allocate space for statistics.
 
72
 
 
73
   \param statistics pointer to statistics structure
 
74
   \param nbands number of band files
 
75
 */
 
76
void alloc_statistics(IClass_statistics * statistics, int nbands)
 
77
{
 
78
    int i;
 
79
 
 
80
    G_debug(4, "alloc_statistics()");
 
81
 
 
82
    statistics->nbands = nbands;
 
83
 
 
84
    statistics->band_min = (int *)G_calloc(nbands, sizeof(int));
 
85
    statistics->band_max = (int *)G_calloc(nbands, sizeof(int));
 
86
    statistics->band_sum = (float *)G_calloc(nbands, sizeof(float));
 
87
    statistics->band_mean = (float *)G_calloc(nbands, sizeof(float));
 
88
    statistics->band_stddev = (float *)G_calloc(nbands, sizeof(float));
 
89
    statistics->band_product = (float **)G_calloc(nbands, sizeof(float *));
 
90
    statistics->band_histo = (int **)G_calloc(nbands, sizeof(int *));
 
91
    statistics->band_range_min = (int *)G_calloc(nbands, sizeof(int));
 
92
    statistics->band_range_max = (int *)G_calloc(nbands, sizeof(int));
 
93
 
 
94
    for (i = 0; i < nbands; i++) {
 
95
        statistics->band_product[i] =
 
96
            (float *)G_calloc(nbands, sizeof(float));
 
97
        statistics->band_histo[i] = (int *)G_calloc(MAX_CATS, sizeof(int));
 
98
    }
 
99
}
 
100
 
 
101
/*!
 
102
   \brief Free space allocated for statistics attributes.
 
103
 
 
104
   Frees all allocated arrays in statistics structure.
 
105
 
 
106
   \param statistics pointer to statistics structure
 
107
 */
 
108
void I_iclass_free_statistics(IClass_statistics * statistics)
 
109
{
 
110
    int i;
 
111
 
 
112
    G_debug(4, "free_statistics()");
 
113
 
 
114
    G_free((char *) statistics->name);
 
115
    G_free((char *) statistics->color);
 
116
    G_free(statistics->band_min);
 
117
    G_free(statistics->band_max);
 
118
    G_free(statistics->band_sum);
 
119
    G_free(statistics->band_mean);
 
120
    G_free(statistics->band_stddev);
 
121
    G_free(statistics->band_range_max);
 
122
    G_free(statistics->band_range_min);
 
123
 
 
124
 
 
125
    for (i = 0; i < statistics->nbands; i++) {
 
126
        G_free(statistics->band_histo[i]);
 
127
        G_free(statistics->band_product[i]);
 
128
    }
 
129
    G_free(statistics->band_histo);
 
130
    G_free(statistics->band_product);
 
131
}
 
132
 
 
133
/*!
 
134
   \brief Calculate statistics for all training areas.
 
135
 
 
136
   \param statistics pointer to statistics structure
 
137
   \param perimeters list of all area perimeters
 
138
   \param band_buffer buffer to read band rows into
 
139
   \param band_fd band files descriptors
 
140
 
 
141
   \return 1 on succes
 
142
   \return 0 on failure
 
143
 */
 
144
int make_all_statistics(IClass_statistics * statistics,
 
145
                        IClass_perimeter_list * perimeters,
 
146
                        CELL ** band_buffer, int *band_fd)
 
147
{
 
148
    int i, b, b2, nbands;
 
149
 
 
150
    float mean_value, stddev_value;
 
151
 
 
152
    G_debug(5, "make_all_statistics()");
 
153
 
 
154
    nbands = statistics->nbands;
 
155
    for (b = 0; b < nbands; b++) {
 
156
        statistics->band_sum[b] = 0.0;
 
157
        statistics->band_min[b] = MAX_CATS;
 
158
        statistics->band_max[b] = 0;
 
159
        for (b2 = 0; b2 < nbands; b2++)
 
160
            statistics->band_product[b][b2] = 0.0;
 
161
        for (b2 = 0; b2 < MAX_CATS; b2++)
 
162
            statistics->band_histo[b][b2] = 0;
 
163
    }
 
164
 
 
165
    for (i = 0; i < perimeters->nperimeters; i++) {
 
166
        if (!make_statistics
 
167
            (statistics, &perimeters->perimeters[i], band_buffer, band_fd)) {
 
168
            return 0;
 
169
        }
 
170
    }
 
171
    for (b = 0; b < statistics->nbands; b++) {
 
172
        mean_value = mean(statistics, b);
 
173
        stddev_value = stddev(statistics, b);
 
174
 
 
175
        statistics->band_stddev[b] = stddev_value;
 
176
        statistics->band_mean[b] = mean_value;
 
177
 
 
178
        band_range(statistics, b);
 
179
    }
 
180
 
 
181
    return 1;
 
182
}
 
183
 
 
184
/*!
 
185
   \brief Calculate statistics for one training area.
 
186
 
 
187
   \param[out] statistics pointer to statistics structure
 
188
   \param perimeter area perimeter
 
189
   \param band_buffer buffer to read band rows into
 
190
   \param band_fd band files descriptors
 
191
 
 
192
   \return 1 on succes
 
193
   \return 0 on failure
 
194
 */
 
195
int make_statistics(IClass_statistics * statistics,
 
196
                    IClass_perimeter * perimeter, CELL ** band_buffer,
 
197
                    int *band_fd)
 
198
{
 
199
    int b, b2;
 
200
 
 
201
    int value;
 
202
 
 
203
    int i;
 
204
 
 
205
    int x0, x1;
 
206
 
 
207
    int x, y;
 
208
 
 
209
    int ncells;
 
210
 
 
211
    int nbands;
 
212
 
 
213
    G_debug(5, "make_statistics()");
 
214
 
 
215
    nbands = statistics->nbands;
 
216
 
 
217
    if (perimeter->npoints % 2) {
 
218
        G_warning(_("prepare_signature: outline has odd number of points."));
 
219
        return 0;
 
220
    }
 
221
 
 
222
    ncells = 0;
 
223
 
 
224
    for (i = 1; i < perimeter->npoints; i += 2) {
 
225
        y = perimeter->points[i].y;
 
226
        if (y != perimeter->points[i - 1].y) {
 
227
            G_warning(_("prepare_signature: scan line %d has odd number of points."),
 
228
                      (i + 1) / 2);
 
229
            return 0;
 
230
        }
 
231
        read_band_row(band_buffer, band_fd, nbands, y);
 
232
 
 
233
        x0 = perimeter->points[i - 1].x - 1;
 
234
        x1 = perimeter->points[i].x - 1;
 
235
 
 
236
        if (x0 > x1) {
 
237
            G_warning(_("signature: perimeter points out of order."));
 
238
            return 0;
 
239
        }
 
240
 
 
241
        for (x = x0; x <= x1; x++) {
 
242
            ncells++;           /* count interior points */
 
243
            for (b = 0; b < nbands; b++) {
 
244
                value = band_buffer[b][x];
 
245
                G_debug(5, "make_statistics() band: %d, read value: %d (max: %d)",
 
246
                        b, value, MAX_CATS);
 
247
                if (value < 0 || value > MAX_CATS - 1) {
 
248
                    G_warning(_("Data error preparing signatures: value (%d) > num of cats (%d)"),
 
249
                              value, MAX_CATS);
 
250
                    return 0;
 
251
                }
 
252
                statistics->band_sum[b] += value;       /* sum for means */
 
253
                statistics->band_histo[b][value]++;     /* histogram */
 
254
                if (statistics->band_min[b] > value)
 
255
                    statistics->band_min[b] = value;    /* absolute min, max */
 
256
                if (statistics->band_max[b] < value) {
 
257
                    statistics->band_max[b] = value;
 
258
                    G_debug(5,
 
259
                            "make_statistics() statistics->band_max[%d]: %d",
 
260
                            b, statistics->band_max[b]);
 
261
                }
 
262
 
 
263
                for (b2 = 0; b2 <= b; b2++)     /* products for variance */
 
264
                    statistics->band_product[b][b2] +=
 
265
                        value * band_buffer[b2][x];
 
266
            }
 
267
        }
 
268
    }
 
269
    statistics->ncells += ncells;
 
270
 
 
271
    return 1;
 
272
}
 
273
 
 
274
/*!
 
275
   \brief Create raster map based on statistics.
 
276
 
 
277
   \param statistics pointer to statistics structure
 
278
   \param band_buffer buffer to read band rows into
 
279
   \param band_fd band files descriptors
 
280
   \param raster_name name of new raster map
 
281
 */
 
282
void create_raster(IClass_statistics * statistics, CELL ** band_buffer,
 
283
                   int *band_fd, const char *raster_name)
 
284
{
 
285
    int fd;
 
286
 
 
287
    CELL *buffer;
 
288
 
 
289
    int n;
 
290
 
 
291
    int col;
 
292
 
 
293
    int nbands;
 
294
 
 
295
    int row, nrows, ncols;
 
296
 
 
297
    struct Colors raster_colors;
 
298
 
 
299
    int r, g, b;
 
300
 
 
301
    int cell_in_ranges;
 
302
 
 
303
    nbands = statistics->nbands;
 
304
 
 
305
    /* build new raster based on current signature and Nstd */
 
306
 
 
307
    fd = Rast_open_c_new(raster_name);
 
308
    buffer = Rast_allocate_c_buf();
 
309
    nrows = Rast_window_rows();
 
310
    ncols = Rast_window_cols();
 
311
 
 
312
    for (row = 0; row < nrows; row++) {
 
313
        read_band_row(band_buffer, band_fd, nbands, row);
 
314
        for (col = 0; col < ncols; col++) {
 
315
            buffer[col] = (CELL) 0;
 
316
            cell_in_ranges = 1;
 
317
            for (n = 0; n < nbands; n++) {
 
318
                if (band_buffer[n][col] < statistics->band_range_min[n] ||
 
319
                    band_buffer[n][col] > statistics->band_range_max[n]) {
 
320
                    /* out of at least 1 range */
 
321
                    cell_in_ranges = 0;
 
322
                }
 
323
            }
 
324
            if (cell_in_ranges) {
 
325
                /* if in range do the assignment */
 
326
                buffer[col] = (CELL) 1;
 
327
            }
 
328
        }
 
329
        Rast_put_row(fd, buffer, CELL_TYPE);
 
330
    }
 
331
    Rast_close(fd);
 
332
 
 
333
    /* generate and write the color table for the mask */
 
334
    Rast_init_colors(&raster_colors);
 
335
    G_str_to_color(statistics->color, &r, &g, &b);
 
336
    Rast_set_c_color((CELL) 1, r, g, b, &raster_colors);
 
337
    Rast_write_colors(raster_name, G_mapset(), &raster_colors);
 
338
}
 
339
 
 
340
/* helpers */
 
341
/*!
 
342
   \brief Helper function for computing min and max range in one band.
 
343
 
 
344
   Computing min and max range value (distance from mean
 
345
   dependent on number od std ddevs).
 
346
 
 
347
   \param statistics pointer to statistics structure
 
348
   \param band band index
 
349
 */
 
350
void band_range(IClass_statistics * statistics, int band)
 
351
{
 
352
    float dist;
 
353
 
 
354
    dist = statistics->nstd * statistics->band_stddev[band];
 
355
    statistics->band_range_min[band] =
 
356
        statistics->band_mean[band] - dist + 0.5;
 
357
    statistics->band_range_max[band] =
 
358
        statistics->band_mean[band] + dist + 0.5;
 
359
}
 
360
 
 
361
/*!
 
362
   \brief Helper function for computing mean.
 
363
 
 
364
   Computing mean value of cell category values
 
365
   in one band within training area.
 
366
 
 
367
   \param statistics pointer to statistics structure
 
368
   \param band band index
 
369
 
 
370
   \return mean value
 
371
 */
 
372
float mean(IClass_statistics * statistics, int band)
 
373
{
 
374
    return statistics->band_sum[band] / statistics->ncells;
 
375
}
 
376
 
 
377
/*!
 
378
   \brief Helper function for standard deviation.
 
379
 
 
380
   Computing standard deviation of cell category values
 
381
   in one band within training area.
 
382
 
 
383
   \param statistics pointer to statistics structure
 
384
   \param band band index
 
385
 
 
386
   \return standard deviation
 
387
 */
 
388
float stddev(IClass_statistics * statistics, int band)
 
389
{
 
390
    return sqrt(var(statistics, band, band));
 
391
}
 
392
 
 
393
/*!
 
394
   \brief Helper function for computing variance.
 
395
 
 
396
   Computing variance of cell category values
 
397
   in one band within training area.
 
398
 
 
399
   \param statistics pointer to statistics structure
 
400
   \param band1 band index
 
401
   \param band2 band index
 
402
 
 
403
   \return variance
 
404
 
 
405
   \see var_signature
 
406
 */
 
407
float var(IClass_statistics * statistics, int band1, int band2)
 
408
{
 
409
    float product;
 
410
 
 
411
    float mean1, mean2;
 
412
 
 
413
    int n;
 
414
 
 
415
    product = statistics->band_product[band1][band2];
 
416
    mean1 = mean(statistics, band1);
 
417
    mean2 = mean(statistics, band2);
 
418
    n = statistics->ncells;
 
419
 
 
420
    return product / n - mean1 * mean2;
 
421
}
 
422
 
 
423
/*!
 
424
   \brief Helper function for computing variance for signature file.
 
425
 
 
426
   Computing variance of cell category values
 
427
   in one band within training area. Variance is computed
 
428
   in special way.
 
429
 
 
430
   \param statistics pointer to statistics structure
 
431
   \param band1 band index
 
432
   \param band2 band index
 
433
 
 
434
   \return variance
 
435
 
 
436
   \see var
 
437
 
 
438
   \todo verify the computation
 
439
 */
 
440
float var_signature(IClass_statistics * statistics, int band1, int band2)
 
441
{
 
442
    float product;
 
443
 
 
444
    float sum1, sum2;
 
445
 
 
446
    int n;
 
447
 
 
448
    product = statistics->band_product[band1][band2];
 
449
    sum1 = statistics->band_sum[band1];
 
450
    sum2 = statistics->band_sum[band2];
 
451
    n = statistics->ncells;
 
452
 
 
453
    return (product - sum1 * sum2 / n) / (n - 1);
 
454
}
 
455
 
 
456
/* getters */
 
457
/*!
 
458
   \brief Get number of bands.
 
459
 
 
460
   \param statistics pointer to statistics structure
 
461
   \param[out] nbands number of bands
 
462
 */
 
463
void I_iclass_statistics_get_nbands(IClass_statistics * statistics,
 
464
                                    int *nbands)
 
465
{
 
466
    *nbands = statistics->nbands;
 
467
}
 
468
 
 
469
/*!
 
470
   \brief Get category (class).
 
471
 
 
472
   \param statistics pointer to statistics structure
 
473
   \param[out] cat category
 
474
 */
 
475
void I_iclass_statistics_get_cat(IClass_statistics * statistics, int *cat)
 
476
{
 
477
    *cat = statistics->cat;
 
478
}
 
479
 
 
480
/*!
 
481
   \brief Get category (class) name.
 
482
 
 
483
   \note \a name is pointer to already allocated
 
484
   const char * in \a statistics.
 
485
   You should not free it.
 
486
 
 
487
   \param statistics pointer to statistics structure
 
488
   \param[out] name category name
 
489
 */
 
490
void I_iclass_statistics_get_name(IClass_statistics * statistics,
 
491
                                  const char **name)
 
492
{
 
493
    *name = statistics->name;
 
494
}
 
495
 
 
496
/*!
 
497
   \brief Get category (class) color.
 
498
 
 
499
   \note \a color is pointer to already allocated
 
500
   const char * in \a statistics.
 
501
   You should not free it.
 
502
 
 
503
   \param statistics pointer to statistics structure
 
504
   \param[out] color category color
 
505
 */
 
506
void I_iclass_statistics_get_color(IClass_statistics * statistics,
 
507
                                   const char **color)
 
508
{
 
509
    *color = statistics->color;
 
510
}
 
511
 
 
512
 
 
513
/*!
 
514
   \brief Get number of cells in training areas.
 
515
 
 
516
   \param statistics pointer to statistics structure
 
517
   \param[out] ncells number of cells
 
518
 */
 
519
void I_iclass_statistics_get_ncells(IClass_statistics * statistics,
 
520
                                    int *ncells)
 
521
{
 
522
    *ncells = statistics->ncells;
 
523
}
 
524
 
 
525
/*!
 
526
   \brief Get the multiplier of standard deviation.
 
527
 
 
528
   \param statistics pointer to statistics structure
 
529
   \param[out] nstd multiplier of standard deviation
 
530
 */
 
531
void I_iclass_statistics_get_nstd(IClass_statistics * statistics, float *nstd)
 
532
{
 
533
    *nstd = statistics->nstd;
 
534
}
 
535
 
 
536
/*!
 
537
   \brief Set the multiplier of standard deviation.
 
538
 
 
539
   \param statistics pointer to statistics structure
 
540
   \param nstd multiplier of standard deviation
 
541
 */
 
542
void I_iclass_statistics_set_nstd(IClass_statistics * statistics, float nstd)
 
543
{
 
544
    statistics->nstd = nstd;
 
545
}
 
546
 
 
547
/*!
 
548
   \brief Get minimum value in band.
 
549
 
 
550
   \param statistics pointer to statistics structure
 
551
   \param band band index
 
552
   \param[out] min minimum value
 
553
 
 
554
   \return 1 on success
 
555
   \return 0 band index out of range
 
556
 */
 
557
int I_iclass_statistics_get_min(IClass_statistics * statistics, int band,
 
558
                                int *min)
 
559
{
 
560
    if (band >= statistics->nbands) {
 
561
        G_warning(_("Band index out of range"));
 
562
        return 0;
 
563
    }
 
564
 
 
565
    *min = statistics->band_min[band];
 
566
 
 
567
    return 1;
 
568
}
 
569
 
 
570
/*!
 
571
   \brief Get maximum value in band.
 
572
 
 
573
   \param statistics pointer to statistics structure
 
574
   \param band band index
 
575
   \param[out] max maximum value
 
576
 
 
577
   \return 1 on success
 
578
   \return 0 band index out of range
 
579
 */
 
580
int I_iclass_statistics_get_max(IClass_statistics * statistics, int band,
 
581
                                int *max)
 
582
{
 
583
    if (band >= statistics->nbands) {
 
584
        G_warning(_("Band index out of range"));
 
585
        return 0;
 
586
    }
 
587
 
 
588
    *max = statistics->band_max[band];
 
589
 
 
590
    return 1;
 
591
}
 
592
 
 
593
/*!
 
594
   \brief Get sum of values in band.
 
595
 
 
596
   \param statistics pointer to statistics structure
 
597
   \param band band index
 
598
   \param[out] sum sum
 
599
 
 
600
   \return 1 on success
 
601
   \return 0 band index out of range
 
602
 */
 
603
int I_iclass_statistics_get_sum(IClass_statistics * statistics, int band,
 
604
                                float *sum)
 
605
{
 
606
    if (band >= statistics->nbands) {
 
607
        G_warning(_("Band index out of range"));
 
608
        return 0;
 
609
    }
 
610
 
 
611
    *sum = statistics->band_sum[band];
 
612
 
 
613
    return 1;
 
614
}
 
615
 
 
616
/*!
 
617
   \brief Get mean of cell category values in band.
 
618
 
 
619
   \param statistics pointer to statistics structure
 
620
   \param band band index
 
621
   \param[out] mean mean
 
622
 
 
623
   \return 1 on success
 
624
   \return 0 band index out of range
 
625
 */
 
626
int I_iclass_statistics_get_mean(IClass_statistics * statistics, int band,
 
627
                                 float *mean)
 
628
{
 
629
    if (band >= statistics->nbands) {
 
630
        G_warning(_("Band index out of range"));
 
631
        return 0;
 
632
    }
 
633
 
 
634
    *mean = statistics->band_mean[band];
 
635
 
 
636
    return 1;
 
637
}
 
638
 
 
639
/*!
 
640
   \brief Get standard deviation of cell category values in band.
 
641
 
 
642
   \param statistics pointer to statistics structure
 
643
   \param band band index
 
644
   \param[out] stddev standard deviation
 
645
 
 
646
   \return 1 on success
 
647
   \return 0 band index out of range
 
648
 */
 
649
int I_iclass_statistics_get_stddev(IClass_statistics * statistics, int band,
 
650
                                   float *stddev)
 
651
{
 
652
    if (band >= statistics->nbands) {
 
653
        G_warning(_("Band index out of range"));
 
654
        return 0;
 
655
    }
 
656
 
 
657
    *stddev = statistics->band_stddev[band];
 
658
 
 
659
    return 1;
 
660
}
 
661
 
 
662
/*!
 
663
   \brief Get histogram value in band.
 
664
 
 
665
   Each band has one value for each raster cell category.
 
666
   Value is number of cells in category.
 
667
 
 
668
   \param statistics pointer to statistics structure
 
669
   \param band band index
 
670
   \param cat raster cell category
 
671
   \param[out] value number of cells in category
 
672
 
 
673
   \return 1 on success
 
674
   \return 0 band index or cell category value out of range
 
675
 */
 
676
int I_iclass_statistics_get_histo(IClass_statistics * statistics, int band,
 
677
                                  int cat, int *value)
 
678
{
 
679
    if (band >= statistics->nbands) {
 
680
        G_warning(_("Band index out of range"));
 
681
        return 0;
 
682
    }
 
683
    if (cat >= MAX_CATS) {
 
684
        G_warning(_("Cell category value out of range"));
 
685
        return 0;
 
686
    }
 
687
 
 
688
    *value = statistics->band_histo[band][cat];
 
689
 
 
690
    return 1;
 
691
}
 
692
 
 
693
/*!
 
694
   \brief Get product value
 
695
 
 
696
   Product value of two bands is sum of products
 
697
   of cell category values of two bands.
 
698
   Only cells from training areas are taken into account.
 
699
 
 
700
   \param statistics statistics object
 
701
   \param band1 index of first band
 
702
   \param band2 index of second band
 
703
   \param[out] value product value
 
704
 
 
705
   \return 1 on success
 
706
   \return 0 band index out of range
 
707
 */
 
708
int I_iclass_statistics_get_product(IClass_statistics * statistics, int band1,
 
709
                                    int band2, float *value)
 
710
{
 
711
    if (band1 >= statistics->nbands || band2 >= statistics->nbands) {
 
712
        G_warning(_("Band index out of range"));
 
713
        return 0;
 
714
    }
 
715
 
 
716
    *value = statistics->band_product[band1][band2];
 
717
 
 
718
    return 1;
 
719
}
 
720
 
 
721
/*!
 
722
   \brief Get minimum cell value based on mean and standard deviation for band.
 
723
 
 
724
   \param statistics pointer to statistics structure
 
725
   \param band band index
 
726
   \param[out] min minumum value
 
727
 
 
728
   \return 1 on success
 
729
   \return 0 band index out of range
 
730
 */
 
731
int I_iclass_statistics_get_range_min(IClass_statistics * statistics,
 
732
                                      int band, int *min)
 
733
{
 
734
    if (band >= statistics->nbands) {
 
735
        G_warning(_("Band index out of range"));
 
736
        return 0;
 
737
    }
 
738
 
 
739
    *min = statistics->band_range_min[band];
 
740
 
 
741
    return 1;
 
742
}
 
743
 
 
744
/*!
 
745
   \brief Get maximum cell value based on mean and standard deviation for band.
 
746
 
 
747
   \param statistics pointer to statistics structure
 
748
   \param band band index
 
749
   \param[out] max maximum value
 
750
 
 
751
   \return 1 on success
 
752
   \return 0 band index out of range
 
753
 */
 
754
int I_iclass_statistics_get_range_max(IClass_statistics * statistics,
 
755
                                      int band, int *max)
 
756
{
 
757
    if (band >= statistics->nbands) {
 
758
        G_warning(_("Band index out of range"));
 
759
        return 0;
 
760
    }
 
761
 
 
762
    *max = statistics->band_range_max[band];
 
763
 
 
764
    return 1;
 
765
}