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
012import imagingbook.pub.histogram.PiecewiseLinearCdf;
013import imagingbook.pub.histogram.Util;
014
015public class HistogramMatcher {
016        
017        public HistogramMatcher() {
018        }
019        
020        /**
021         * @param hA histogram of target image
022         * @param hR reference histogram
023         * @return the mapping function fhs() to be applied to the target image as an int table.
024         */
025        public int[] matchHistograms (int[] hA, int[] hR) {
026                int K = hA.length;
027                double[] PA = Util.Cdf(hA); // get CDF of histogram hA
028                double[] PR = Util.Cdf(hR); // get CDF of histogram hR
029                int[] fhs = new int[K]; // pixel mapping function f()
030
031                // compute pixel mapping function f():
032                for (int a = 0; a < K; a++) {
033                        int j = K - 1;
034                        do {
035                                fhs[a] = j;
036                                j--;
037                        } while (j >= 0 && PA[a] <= PR[j]);
038                }
039                return fhs;
040        }
041        
042        public int[] matchHistograms(int[] hA, PiecewiseLinearCdf PR) {
043                int K = hA.length;
044                double[] PA = Util.Cdf(hA); // get p.d.f. of histogram Ha
045                int[] F = new int[K]; // pixel mapping function f()
046
047                // compute pixel mapping function f():
048                for (int a = 0; a < K; a++) {
049                        double b = PA[a];
050                        F[a] = PR.getInverseCdf(b);
051                }
052                return F;
053        }
054}