34 #include "iclass_local_proto.h" 46 const char *
name,
const char *color,
float nstd)
48 G_debug(4,
"init_statistics() category=%d, name=%s, color=%s, nstd=%f",
49 category, name, color, nstd);
51 statistics->
cat = category;
54 statistics->
nstd = nstd;
80 G_debug(4,
"alloc_statistics()");
82 statistics->
nbands = nbands;
94 for (i = 0; i < nbands; i++) {
96 (
float *)
G_calloc(nbands,
sizeof(
float));
112 G_debug(4,
"free_statistics()");
125 for (i = 0; i < statistics->
nbands; i++) {
145 IClass_perimeter_list * perimeters,
146 CELL ** band_buffer,
int *band_fd)
148 int i,
b, b2, nbands;
150 float mean_value, stddev_value;
152 G_debug(5,
"make_all_statistics()");
154 nbands = statistics->
nbands;
155 for (b = 0; b < nbands; b++) {
159 for (b2 = 0; b2 < nbands; b2++)
161 for (b2 = 0; b2 < MAX_CATS; b2++)
165 for (i = 0; i < perimeters->nperimeters; i++) {
167 (statistics, &perimeters->perimeters[i], band_buffer, band_fd)) {
171 for (b = 0; b < statistics->
nbands; b++) {
172 mean_value =
mean(statistics, b);
173 stddev_value =
stddev(statistics, b);
196 IClass_perimeter * perimeter,
CELL ** band_buffer,
213 G_debug(5,
"make_statistics()");
215 nbands = statistics->
nbands;
217 if (perimeter->npoints % 2) {
218 G_warning(
_(
"prepare_signature: outline has odd number of points."));
224 for (i = 1; i < perimeter->npoints; i += 2) {
225 y = perimeter->points[i].y;
226 if (y != perimeter->points[i - 1].y) {
227 G_warning(
_(
"prepare_signature: scan line %d has odd number of points."),
233 x0 = perimeter->points[i - 1].x - 1;
234 x1 = perimeter->points[i].x - 1;
237 G_warning(
_(
"signature: perimeter points out of order."));
241 for (x = x0; x <= x1; x++) {
243 for (b = 0; b < nbands; b++) {
244 value = band_buffer[
b][
x];
245 G_debug(5,
"make_statistics() band: %d, read value: %d (max: %d)",
247 if (value < 0 || value > MAX_CATS - 1) {
248 G_warning(
_(
"Data error preparing signatures: value (%d) > num of cats (%d)"),
254 if (statistics->
band_min[b] > value)
256 if (statistics->
band_max[b] < value) {
259 "make_statistics() statistics->band_max[%d]: %d",
263 for (b2 = 0; b2 <=
b; b2++)
265 value * band_buffer[b2][x];
269 statistics->
ncells += ncells;
283 int *band_fd,
const char *raster_name)
295 int row, nrows, ncols;
297 struct Colors raster_colors;
303 nbands = statistics->
nbands;
312 for (row = 0; row < nrows; row++) {
314 for (col = 0; col < ncols; col++) {
315 buffer[col] = (
CELL) 0;
317 for (n = 0; n < nbands; n++) {
324 if (cell_in_ranges) {
326 buffer[col] = (
CELL) 1;
356 statistics->
band_mean[band] - dist + 0.5;
358 statistics->
band_mean[band] + dist + 0.5;
390 return sqrt(
var(statistics, band, band));
416 mean1 =
mean(statistics, band1);
417 mean2 =
mean(statistics, band2);
420 return product / n - mean1 * mean2;
453 return (product - sum1 * sum2 / n) / (n - 1);
466 *nbands = statistics->
nbands;
477 *cat = statistics->
cat;
493 *name = statistics->
name;
509 *color = statistics->
color;
522 *ncells = statistics->
ncells;
533 *nstd = statistics->
nstd;
544 statistics->
nstd = nstd;
560 if (band >= statistics->
nbands) {
583 if (band >= statistics->
nbands) {
606 if (band >= statistics->
nbands) {
629 if (band >= statistics->
nbands) {
652 if (band >= statistics->
nbands) {
679 if (band >= statistics->
nbands) {
683 if (cat >= MAX_CATS) {
684 G_warning(
_(
"Cell category value out of range"));
709 int band2,
float *value)
711 if (band1 >= statistics->
nbands || band2 >= statistics->
nbands) {
734 if (band >= statistics->
nbands) {
757 if (band >= statistics->
nbands) {
int I_iclass_statistics_get_product(IClass_statistics *statistics, int band1, int band2, float *value)
Get product value.
void Rast_write_colors(const char *, const char *, struct Colors *)
Write map layer color table.
void Rast_put_row(int, const void *, RASTER_MAP_TYPE)
Writes the next row for cell/fcell/dcell file.
void I_iclass_statistics_get_nbands(IClass_statistics *statistics, int *nbands)
Get number of bands.
int I_iclass_statistics_get_mean(IClass_statistics *statistics, int band, float *mean)
Get mean of cell category values in band.
void I_iclass_statistics_get_ncells(IClass_statistics *statistics, int *ncells)
Get number of cells in training areas.
int I_iclass_statistics_get_sum(IClass_statistics *statistics, int band, float *sum)
Get sum of values in band.
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
int I_iclass_statistics_get_stddev(IClass_statistics *statistics, int band, float *stddev)
Get standard deviation of cell category values in band.
void G_free(void *)
Free allocated memory.
void create_raster(IClass_statistics *statistics, CELL **band_buffer, int *band_fd, const char *raster_name)
Create raster map based on statistics.
void alloc_statistics(IClass_statistics *statistics, int nbands)
Allocate space for statistics.
int I_iclass_statistics_get_max(IClass_statistics *statistics, int band, int *max)
Get maximum value in band.
void band_range(IClass_statistics *statistics, int band)
Helper function for computing min and max range in one band.
int I_iclass_statistics_get_range_max(IClass_statistics *statistics, int band, int *max)
Get maximum cell value based on mean and standard deviation for band.
void I_iclass_statistics_get_name(IClass_statistics *statistics, const char **name)
Get category (class) name.
void I_iclass_free_statistics(IClass_statistics *statistics)
Free space allocated for statistics attributes.
int I_iclass_statistics_get_range_min(IClass_statistics *statistics, int band, int *min)
Get minimum cell value based on mean and standard deviation for band.
int make_all_statistics(IClass_statistics *statistics, IClass_perimeter_list *perimeters, CELL **band_buffer, int *band_fd)
Calculate statistics for all training areas.
int I_iclass_statistics_get_histo(IClass_statistics *statistics, int band, int cat, int *value)
Get histogram value in band.
float var_signature(IClass_statistics *statistics, int band1, int band2)
Helper function for computing variance for signature file.
void I_iclass_init_statistics(IClass_statistics *statistics, int category, const char *name, const char *color, float nstd)
Initialize statistics.
int I_iclass_statistics_get_min(IClass_statistics *statistics, int band, int *min)
Get minimum value in band.
float stddev(IClass_statistics *statistics, int band)
Helper function for standard deviation.
int Rast_open_c_new(const char *)
Opens a new cell file in a database (compressed)
double * y
Array of Y coordinates.
void I_iclass_statistics_get_nstd(IClass_statistics *statistics, float *nstd)
Get the multiplier of standard deviation.
const char * G_mapset(void)
Get current mapset name.
int Rast_window_rows(void)
Number of rows in active window.
float var(IClass_statistics *statistics, int band1, int band2)
Helper function for computing variance.
int Rast_window_cols(void)
Number of columns in active window.
void G_warning(const char *,...) __attribute__((format(printf
int make_statistics(IClass_statistics *statistics, IClass_perimeter *perimeter, CELL **band_buffer, int *band_fd)
Calculate statistics for one training area.
char * G_store(const char *)
Copy string to allocated memory.
void I_iclass_statistics_get_color(IClass_statistics *statistics, const char **color)
Get category (class) color.
float mean(IClass_statistics *statistics, int band)
Helper function for computing mean.
void Rast_init_colors(struct Colors *)
Initialize color structure.
void Rast_set_c_color(CELL, int, int, int, struct Colors *)
Set a category color (CELL)
void I_iclass_statistics_set_nstd(IClass_statistics *statistics, float nstd)
Set the multiplier of standard deviation.
int G_debug(int, const char *,...) __attribute__((format(printf
CELL * Rast_allocate_c_buf(void)
Allocate memory for a CELL type raster map.
void read_band_row(CELL **band_buffer, int *band_fd, int nbands, int row)
Read one row of each band.
void Rast_close(int)
Close a raster map.
void I_iclass_statistics_get_cat(IClass_statistics *statistics, int *cat)
Get category (class).