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.color.statistics;
011import ij.process.ColorProcessor;
012
013import java.util.Arrays;
014
015public class ColorStatistics {
016        
017        //determine how many different colors are contained in the 24 bit full-color image cp
018        public static int countColors (ColorProcessor cp) { 
019                // duplicate pixel array and sort
020                int[] pixels = ((int[]) cp.getPixels()).clone();
021                Arrays.sort(pixels);  
022                
023                int k = 1;      // image contains at least one color
024                for (int i = 0; i < pixels.length-1; i++) {
025                        if (pixels[i] != pixels[i+1])
026                                k = k + 1;
027                }
028                return k;
029        }
030        
031        //computes the combined color histogram for color components (c1,c2)
032        static int[][] get2dHistogram (ColorProcessor cp, int c1, int c2) 
033        {       // c1, c2:  R = 0, G = 1, B = 2
034                int[] RGB = new int[3];
035                int[][] H = new int[256][256];  // histogram array H[i1][i2] 
036
037                for (int v = 0; v < cp.getHeight(); v++) {
038                        for (int u = 0; u < cp.getWidth(); u++) {
039                                cp.getPixel(u, v, RGB); 
040                                int i1 = RGB[c1];       
041                                int i2 = RGB[c2];       
042                                // increment corresponding histogram cell
043                                H[i1][i2]++;
044                        }
045                }       
046                return H;
047        }
048        
049
050}