GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 
38 /*!
39  \brief Initialize signatures.
40 
41  \param[out] sigs pointer to signatures
42  \param refer pointer to band files structure
43 
44  \return 1 on success
45  \return 0 on failure
46  */
47 int I_iclass_init_signatures(struct Signature *sigs, struct Ref *refer)
48 {
49  G_debug(3, "I_iclass_init_signatures()");
50 
51  I_init_signatures(sigs, refer->nfiles);
52  for (unsigned int i = refer->nfiles; i--;) {
53  sigs->semantic_labels[i] = Rast_get_semantic_label_or_name(refer->file[i].name, refer->file[i].mapset);
54  }
55 
56  return 1;
57 }
58 
59 /*!
60  \brief Add one signature.
61 
62  \param[out] sigs pointer to signatures
63  \param statistics pointer to statistics structure
64  */
66  IClass_statistics * statistics)
67 {
68  int sn;
69 
70  int b1, b2;
71 
72  int r, g, b;
73 
74  G_debug(3, "I_iclass_add_signature()");
75 
76  G_str_to_color(statistics->color, &r, &g, &b);
77 
78  /* get a new signature */
79  I_new_signature(sigs);
80 
81  /* save the signature in a Sig structure */
82  sn = sigs->nsigs;
83  strcpy(sigs->sig[sn - 1].desc, statistics->name);
84  sigs->sig[sn - 1].npoints = statistics->ncells;
85  sigs->sig[sn - 1].status = 1;
86 
87  sigs->sig[sn - 1].have_color = 1;
88  sigs->sig[sn - 1].r = r;
89  sigs->sig[sn - 1].g = g;
90  sigs->sig[sn - 1].b = b;
91 
92  for (b1 = 0; b1 < sigs->nbands; b1++) {
93  sigs->sig[sn - 1].mean[b1] = statistics->band_mean[b1];
94  for (b2 = 0; b2 <= b1; b2++) {
95  sigs->sig[sn - 1].var[b1][b2] = var_signature(statistics, b1, b2);
96  }
97  }
98 }
99 
100 /*!
101  \brief Write signtures to signature file.
102 
103  \param sigs pointer to signatures
104  \param file_name name of signature file
105 
106  \return 1 on success
107  \return 0 on failure
108  */
109 int I_iclass_write_signatures(struct Signature *sigs, const char *file_name)
110 {
111  FILE *outsig_fd;
112 
113  G_debug(3, "I_write_signatures(): file_name=%s", file_name);
114 
115  if (!
116  (outsig_fd =
117  I_fopen_signature_file_new(file_name))) {
118  G_warning(_("Unable to open output signature file '%s'"), file_name);
119  return 0;
120  }
121 
122  I_write_signatures(outsig_fd, sigs);
123  fclose(outsig_fd);
124 
125  return 1;
126 }
char name[INAME_LEN]
Definition: imagery.h:22
const char * color
Definition: imagery.h:121
double ** var
Definition: imagery.h:57
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
Definition: color_str.c:112
Definition: imagery.h:26
int npoints
Definition: imagery.h:55
char desc[100]
Definition: imagery.h:54
int have_color
Definition: imagery.h:60
const char * name
Definition: imagery.h:120
float g
Definition: imagery.h:59
float b
Definition: imagery.h:59
int I_iclass_init_signatures(struct Signature *sigs, struct Ref *refer)
Initialize signatures.
double b
Definition: r_raster.c:39
char mapset[INAME_LEN]
Definition: imagery.h:23
int nbands
Definition: imagery.h:65
char ** semantic_labels
Definition: imagery.h:66
float g
Definition: named_colr.c:8
FILE * I_fopen_signature_file_new(const char *)
Create signature file.
Definition: sigfile.c:28
int nsigs
Definition: imagery.h:67
float var_signature(IClass_statistics *statistics, int band1, int band2)
Helper function for computing variance for signature file.
int I_iclass_write_signatures(struct Signature *sigs, const char *file_name)
Write signtures to signature file.
int status
Definition: imagery.h:58
int nfiles
Definition: imagery.h:28
float * band_mean
Definition: imagery.h:129
struct Ref_Files * file
Definition: imagery.h:29
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
char * Rast_get_semantic_label_or_name(const char *, const char *)
Get a raster map semantic label or fall back to its name.
struct One_Sig * sig
Definition: imagery.h:69
int I_new_signature(struct Signature *)
Definition: sig.c:29
void I_iclass_add_signature(struct Signature *sigs, IClass_statistics *statistics)
Add one signature.
double * mean
Definition: imagery.h:56
int G_debug(int, const char *,...) __attribute__((format(printf
int I_init_signatures(struct Signature *, int)
Initialize struct Signature before use.
Definition: sig.c:14
double r
Definition: r_raster.c:39
int I_write_signatures(FILE *, struct Signature *)
Write signatures to file.
Definition: sig.c:228
float r
Definition: imagery.h:59