forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_train.c
84 lines (70 loc) · 2.05 KB
/
get_train.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdlib.h>
#include <grass/raster.h>
#include <grass/imagery.h>
#include <grass/glocale.h>
#include "files.h"
#include "parms.h"
int get_training_classes(struct parms *parms,
struct files *files, struct SigSet *S)
{
int fd;
CELL *cell;
CELL cat;
struct Cell_stats cell_stats;
CELL *list;
int row, nrows, ncols;
int i, n;
long count;
struct ClassSig *Sig;
fd = files->train_fd;
cell = files->train_cell;
nrows = Rast_window_rows();
ncols = Rast_window_cols();
/* determine the non-zero categories in the map */
I_InitSigSet(S);
I_SigSetNBands(S, files->nbands);
I_SetSigTitle(S, Rast_get_cats_title(&files->training_labels));
Rast_init_cell_stats(&cell_stats);
G_message(_("Finding training classes..."));
for (row = 0; row < nrows; row++) {
G_percent(row, nrows, 2);
Rast_get_c_row(fd, cell, row);
Rast_update_cell_stats(cell, ncols, &cell_stats);
}
G_percent(nrows, nrows, 2);
/* convert this to an array */
Rast_rewind_cell_stats(&cell_stats);
n = 0;
while (Rast_next_cell_stat(&cat, &count, &cell_stats)) {
if (count > 1) {
Sig = I_NewClassSig(S);
I_SetClassTitle(Sig, Rast_get_c_cat(&cat, &files->training_labels));
Sig->classnum = cat;
/* initialize this class with maxsubclasses (by allocating them) */
for (i = 0; i < parms->maxsubclasses; i++)
I_NewSubSig(S, Sig);
I_AllocClassData(S, Sig, count);
n++;
}
else
G_warning(_("Training class %d only has one cell - this class will be ignored"),
cat);
}
if (n == 0) {
G_fatal_error(_("Training map has no classes"));
}
list = (CELL *) G_calloc(n, sizeof(CELL));
n = 0;
Rast_rewind_cell_stats(&cell_stats);
while (Rast_next_cell_stat(&cat, &count, &cell_stats))
if (count > 1)
list[n++] = cat;
Rast_free_cell_stats(&cell_stats);
files->ncats = n;
files->training_cats = list;
if (files->ncats == 1)
G_message(_("1 class found"));
else
G_message(_("%d classes found"), files->ncats);
return 0;
}