Java Array Normalize normalCentralMoment(boolean[][] img, int p, int q)

Here you can find the source of normalCentralMoment(boolean[][] img, int p, int q)

Description

Computes the (normal) central moment.

License

Open Source License

Parameter

Parameter Description
img the matrix
p the p parameter, exponent for x
q the q parameter, exponent for y

Return

the moment

Declaration

public static double normalCentralMoment(boolean[][] img, int p, int q) 

Method Source Code

//package com.java2s;
/*//from w w w. j a  va2s  .  c om
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Computes the (normal) central moment.
     *
     * @param img      the matrix
     * @param p      the p parameter, exponent for x
     * @param q      the q parameter, exponent for y
     * @return      the moment
     * @see      #centralMoment(boolean[][], int, int)
     */
    public static double normalCentralMoment(boolean[][] img, int p, int q) {
        double result;
        double m00 = moment(img, 0, 0);
        double norm = Math.pow(m00, (double) (p + q + 2) / 2);
        result = centralMoment(img, p, q) / norm;
        return result;
    }

    /**
     * Computes the moment.
     *
     * @param img      the matrix
     * @param p      the p parameter, exponent for x
     * @param q      the q parameter, exponent for y
     * @return      the moment
     */
    public static double moment(boolean[][] img, int p, int q) {
        double result = 0.0;
        // For every pixel that is not a background pixel
        for (int v = 0; v < img.length; v++) {
            for (int u = 0; u < img[v].length; u++) {
                if (img[v][u]) {
                    // sum u^p * v^q
                    result += Math.pow(u, p) * Math.pow(v, q);
                }
            }
        }
        return result;
    }

    /**
     * Computes the central moment.
     * See <a href="https://en.wikipedia.org/wiki/Image_moment#Central_moments">here</a>
     *
     * @param img      the matrix
     * @param p      the p parameter, exponent for x
     * @param q      the q parameter, exponent for y
     * @return      the moment
     */
    public static double centralMoment(boolean[][] img, int p, int q) {
        double result = 0.0;
        double area = moment(img, 0, 0); // gives the area of the region
        double xCenter = moment(img, 1, 0) / area;
        double yCenter = moment(img, 0, 1) / area;
        for (int v = 0; v < img.length; v++) {
            for (int u = 0; u < img[v].length; u++) {
                if (img[v][u])
                    result += Math.pow(u - xCenter, p) * Math.pow(v - yCenter, q);
            }
        }
        return result;
    }
}

Related

  1. norm2(double[] x)
  2. norm2DoubleArray(double[] v)
  3. norm_variance(double[] array)
  4. norm_vec(double[] vec)
  5. normal0(double[] p1, double[] p2, double[] p3)
  6. normalDist(double[] features, double[] avg, double[][] cov)
  7. normalForm(int[] a)
  8. normalHashCode(char[] val)
  9. normalise(double[] a)