GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
histo_eq.c
Go to the documentation of this file.
1 
2 /**************************************************************
3 * G_histogram_eq (histo, map, min, max)
4 *
5 * struct Histogram *histo; histogram as returned by G_read_histogram()
6 * unsigned char **map; equalized category mapping
7 * CELL *min, *max; min,max category for map
8 *
9 * perform histogram equalization
10 * inputs are histo, output is map,min,max
11 ****************************************************************/
12 #include <grass/gis.h>
13 int G_histogram_eq(const struct Histogram *histo,
14  unsigned char **map, CELL * min, CELL * max)
15 {
16  int i;
17  int x;
18  CELL cat, prev;
19  double total;
20  double sum;
21  double span;
22  int ncats;
23  long count;
24  unsigned char *xmap;
25  int len;
26  int first, last;
27 
28  ncats = G_get_histogram_num(histo);
29  if (ncats == 1) {
30  *min = *max = G_get_histogram_cat(0, histo);
31  *map = xmap = (unsigned char *)G_malloc(1);
32  *xmap = 0;
33  return 0;
34  }
35  if ((*min = G_get_histogram_cat(first = 0, histo)) == 0)
36  *min = G_get_histogram_cat(++first, histo);
37  if ((*max = G_get_histogram_cat(last = ncats - 1, histo)) == 0)
38  *max = G_get_histogram_cat(--last, histo);
39  len = *max - *min + 1;
40  *map = xmap = (unsigned char *)G_malloc(len);
41 
42  total = 0;
43  for (i = first; i <= last; i++) {
44  if (G_get_histogram_cat(i, histo) == 0)
45  continue;
46  count = G_get_histogram_count(i, histo);
47  if (count > 0)
48  total += count;
49  }
50  if (total <= 0) {
51  for (i = 0; i < len; i++)
52  xmap[i] = 0;
53  return 0;
54  }
55 
56  span = total / 256;
57 
58  sum = 0.0;
59  cat = *min - 1;
60  for (i = first; i <= last; i++) {
61  prev = cat + 1;
62  cat = G_get_histogram_cat(i, histo);
63  count = G_get_histogram_count(i, histo);
64  if (count < 0 || cat == 0)
65  count = 0;
66  x = (sum + (count / 2.0)) / span;
67  if (x < 0)
68  x = 0;
69  else if (x > 255)
70  x = 255;
71  sum += count;
72 
73  while (prev++ <= cat)
74  *xmap++ = x;
75  }
76 
77  return 0;
78 }
int G_get_histogram_num(const struct Histogram *histogram)
Sorts the histogram in ascending order by counts then category.
Definition: histogram.c:188
#define min(x, y)
Definition: draw2.c:68
int count
#define max(x, y)
Definition: draw2.c:69
CELL G_get_histogram_cat(int n, const struct Histogram *histogram)
Returns cat for the nth element in the histogram.
Definition: histogram.c:201
int G_histogram_eq(const struct Histogram *histo, unsigned char **map, CELL *min, CELL *max)
Definition: histo_eq.c:13
int first
Definition: form/open.c:25
long G_get_histogram_count(int n, const struct Histogram *histogram)
Returns count for the nth element in the histogram.
Definition: histogram.c:218
CELL cat
Definition: g3dcats.c:90