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 ij.ImagePlus; 013import ij.gui.NewImage; 014import ij.process.ImageProcessor; 015import imagingbook.pub.histogram.HistogramPlot; 016import imagingbook.pub.histogram.PiecewiseLinearCdf; 017import imagingbook.pub.histogram.Util; 018 019public class HistogramPlot { 020 // TODO: make HistogramPlot extend ImagePlus 021 022 static final int BACKGROUND = 255; 023 024 int width = 256; 025 int height = 128; 026 int base = height-1; 027 int paintValue = 0; 028 ImagePlus hist_img; 029 ImageProcessor ip; 030 int[] H = new int[256]; 031 032 public HistogramPlot(int[] h, String title) { 033 this(Util.normalizeHistogram(h), title); 034 } 035 036 public HistogramPlot(double[] nH, String title) { 037 createHistogramImage(title); 038 // nH must be a normalized histogram of length 256 039 for (int i = 0; i < nH.length; i++) { 040 H[i] = (int) Math.round(height * nH[i]); 041 } 042 draw(); 043 //show(); 044 } 045 046 public HistogramPlot(PiecewiseLinearCdf cdf, String title) { 047 // TODO: needed? 048 createHistogramImage(title); 049 // nH must be a normalized histogram of length 256 050 for (int i = 0; i < 256; i++) { 051 H[i] = (int) Math.round(height * cdf.getCdf(i)); 052 } 053 draw(); 054 //show(); 055 } 056 057 void createHistogramImage(String title) { 058 if (title == null) 059 title = "Histogram Plot"; 060 hist_img = NewImage.createByteImage(title,width,height,1,0); 061 ip = hist_img.getProcessor(); 062 ip.setValue(BACKGROUND); 063 ip.fill(); 064 } 065 066 void draw() { 067 ip.setValue(0); 068 ip.drawLine(0,base,width-1,base); 069 ip.setValue(paintValue); 070 int u = 0; 071 for (int i=0; i<H.length; i++) { 072 int k = H[i]; 073 if (k > 0){ 074 ip.drawLine(u, base-1, u, base-k); 075 } 076 u = u + 1; 077 } 078 } 079 080 void update() { 081 hist_img.updateAndDraw(); 082 } 083 084 public void show() { 085 hist_img.show(); 086 update(); 087 } 088 089 void makeRamp() { 090 for (int i = 0; i < H.length; i++) { 091 H[i] = i; 092 } 093 } 094 095 void makeRandom() { 096 for (int i = 0; i < H.length; i++) { 097 H[i] = (int)(Math.random() * height); 098 } 099 } 100 101 //----- static methods ---------------------- 102 103// public static void showHistogram(ImageProcessor ip, String title) { 104// int[] Ha = ip.getHistogram(); 105// HistogramPlot hp = new HistogramPlot(Util.normalizeHistogram(Ha), title); 106// hp.show(); 107// } 108 109// public static void showCumHistogram(ImageProcessor ip, String title) { 110// int[] Ha = ip.getHistogram(); 111// HistogramPlot hp = new HistogramPlot(Util.Cdf(Ha), title); 112// hp.show(); 113// } 114 115}