histo_eq.c

Go to the documentation of this file.
00001 /**************************************************************
00002 * G_histogram_eq (histo, map, min, max)
00003 *
00004 *   struct Histogram *histo;    histogram as returned by G_read_histogram()
00005 *   unsigned char **map;        equalized category mapping
00006 *   CELL *min, *max;            min,max category for map
00007 *
00008 * perform histogram equalization
00009 * inputs are histo, output is map,min,max
00010 ****************************************************************/
00011 #include "gis.h"
00012 int G_histogram_eq (
00013     struct Histogram *histo,
00014     unsigned char **map,
00015     CELL *min,CELL *max)
00016 {
00017     int i;
00018     int x;
00019     CELL cat, prev;
00020     double total;
00021     double sum;
00022     double span;
00023     int ncats;
00024     long count;
00025     unsigned char *xmap;
00026     int len;
00027     int first, last;
00028 
00029     ncats = G_get_histogram_num (histo);
00030     if (ncats == 1)
00031     {
00032         *min = *max = G_get_histogram_cat (0, histo);
00033         *map = xmap = (unsigned char *) G_malloc (1);
00034         *xmap = 0;
00035         return 0;
00036     }
00037     if((*min = G_get_histogram_cat (first = 0, histo)) == 0)
00038         *min = G_get_histogram_cat (++first, histo);
00039     if((*max = G_get_histogram_cat (last = ncats-1, histo)) == 0)
00040         *max = G_get_histogram_cat (--last, histo);
00041     len = *max - *min + 1;
00042     *map = xmap = (unsigned char *) G_malloc (len);
00043 
00044     total = 0;
00045     for (i = first; i <= last; i++)
00046     {
00047         if (G_get_histogram_cat (i, histo) == 0)
00048                 continue;
00049         count = G_get_histogram_count (i, histo);
00050         if (count > 0) total += count;
00051     }
00052     if (total <= 0)
00053     {
00054         for (i = 0; i < len; i++)
00055             xmap[i] = 0;
00056         return 0;
00057     }
00058 
00059     span = total / 256 ;
00060 
00061     sum = 0.0;
00062     cat = *min-1;
00063     for (i = first; i <= last; i++)
00064     {
00065         prev = cat+1;
00066         cat   = G_get_histogram_cat (i, histo);
00067         count = G_get_histogram_count (i, histo);
00068         if (count < 0 || cat == 0)
00069             count = 0;
00070         x = (sum + (count / 2.0)) / span;
00071         if (x < 0) x = 0;
00072         else if (x > 255) x = 255;
00073         sum += count;
00074 
00075         while (prev++ <= cat)
00076             *xmap++ = x ;
00077     }
00078 
00079     return 0;
00080 }

Generated on Wed Aug 23 17:49:22 2006 for GRASS by  doxygen 1.4.7