get_train.c 2.0 KB

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