iclass_signatures.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*!
  2. \file lib/imagery/iclass_statistics.c
  3. \brief Imagery library - functions for wx.iclass
  4. Computation based on training areas for supervised classification.
  5. Based on i.class module (GRASS 6).
  6. Computation and writing signatures to file.
  7. Copyright (C) 1999-2007, 2011 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. \author David Satnik, Central Washington University (original author)
  11. \author Markus Neteler <neteler itc.it> (i.class module)
  12. \author Bernhard Reiter <bernhard intevation.de> (i.class module)
  13. \author Brad Douglas <rez touchofmadness.com>(i.class module)
  14. \author Glynn Clements <glynn gclements.plus.com> (i.class module)
  15. \author Hamish Bowman <hamish_b yahoo.com> (i.class module)
  16. \author Jan-Oliver Wagner <jan intevation.de> (i.class module)
  17. \author Anna Kratochvilova <kratochanna gmail.com> (rewriting for wx.iclass)
  18. \author Vaclav Petras <wenzeslaus gmail.com> (rewriting for wx.iclass)
  19. */
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <grass/imagery.h>
  23. #include <grass/glocale.h>
  24. #include <grass/colors.h>
  25. #include "iclass_local_proto.h"
  26. /*!
  27. \brief Initialize signatures.
  28. \param[out] sigs pointer to signatures
  29. \param refer pointer to band files structure
  30. \return 1 on success
  31. \return 0 on failure
  32. */
  33. int I_iclass_init_signatures(struct Signature *sigs, struct Ref *refer)
  34. {
  35. G_debug(3, "I_iclass_init_signatures()");
  36. I_init_signatures(sigs, refer->nfiles);
  37. for (unsigned int i = refer->nfiles; i--;) {
  38. sigs->semantic_labels[i] = Rast_get_semantic_label_or_name(refer->file[i].name, refer->file[i].mapset);
  39. }
  40. return 1;
  41. }
  42. /*!
  43. \brief Add one signature.
  44. \param[out] sigs pointer to signatures
  45. \param statistics pointer to statistics structure
  46. */
  47. void I_iclass_add_signature(struct Signature *sigs,
  48. IClass_statistics * statistics)
  49. {
  50. int sn;
  51. int b1, b2;
  52. int r, g, b;
  53. G_debug(3, "I_iclass_add_signature()");
  54. G_str_to_color(statistics->color, &r, &g, &b);
  55. /* get a new signature */
  56. I_new_signature(sigs);
  57. /* save the signature in a Sig structure */
  58. sn = sigs->nsigs;
  59. strcpy(sigs->sig[sn - 1].desc, statistics->name);
  60. sigs->sig[sn - 1].npoints = statistics->ncells;
  61. sigs->sig[sn - 1].status = 1;
  62. sigs->sig[sn - 1].have_color = 1;
  63. sigs->sig[sn - 1].r = r;
  64. sigs->sig[sn - 1].g = g;
  65. sigs->sig[sn - 1].b = b;
  66. for (b1 = 0; b1 < sigs->nbands; b1++) {
  67. sigs->sig[sn - 1].mean[b1] = statistics->band_mean[b1];
  68. for (b2 = 0; b2 <= b1; b2++) {
  69. sigs->sig[sn - 1].var[b1][b2] = var_signature(statistics, b1, b2);
  70. }
  71. }
  72. }
  73. /*!
  74. \brief Write signtures to signature file.
  75. \param sigs pointer to signatures
  76. \param file_name name of signature file
  77. \return 1 on success
  78. \return 0 on failure
  79. */
  80. int I_iclass_write_signatures(struct Signature *sigs, const char *file_name)
  81. {
  82. FILE *outsig_fd;
  83. G_debug(3, "I_write_signatures(): file_name=%s", file_name);
  84. if (!
  85. (outsig_fd =
  86. I_fopen_signature_file_new(file_name))) {
  87. G_warning(_("Unable to open output signature file '%s'"), file_name);
  88. return 0;
  89. }
  90. I_write_signatures(outsig_fd, sigs);
  91. fclose(outsig_fd);
  92. return 1;
  93. }