c_sep.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*!
  2. \file cluster/c_sep.c
  3. \brief Cluster library - Separation
  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 <math.h>
  10. #include <grass/cluster.h>
  11. #define FAR ((double) -1.0)
  12. /*!
  13. \brief ?
  14. \param C pointer to Cluster structure
  15. \param class1 1st class
  16. \param class2 2nd class
  17. */
  18. double I_cluster_separation(struct Cluster *C, int class1, int class2)
  19. {
  20. int band;
  21. double q;
  22. double d;
  23. double var;
  24. double a1, a2;
  25. double n1, n2;
  26. double m1, m2;
  27. double s1, s2;
  28. if (C->count[class1] < 2)
  29. return FAR;
  30. if (C->count[class2] < 2)
  31. return FAR;
  32. n1 = (double)C->count[class1];
  33. n2 = (double)C->count[class2];
  34. d = 0.0;
  35. a1 = a2 = 0.0;
  36. for (band = 0; band < C->nbands; band++) {
  37. s1 = C->sum[band][class1];
  38. s2 = C->sum[band][class2];
  39. m1 = s1 / n1;
  40. m2 = s2 / n2;
  41. q = m1 - m2;
  42. q = q * q;
  43. d += q;
  44. var = C->sum2[band][class1] - (s1 * m1);
  45. var /= n1 - 1;
  46. if (var)
  47. a1 += q / var;
  48. var = C->sum2[band][class2] - (s2 * m2);
  49. var /= n2 - 1;
  50. if (var)
  51. a2 += q / var;
  52. }
  53. if (d == 0.0)
  54. return d;
  55. if (a1 < 0 || a2 < 0)
  56. return FAR;
  57. if (a1)
  58. a1 = sqrt(6 * d / a1);
  59. if (a2)
  60. a2 = sqrt(6 * d / a2);
  61. q = a1 + a2;
  62. if (q == 0.0)
  63. return FAR;
  64. return (sqrt(d) / q);
  65. }