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.moments; 011import ij.process.ImageProcessor; 012 013public class BinaryMoments { 014 015 static final int BACKGROUND = 0; 016 017 public static double moment(ImageProcessor I, int p, int q) { 018 double Mpq = 0.0; 019 for (int v = 0; v < I.getHeight(); v++) { 020 for (int u = 0; u < I.getWidth(); u++) { 021 if (I.getPixel(u, v) != BACKGROUND) { 022 Mpq+= Math.pow(u, p) * Math.pow(v, q); 023 } 024 } 025 } 026 return Mpq; 027 } 028 029 public static double centralMoment(ImageProcessor I, int p, int q) { 030 double m00 = moment(I, 0, 0); // region area 031 double xCtr = moment(I, 1, 0) / m00; 032 double yCtr = moment(I, 0, 1) / m00; 033 double cMpq = 0.0; 034 for (int v = 0; v < I.getHeight(); v++) { 035 for (int u = 0; u < I.getWidth(); u++) { 036 if (I.getPixel(u, v) != BACKGROUND) { 037 cMpq+= Math.pow(u - xCtr, p) * Math.pow(v - yCtr, q); 038 } 039 } 040 } 041 return cMpq; 042 } 043 044 @Deprecated // renamed to nCentralMoment 045 public static double normalCentralMoment(ImageProcessor I, int p, int q) { 046 double m00 = moment(I, 0, 0); 047 double norm = Math.pow(m00, 0.5 * (p + q + 2)); 048 return centralMoment(I, p, q) / norm; 049 } 050 051 public static double nCentralMoment(ImageProcessor I, int p, int q) { 052 double m00 = moment(I, 0, 0); 053 double norm = Math.pow(m00, 0.5 * (p + q + 2)); 054 return centralMoment(I, p, q) / norm; 055 } 056} 057