GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bceeab9228
iclass_signatures.c
Go to the documentation of this file.
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  Computation and writing signatures to file.
10 
11  Copyright (C) 1999-2007, 2011 by the GRASS Development Team
12 
13  This program is free software under the GNU General Public License
14  (>=v2). Read the file COPYING that comes with GRASS for details.
15 
16  \author David Satnik, Central Washington University (original author)
17  \author Markus Neteler <neteler itc.it> (i.class module)
18  \author Bernhard Reiter <bernhard intevation.de> (i.class module)
19  \author Brad Douglas <rez touchofmadness.com>(i.class module)
20  \author Glynn Clements <glynn gclements.plus.com> (i.class module)
21  \author Hamish Bowman <hamish_b yahoo.com> (i.class module)
22  \author Jan-Oliver Wagner <jan intevation.de> (i.class module)
23  \author Anna Kratochvilova <kratochanna gmail.com> (rewriting for wx.iclass)
24  \author Vaclav Petras <wenzeslaus gmail.com> (rewriting for wx.iclass)
25  */
26 
27 #include <string.h>
28 #include <stdio.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 signatures.
38 
39  \param[out] sigs pointer to signatures
40  \param refer pointer to band files structure
41 
42  \return 1 on success
43  \return 0 on failure
44  */
45 int I_iclass_init_signatures(struct Signature *sigs, struct Ref *refer)
46 {
47  G_debug(3, "I_iclass_init_signatures()");
48 
49  I_init_signatures(sigs, refer->nfiles);
50  for (unsigned int i = refer->nfiles; i--;) {
52  refer->file[i].name, refer->file[i].mapset);
53  }
54 
55  return 1;
56 }
57 
58 /*!
59  \brief Add one signature.
60 
61  \param[out] sigs pointer to signatures
62  \param statistics pointer to statistics structure
63  */
65  IClass_statistics *statistics)
66 {
67  int sn;
68 
69  int b1, b2;
70 
71  int r, g, b;
72 
73  G_debug(3, "I_iclass_add_signature()");
74 
75  G_str_to_color(statistics->color, &r, &g, &b);
76 
77  /* get a new signature */
78  I_new_signature(sigs);
79 
80  /* save the signature in a Sig structure */
81  sn = sigs->nsigs;
82  strcpy(sigs->sig[sn - 1].desc, statistics->name);
83  sigs->sig[sn - 1].npoints = statistics->ncells;
84  sigs->sig[sn - 1].status = 1;
85 
86  sigs->sig[sn - 1].have_color = 1;
87  sigs->sig[sn - 1].r = r;
88  sigs->sig[sn - 1].g = g;
89  sigs->sig[sn - 1].b = b;
90 
91  for (b1 = 0; b1 < sigs->nbands; b1++) {
92  sigs->sig[sn - 1].mean[b1] = statistics->band_mean[b1];
93  for (b2 = 0; b2 <= b1; b2++) {
94  sigs->sig[sn - 1].var[b1][b2] = var_signature(statistics, b1, b2);
95  }
96  }
97 }
98 
99 /*!
100  \brief Write signtures to signature file.
101 
102  \param sigs pointer to signatures
103  \param file_name name of signature file
104 
105  \return 1 on success
106  \return 0 on failure
107  */
108 int I_iclass_write_signatures(struct Signature *sigs, const char *file_name)
109 {
110  FILE *outsig_fd;
111 
112  G_debug(3, "I_write_signatures(): file_name=%s", file_name);
113 
114  if (!(outsig_fd = I_fopen_signature_file_new(file_name))) {
115  G_warning(_("Unable to open output signature file '%s'"), file_name);
116  return 0;
117  }
118 
119  I_write_signatures(outsig_fd, sigs);
120  fclose(outsig_fd);
121 
122  return 1;
123 }
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
Definition: color_str.c:101
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
int I_write_signatures(FILE *, struct Signature *)
Write signatures to file.
Definition: sig.c:245
int I_new_signature(struct Signature *)
Definition: sig.c:30
int I_init_signatures(struct Signature *, int)
Initialize struct Signature before use.
Definition: sig.c:14
FILE * I_fopen_signature_file_new(const char *)
Create signature file.
Definition: sigfile.c:28
char * Rast_get_semantic_label_or_name(const char *, const char *)
Get a raster map semantic label or fall back to its name.
#define _(str)
Definition: glocale.h:10
int I_iclass_write_signatures(struct Signature *sigs, const char *file_name)
Write signtures to signature file.
void I_iclass_add_signature(struct Signature *sigs, IClass_statistics *statistics)
Add one signature.
int I_iclass_init_signatures(struct Signature *sigs, struct Ref *refer)
Initialize signatures.
float var_signature(IClass_statistics *statistics, int band1, int band2)
Helper function for computing variance for signature file.
float g
Definition: named_colr.c:7
#define strcpy
Definition: parson.c:62
double b
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
const char * color
Definition: imagery.h:112
const char * name
Definition: imagery.h:111
float * band_mean
Definition: imagery.h:120
int status
Definition: imagery.h:52
char desc[256]
Definition: imagery.h:48
int npoints
Definition: imagery.h:49
double * mean
Definition: imagery.h:50
double ** var
Definition: imagery.h:51
float g
Definition: imagery.h:53
float b
Definition: imagery.h:53
float r
Definition: imagery.h:53
int have_color
Definition: imagery.h:54
char name[INAME_LEN]
Definition: imagery.h:20
char mapset[INAME_LEN]
Definition: imagery.h:21
Definition: imagery.h:24
int nfiles
Definition: imagery.h:25
struct Ref_Files * file
Definition: imagery.h:26
int nsigs
Definition: imagery.h:61
struct One_Sig * sig
Definition: imagery.h:64
char ** semantic_labels
Definition: imagery.h:60
int nbands
Definition: imagery.h:59