GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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  if (!I_init_signatures(sigs, refer->nfiles))
52  return 1; /* success */
53 
54  return 0;
55 }
56 
57 /*!
58  \brief Add one signature.
59 
60  \param[out] sigs pointer to signatures
61  \param statistics pointer to statistics structure
62  */
64  IClass_statistics * statistics)
65 {
66  int sn;
67 
68  int b1, b2;
69 
70  int r, g, b;
71 
72  G_debug(3, "I_iclass_add_signature()");
73 
74  G_str_to_color(statistics->color, &r, &g, &b);
75 
76  /* get a new signature */
77  I_new_signature(sigs);
78 
79  /* save the signature in a Sig structure */
80  sn = sigs->nsigs;
81  strcpy(sigs->sig[sn - 1].desc, statistics->name);
82  sigs->sig[sn - 1].npoints = statistics->ncells;
83  sigs->sig[sn - 1].status = 1;
84 
85  sigs->sig[sn - 1].have_color = 1;
86  sigs->sig[sn - 1].r = r;
87  sigs->sig[sn - 1].g = g;
88  sigs->sig[sn - 1].b = b;
89 
90  for (b1 = 0; b1 < sigs->nbands; b1++) {
91  sigs->sig[sn - 1].mean[b1] = statistics->band_mean[b1];
92  for (b2 = 0; b2 <= b1; b2++) {
93  sigs->sig[sn - 1].var[b1][b2] = var_signature(statistics, b1, b2);
94  }
95  }
96 }
97 
98 /*!
99  \brief Write signtures to signature file.
100 
101  \param sigs pointer to signatures
102  \param group image group
103  \param sub_group image subgroup
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 *group,
110  const char *sub_group, const char *file_name)
111 {
112  FILE *outsig_fd;
113 
114  G_debug(3, "I_write_signatures(): group=%s, file_name=%s", group,
115  file_name);
116 
117  if (!
118  (outsig_fd =
119  I_fopen_signature_file_new(group, sub_group, file_name))) {
120  G_warning(_("Unable to open output signature file '%s'"), file_name);
121  return 0;
122  }
123 
124  I_write_signatures(outsig_fd, sigs);
125  fclose(outsig_fd);
126 
127  return 1;
128 }
const char * color
Definition: imagery.h:119
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 I_iclass_write_signatures(struct Signature *sigs, const char *group, const char *sub_group, const char *file_name)
Write signtures to signature file.
int have_color
Definition: imagery.h:60
const char * name
Definition: imagery.h:118
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
int nbands
Definition: imagery.h:65
float g
Definition: named_colr.c:8
int nsigs
Definition: imagery.h:66
float var_signature(IClass_statistics *statistics, int band1, int band2)
Helper function for computing variance for signature file.
int status
Definition: imagery.h:58
int nfiles
Definition: imagery.h:28
float * band_mean
Definition: imagery.h:127
FILE * I_fopen_signature_file_new(const char *, const char *, const char *)
Create signature file.
Definition: sigfile.c:27
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
struct One_Sig * sig
Definition: imagery.h:68
int I_new_signature(struct Signature *)
Definition: sig.c:16
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)
Definition: sig.c:4
double r
Definition: r_raster.c:39
int I_write_signatures(FILE *, struct Signature *)
Definition: sig.c:116
float r
Definition: imagery.h:59