3 #include <grass/imagery.h>
6 static int gettag(FILE *,
char *);
7 static int get_nbands(FILE *,
struct SigSet *);
8 static int get_title(FILE *,
struct SigSet *);
9 static int get_class(FILE *,
struct SigSet *);
10 static int get_classnum(FILE *,
struct ClassSig *);
11 static int get_classtype(FILE *,
struct ClassSig *);
12 static int get_classtitle(FILE *,
struct ClassSig *);
13 static int get_subclass(FILE *,
struct SigSet *,
struct ClassSig *);
14 static int get_subclass_pi(FILE *,
struct SubSig *);
15 static int get_subclass_means(FILE *,
struct SubSig *,
int);
16 static int get_subclass_covar(FILE *,
struct SubSig *,
int);
18 static double **alloc_matrix(
int rows,
int cols)
23 m = (
double **)G_calloc(rows,
sizeof(
double *));
24 m[0] = (
double *)G_calloc(rows * cols,
sizeof(
double));
25 for (i = 1; i < rows; i++)
26 m[i] = m[i - 1] + cols;
35 for (i = 0, count = 0; i < S->nclasses; i++)
36 if (S->ClassSig[i].used)
44 struct ClassSig *
C,
int npixels)
46 struct ClassData *Data;
48 Data = &(C->ClassData);
49 Data->npixels = npixels;
51 Data->x = alloc_matrix(npixels, S->nbands);
52 Data->p = alloc_matrix(npixels, C->nsubclasses);
78 S->ClassSig = (
struct ClassSig *)G_malloc(
sizeof(
struct ClassSig));
80 S->ClassSig = (
struct ClassSig *)G_realloc((
char *)S->ClassSig,
81 sizeof(
struct ClassSig) *
84 Sp = &S->ClassSig[S->nclasses++];
88 Sp->type = SIGNATURE_TYPE_MIXED;
98 if (C->nsubclasses == 0)
99 C->SubSig = (
struct SubSig *)G_malloc(
sizeof(
struct SubSig));
101 C->SubSig = (
struct SubSig *)G_realloc((
char *)C->SubSig,
102 sizeof(
struct SubSig) *
103 (C->nsubclasses + 1));
105 Sp = &C->SubSig[C->nsubclasses++];
107 Sp->R = (
double **)G_calloc(S->nbands,
sizeof(
double *));
108 Sp->R[0] = (
double *)G_calloc(S->nbands * S->nbands,
sizeof(
double));
109 for (i = 1; i < S->nbands; i++)
110 Sp->R[i] = Sp->R[i - 1] + S->nbands;
111 Sp->Rinv = (
double **)G_calloc(S->nbands,
sizeof(
double *));
112 Sp->Rinv[0] = (
double *)G_calloc(S->nbands * S->nbands,
sizeof(
double));
113 for (i = 1; i < S->nbands; i++)
114 Sp->Rinv[i] = Sp->Rinv[i - 1] + S->nbands;
115 Sp->means = (
double *)G_calloc(S->nbands,
sizeof(
double));
122 #define eq(a,b) strcmp(a,b)==0
130 while (gettag(fd, tag)) {
131 if (
eq(tag,
"title:"))
133 if (
eq(tag,
"nbands:"))
135 if (
eq(tag,
"class:"))
141 static int gettag(FILE *
fd,
char *tag)
143 if (fscanf(fd,
"%s", tag) != 1)
149 static int get_nbands(FILE * fd,
struct SigSet *S)
151 fscanf(fd,
"%d", &S->nbands);
156 static int get_title(FILE * fd,
struct SigSet *S)
161 fscanf(fd,
"%[^\n]", title);
167 static int get_class(FILE * fd,
struct SigSet *S)
173 while (gettag(fd, tag)) {
174 if (
eq(tag,
"endclass:"))
176 if (
eq(tag,
"classnum:"))
178 if (
eq(tag,
"classtype:"))
179 get_classtype(fd, C);
180 if (
eq(tag,
"classtitle:"))
181 get_classtitle(fd, C);
182 if (
eq(tag,
"subclass:"))
183 get_subclass(fd, S, C);
189 static int get_classnum(FILE * fd,
struct ClassSig *C)
191 fscanf(fd,
"%ld", &C->classnum);
196 static int get_classtype(FILE * fd,
struct ClassSig *C)
198 fscanf(fd,
"%d", &C->type);
203 static int get_classtitle(FILE * fd,
struct ClassSig *C)
208 fscanf(fd,
"%[^\n]", title);
214 static int get_subclass(FILE * fd,
struct SigSet *S,
struct ClassSig *C)
221 while (gettag(fd, tag)) {
222 if (
eq(tag,
"endsubclass:"))
225 get_subclass_pi(fd, Sp);
226 if (
eq(tag,
"means:"))
227 get_subclass_means(fd, Sp, S->nbands);
228 if (
eq(tag,
"covar:"))
229 get_subclass_covar(fd, Sp, S->nbands);
235 static int get_subclass_pi(FILE * fd,
struct SubSig *Sp)
237 fscanf(fd,
"%lf", &Sp->pi);
242 static int get_subclass_means(FILE * fd,
struct SubSig *Sp,
int nbands)
246 for (i = 0; i < nbands; i++)
247 fscanf(fd,
"%lf", &Sp->means[i]);
252 static int get_subclass_covar(FILE * fd,
struct SubSig *Sp,
int nbands)
256 for (i = 0; i < nbands; i++)
257 for (j = 0; j < nbands; j++)
258 fscanf(fd,
"%lf", &Sp->R[i][j]);
303 const struct ClassSig *Cp;
304 const struct SubSig *Sp;
308 fprintf(fd,
"nbands: %d\n", S->nbands);
309 for (i = 0; i < S->nclasses; i++) {
310 Cp = &S->ClassSig[i];
313 if (Cp->nsubclasses <= 0)
315 fprintf(fd,
"class:\n");
316 fprintf(fd,
" classnum: %ld\n", Cp->classnum);
318 fprintf(fd,
" classtype: %d\n", Cp->type);
320 for (j = 0; j < Cp->nsubclasses; j++) {
322 fprintf(fd,
" subclass:\n");
323 fprintf(fd,
" pi: %g\n", Sp->pi);
324 fprintf(fd,
" means:");
325 for (b1 = 0; b1 < S->nbands; b1++)
326 fprintf(fd,
" %g", Sp->means[b1]);
328 fprintf(fd,
" covar:\n");
329 for (b1 = 0; b1 < S->nbands; b1++) {
331 for (b2 = 0; b2 < S->nbands; b2++)
332 fprintf(fd,
" %g", Sp->R[b1][b2]);
335 fprintf(fd,
" endsubclass:\n");
337 fprintf(fd,
"endclass:\n");
char * G_store(const char *s)
Copy string to allocated memory.
const char * I_GetSigTitle(const struct SigSet *S)
int I_InitSigSet(struct SigSet *S)
int I_SetClassTitle(struct ClassSig *C, const char *title)
int I_SetSigTitle(struct SigSet *S, const char *title)
struct ClassSig * I_NewClassSig(struct SigSet *S)
int I_SigSetNClasses(struct SigSet *S)
int G_strip(char *buf)
Removes all leading and trailing white space from string.
struct ClassData * I_AllocClassData(struct SigSet *S, struct ClassSig *C, int npixels)
int I_WriteSigSet(FILE *fd, const struct SigSet *S)
int I_SigSetNBands(struct SigSet *S, int nbands)
struct SubSig * I_NewSubSig(struct SigSet *S, struct ClassSig *C)
int I_ReadSigSet(FILE *fd, struct SigSet *S)
const char * I_GetClassTitle(const struct ClassSig *C)