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-2023 Wilhelm Burger, Mark J. Burge. All rights reserved. 007 * Visit https://imagingbook.com for additional details. 008 ******************************************************************************/ 009package Ch02_Histograms_Statistics; 010 011import ij.ImagePlus; 012import ij.plugin.filter.PlugInFilter; 013import ij.process.ByteProcessor; 014import ij.process.FloatProcessor; 015import ij.process.ImageProcessor; 016import imagingbook.common.ij.DialogUtils; 017import imagingbook.common.image.IntegralImage; 018import imagingbook.core.jdoc.JavaDocHelp; 019import imagingbook.sampleimages.GeneralSampleImage; 020 021import static imagingbook.common.ij.IjUtils.noCurrentImage; 022 023/** 024 * <p> 025 * This ImageJ plugin calculates the integral image for the current image (8 bit grayscale only) and displays the 026 * resulting first and second order summed area tables (S1, S2) as floating-point images. See Sec. 2.8.1 of [1] for 027 * additional details. 028 * </p> 029 * <p> 030 * [1] W. Burger, M.J. Burge, <em>Digital Image Processing – An Algorithmic Introduction</em>, 3rd ed, Springer 031 * (2022). 032 * </p> 033 * 034 * @author WB 035 */ 036public class Integral_Image_Demo implements PlugInFilter, JavaDocHelp { 037 038 /** Constructor, asks to open a predefined sample image if no other image is currently open. */ 039 public Integral_Image_Demo() { 040 if (noCurrentImage()) { 041 DialogUtils.askForSampleImage(GeneralSampleImage.IrishManor); 042 } 043 } 044 045 @Override 046 public int setup(String arg0, ImagePlus im) { 047 return DOES_8G + NO_CHANGES; 048 } 049 050 @Override 051 public void run(ImageProcessor ip) { 052 IntegralImage iI = new IntegralImage((ByteProcessor) ip); 053 long[][] S1 = iI.getS1(); 054 long[][] S2 = iI.getS2(); 055 056 FloatProcessor fS1 = new FloatProcessor(toFloatArray(S1)); 057 new ImagePlus("S1", fS1).show(); 058 059 FloatProcessor fS2 = new FloatProcessor(toFloatArray(S2)); 060 new ImagePlus("S2", fS2).show(); 061 } 062 063 private float[][] toFloatArray(long[][] A) { 064 float[][] B = new float[A.length][A[0].length]; 065 for (int i = 0; i < A.length; i++) { 066 for (int j = 0; j < A[0].length; j++) { 067 B[i][j] = (float) A[i][j]; 068 } 069 } 070 return B; 071 } 072 073}