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++) {
111 G_debug(4,
"free_statistics()");
123 for (i = 0; i < statistics->
nbands; i++) {
143 IClass_perimeter_list *perimeters,
CELL **band_buffer,
146 int i,
b, b2, nbands;
148 float mean_value, stddev_value;
150 G_debug(5,
"make_all_statistics()");
152 nbands = statistics->
nbands;
153 for (
b = 0;
b < nbands;
b++) {
157 for (b2 = 0; b2 < nbands; b2++)
159 for (b2 = 0; b2 < MAX_CATS; b2++)
163 for (i = 0; i < perimeters->nperimeters; i++) {
165 band_buffer, band_fd)) {
169 for (
b = 0;
b < statistics->
nbands;
b++) {
170 mean_value =
mean(statistics,
b);
171 stddev_value =
stddev(statistics,
b);
194 CELL **band_buffer,
int *band_fd)
210 G_debug(5,
"make_statistics()");
212 nbands = statistics->
nbands;
214 if (perimeter->npoints % 2) {
215 G_warning(
_(
"prepare_signature: outline has odd number of points."));
221 for (i = 1; i < perimeter->npoints; i += 2) {
222 y = perimeter->points[i].y;
223 if (y != perimeter->points[i - 1].y) {
225 _(
"prepare_signature: scan line %d has odd number of points."),
231 x0 = perimeter->points[i - 1].x - 1;
232 x1 = perimeter->points[i].x - 1;
235 G_warning(
_(
"signature: perimeter points out of order."));
239 for (
x = x0;
x <= x1;
x++) {
241 for (
b = 0;
b < nbands;
b++) {
242 value = band_buffer[
b][
x];
244 "make_statistics() band: %d, read value: %d (max: %d)",
246 if (value < 0 || value > MAX_CATS - 1) {
247 G_warning(
_(
"Data error preparing signatures: value (%d) > "
258 G_debug(5,
"make_statistics() statistics->band_max[%d]: %d",
262 for (b2 = 0; b2 <=
b; b2++)
264 value * band_buffer[b2][
x];
268 statistics->
ncells += ncells;
282 int *band_fd,
const char *raster_name)
294 int row, nrows, ncols;
296 struct Colors raster_colors;
302 nbands = statistics->
nbands;
311 for (row = 0; row < nrows; row++) {
313 for (col = 0; col < ncols; col++) {
314 buffer[col] = (
CELL)0;
316 for (n = 0; n < nbands; n++) {
323 if (cell_in_ranges) {
325 buffer[col] = (
CELL)1;
387 return sqrt(
var(statistics, band, band));
413 mean1 =
mean(statistics, band1);
414 mean2 =
mean(statistics, band2);
417 return product / n - mean1 * mean2;
450 return (product - sum1 * sum2 / n) / (n - 1);
462 *nbands = statistics->
nbands;
473 *cat = statistics->
cat;
505 *color = statistics->
color;
516 *ncells = statistics->
ncells;
527 *nstd = statistics->
nstd;
538 statistics->
nstd = nstd;
554 if (band >= statistics->
nbands) {
577 if (band >= statistics->
nbands) {
600 if (band >= statistics->
nbands) {
623 if (band >= statistics->
nbands) {
646 if (band >= statistics->
nbands) {
673 if (band >= statistics->
nbands) {
677 if (cat >= MAX_CATS) {
678 G_warning(
_(
"Cell category value out of range"));
703 int band2,
float *value)
705 if (band1 >= statistics->
nbands || band2 >= statistics->
nbands) {
728 if (band >= statistics->
nbands) {
751 if (band >= statistics->
nbands) {
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
void G_free(void *)
Free allocated memory.
void G_warning(const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
int G_debug(int, const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
void Rast_set_c_color(CELL, int, int, int, struct Colors *)
Set a category color (CELL)
void Rast_close(int)
Close a raster map.
CELL * Rast_allocate_c_buf(void)
Allocate memory for a CELL type raster map.
void Rast_init_colors(struct Colors *)
Initialize color structure.
int Rast_window_cols(void)
Number of columns in active window.
void Rast_put_row(int, const void *, RASTER_MAP_TYPE)
Writes the next row for cell/fcell/dcell file.
int Rast_window_rows(void)
Number of rows in active window.
int Rast_open_c_new(const char *)
Opens a new cell file in a database (compressed)
void Rast_write_colors(const char *, const char *, struct Colors *)
Write map layer color table.
void read_band_row(CELL **band_buffer, int *band_fd, int nbands, int row)
Read one row of each band.
void I_iclass_statistics_set_nstd(IClass_statistics *statistics, float nstd)
Set the multiplier of standard deviation.
int I_iclass_statistics_get_histo(IClass_statistics *statistics, int band, int cat, int *value)
Get histogram value in band.
void I_iclass_statistics_get_nstd(IClass_statistics *statistics, float *nstd)
Get the multiplier of standard deviation.
void alloc_statistics(IClass_statistics *statistics, int nbands)
Allocate space for statistics.
int I_iclass_statistics_get_sum(IClass_statistics *statistics, int band, float *sum)
Get sum of values in band.
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 I_iclass_statistics_get_mean(IClass_statistics *statistics, int band, float *mean)
Get mean of cell category values 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_max(IClass_statistics *statistics, int band, int *max)
Get maximum value in band.
float mean(IClass_statistics *statistics, int band)
Helper function for computing mean.
void I_iclass_init_statistics(IClass_statistics *statistics, int category, const char *name, const char *color, float nstd)
Initialize statistics.
float stddev(IClass_statistics *statistics, int band)
Helper function for standard deviation.
void create_raster(IClass_statistics *statistics, CELL **band_buffer, int *band_fd, const char *raster_name)
Create raster map based on statistics.
int I_iclass_statistics_get_product(IClass_statistics *statistics, int band1, int band2, float *value)
Get product value.
float var_signature(IClass_statistics *statistics, int band1, int band2)
Helper function for computing variance for signature file.
float var(IClass_statistics *statistics, int band1, int band2)
Helper function for computing variance.
void I_iclass_statistics_get_nbands(IClass_statistics *statistics, int *nbands)
Get number of bands.
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_free_statistics(IClass_statistics *statistics)
Free space allocated for statistics attributes.
int I_iclass_statistics_get_min(IClass_statistics *statistics, int band, int *min)
Get minimum value in band.
int make_all_statistics(IClass_statistics *statistics, IClass_perimeter_list *perimeters, CELL **band_buffer, int *band_fd)
Calculate statistics for all training areas.
void I_iclass_statistics_get_ncells(IClass_statistics *statistics, int *ncells)
Get number of cells in training areas.
void I_iclass_statistics_get_cat(IClass_statistics *statistics, int *cat)
Get category (class).
void I_iclass_statistics_get_name(IClass_statistics *statistics, const char **name)
Get category (class) name.
int I_iclass_statistics_get_stddev(IClass_statistics *statistics, int band, float *stddev)
Get standard deviation of cell category values in band.
void I_iclass_statistics_get_color(IClass_statistics *statistics, const char **color)
Get category (class) color.
int make_statistics(IClass_statistics *statistics, IClass_perimeter *perimeter, CELL **band_buffer, int *band_fd)
Calculate statistics for one training area.