GRASS GIS 7 Programmer's Manual  7.7.svn(2018)-r73679
c_sep.c
Go to the documentation of this file.
1 /*!
2  \file cluster/c_sep.c
3
4  \brief Cluster library - Separation
5
6  (C) 2001-2009 by the GRASS Development Team
7
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
11  \author Original author CERL
12 */
13
14 #include <math.h>
15 #include <grass/cluster.h>
16
17 #define FAR ((double) -1.0)
18
19 /*!
20  \brief ?
21
22  \param C pointer to Cluster structure
23  \param class1 1st class
24  \param class2 2nd class
25 */
26 double I_cluster_separation(struct Cluster *C, int class1, int class2)
27 {
28  int band;
29  double q;
30  double d;
31  double var;
32  double a1, a2;
33  double n1, n2;
34  double m1, m2;
35  double s1, s2;
36
37  if (C->count[class1] < 2)
38  return FAR;
39  if (C->count[class2] < 2)
40  return FAR;
41  n1 = (double)C->count[class1];
42  n2 = (double)C->count[class2];
43
44  d = 0.0;
45  a1 = a2 = 0.0;
46  for (band = 0; band < C->nbands; band++) {
47  s1 = C->sum[band][class1];
48  s2 = C->sum[band][class2];
49  m1 = s1 / n1;
50  m2 = s2 / n2;
51  q = m1 - m2;
52  q = q * q;
53  d += q;
54
55
56  var = C->sum2[band][class1] - (s1 * m1);
57  var /= n1 - 1;
58  if (var)
59  a1 += q / var;
60
61  var = C->sum2[band][class2] - (s2 * m2);
62  var /= n2 - 1;
63  if (var)
64  a2 += q / var;
65  }
66  if (d == 0.0)
67  return d;
68
69  if (a1 < 0 || a2 < 0)
70  return FAR;
71  if (a1)
72  a1 = sqrt(6 * d / a1);
73  if (a2)
74  a2 = sqrt(6 * d / a2);
75  q = a1 + a2;
76  if (q == 0.0)
77  return FAR;
78
79  return (sqrt(d) / q);
80 }
int nbands
Definition: cluster.h:9
double ** sum
Definition: cluster.h:21
Definition: cluster.h:7
int * count
Definition: cluster.h:19
double ** sum2
Definition: cluster.h:23
double I_cluster_separation(struct Cluster *C, int class1, int class2)
?
Definition: c_sep.c:26
float var(IClass_statistics *statistics, int band1, int band2)
Helper function for computing variance.
#define FAR
Definition: c_sep.c:17