iclass_signatures.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. if (!I_init_signatures(sigs, refer->nfiles))
  37. return 1; /* success */
  38. return 0;
  39. }
  40. /*!
  41. \brief Add one signature.
  42. \param[out] sigs pointer to signatures
  43. \param statistics pointer to statistics structure
  44. */
  45. void I_iclass_add_signature(struct Signature *sigs,
  46. IClass_statistics * statistics)
  47. {
  48. int sn;
  49. int b1, b2;
  50. int r, g, b;
  51. G_debug(3, "I_iclass_add_signature()");
  52. G_str_to_color(statistics->color, &r, &g, &b);
  53. /* get a new signature */
  54. I_new_signature(sigs);
  55. /* save the signature in a Sig structure */
  56. sn = sigs->nsigs;
  57. strcpy(sigs->sig[sn - 1].desc, statistics->name);
  58. sigs->sig[sn - 1].npoints = statistics->ncells;
  59. sigs->sig[sn - 1].status = 1;
  60. sigs->sig[sn - 1].have_color = 1;
  61. sigs->sig[sn - 1].r = r;
  62. sigs->sig[sn - 1].g = g;
  63. sigs->sig[sn - 1].b = b;
  64. for (b1 = 0; b1 < sigs->nbands; b1++) {
  65. sigs->sig[sn - 1].mean[b1] = statistics->band_mean[b1];
  66. for (b2 = 0; b2 <= b1; b2++) {
  67. sigs->sig[sn - 1].var[b1][b2] = var_signature(statistics, b1, b2);
  68. }
  69. }
  70. }
  71. /*!
  72. \brief Write signtures to signature file.
  73. \param sigs pointer to signatures
  74. \param group image group
  75. \param sub_group image subgroup
  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 *group,
  81. const char *sub_group, const char *file_name)
  82. {
  83. FILE *outsig_fd;
  84. G_debug(3, "I_write_signatures(): group=%s, file_name=%s", group,
  85. file_name);
  86. if (!
  87. (outsig_fd =
  88. I_fopen_signature_file_new(group, sub_group, file_name))) {
  89. G_warning(_("Unable to open output signature file '%s'"), file_name);
  90. return 0;
  91. }
  92. I_write_signatures(outsig_fd, sigs);
  93. fclose(outsig_fd);
  94. return 1;
  95. }