c_sig.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*!
  2. \file cluster/c_sig.c
  3. \brief Cluster library - Signatures
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <grass/cluster.h>
  10. /*!
  11. \brief Create signatures
  12. \param C pointer to Cluster structure
  13. \return 0
  14. */
  15. int I_cluster_signatures(struct Cluster *C)
  16. {
  17. int c, p, band1, band2;
  18. int n;
  19. double m1, m2;
  20. double p1, p2;
  21. double dn;
  22. /*
  23. fprintf (stderr, "c_sig: 1\n");
  24. fprintf (stderr, " nclasses %d\n", C->nclasses);
  25. fprintf (stderr, " npoints %d\n", C->npoints );
  26. fprintf (stderr, " nbands %d\n", C->nbands );
  27. */
  28. for (n = 0; n < C->nclasses; n++) {
  29. I_new_signature(&C->S);
  30. }
  31. for (p = 0; p < C->npoints; p++) {
  32. c = C->class[p];
  33. if (c < 0)
  34. continue;
  35. /*
  36. if (c >= C->nclasses)
  37. fprintf (stderr, " class[%d]=%d ** illegal **\n", p, c);
  38. */
  39. dn = n = C->count[c];
  40. if (n < 2)
  41. continue;
  42. for (band1 = 0; band1 < C->nbands; band1++) {
  43. m1 = C->sum[band1][c] / dn;
  44. p1 = C->points[band1][p];
  45. for (band2 = 0; band2 <= band1; band2++) {
  46. m2 = C->sum[band2][c] / dn;
  47. p2 = C->points[band2][p];
  48. C->S.sig[c].var[band1][band2] += (p1 - m1) * (p2 - m2);
  49. }
  50. }
  51. }
  52. for (c = 0; c < C->nclasses; c++) {
  53. dn = n = C->S.sig[c].npoints = C->count[c];
  54. if (n == 0)
  55. dn = 1.0;
  56. for (band1 = 0; band1 < C->nbands; band1++)
  57. C->S.sig[c].mean[band1] = C->sum[band1][c] / dn;
  58. dn = n = C->count[c] - 1;
  59. if (n < 1)
  60. continue;
  61. for (band1 = 0; band1 < C->nbands; band1++)
  62. for (band2 = 0; band2 <= band1; band2++)
  63. C->S.sig[c].var[band1][band2] /= dn;
  64. C->S.sig[c].status = 1;
  65. }
  66. return 0;
  67. }