001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 *  image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause 
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 
006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 
007 * Visit http://imagingbook.com for additional details.
008 *******************************************************************************/
009
010package imagingbook.pub.histogram;
011
012public class Util {
013
014        static int[] makeGaussianHistogram () {
015                return makeGaussianHistogram(128, 50);
016        }
017
018        public static int[] makeGaussianHistogram (double mean, double sigma) {
019                int[] h = new int[256];
020                double sigma2 = 2 * sigma * sigma;
021                for (int i = 0; i < h.length; i++) {
022                        double x = mean - i;
023                        double g = Math.exp(-(x * x) / sigma2) / sigma;
024                        h[i] = (int) Math.round(10000 * g);
025                }
026                return h;
027        }
028
029        public static double[] normalizeHistogram (double[] h) {
030                // find max histogram entry
031                double max = h[0];
032                for (int i = 0; i < h.length; i++) {
033                        if (h[i] > max)
034                                max = h[i];
035                }
036                if (max == 0) return null;
037                // normalize
038                double[] hn = new double[h.length];
039                double s = 1.0/max;
040                for (int i = 0; i < h.length; i++) {
041                        hn[i] = s * h[i];
042                }
043                return hn;
044        }
045
046        //------------------------------------------------------
047        
048        public static double[] normalizeHistogram (int[] h) {
049                // find the max histogram entry
050                int max = h[0];
051                for (int i = 0; i < h.length; i++) {
052                        if (h[i] > max)
053                                max = h[i];
054                }
055                if (max == 0) return null;
056                // normalize
057                double[] hn = new double[h.length];
058                double s = 1.0/max;
059                for (int i = 0; i < h.length; i++) {
060                        hn[i] = s * h[i];
061                }
062                return hn;
063        }
064
065        public static double[] Cdf (int[] h) {
066                // returns the cumul. probability distribution function (cdf) for histogram h
067                int K = h.length;
068                int n = 0;              // sum all histogram values             
069                for (int i=0; i<K; i++) {       
070                        n = n + h[i]; 
071                }
072                double[] P = new double[K];
073                int c = h[0];
074                P[0] = (double) c / n;
075                for (int i = 1; i < K; i++) {
076                c = c + h[i];
077                P[i] = (double) c / n;
078            }
079            return P;
080        }
081
082        static double[] Pdf (int[] h) {
083                // returns the probability distribution function (pdf) for histogram h
084                int K = h.length;
085                int n = 0;                      // sum all histogram values     
086                for (int i = 0; i < K; i++) {
087                        n = n + h[i]; 
088                }
089                double[] p = new double[K];
090                for (int i = 0; i < h.length; i++) {
091                        p[i] =  (double) h[i] / n;
092                }
093                return p;
094        }
095
096}