Java RGB Color Create RGB_MSE(int[] rgb1, int[] rgb2)

Here you can find the source of RGB_MSE(int[] rgb1, int[] rgb2)

Description

Calculate the mean square error of the two RGB data

License

Open Source License

Parameter

Parameter Description
rgb1 first RGB data
rgb2 second RGB data

Return

the mean square error

Declaration

public static double RGB_MSE(int[] rgb1, int[] rgb2) 

Method Source Code

//package com.java2s;
/*//w  ww .  j  av  a 2s .c o  m
 * ArikTools
 * Copyright (C) Arik Z.Lakritz, Peter Macko, and David K. Wittenberg
 * 
 * This file is part of ArikTools.
 *
 * ArikTools 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.
 *
 * ArikTools 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 ArikTools.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Calculate the mean square error of the two RGB data
     *
     * @param rgb1 first RGB data
     * @param rgb2 second RGB data
     * @return the mean square error
     */
    public static double RGB_MSE(int[] rgb1, int[] rgb2) {

        // Assertion

        if (rgb1.length != rgb2.length)
            throw new RuntimeException("The images need to have the same number of pixels");

        // Calculate the MSE

        double mse = 0;

        for (int i = 0; i < rgb1.length; i++) {
            int a = rgb1[i];
            int b = rgb2[i];
            mse += sqr((a & 0xff) - (b & 0xff));
            mse += sqr(((a >> 8) & 0xff) - ((b >> 8) & 0xff));
            mse += sqr(((a >> 16) & 0xff) - ((b >> 16) & 0xff));
        }

        return mse / (rgb1.length * 3);
    }

    /**
     * Calculate a square of a number
     *
     * @param x the number
     * @return x^2
     */
    public static int sqr(int x) {
        return x * x;
    }

    /**
     * Calculate a square of a number
     *
     * @param x the number
     * @return x^2
     */
    public static long sqr(long x) {
        return x * x;
    }

    /**
     * Calculate a square of a number
     *
     * @param x the number
     * @return x^2
     */
    public static float sqr(float x) {
        return x * x;
    }

    /**
     * Calculate a square of a number
     *
     * @param x the number
     * @return x^2
     */
    public static double sqr(double x) {
        return x * x;
    }
}

Related

  1. rgb(int argb)
  2. rgb(int pixel)
  3. rgb(int r, int g, int b)
  4. rgb(int red, int green, int blue)
  5. RGB_GREEN(int rgb)
  6. RGB_PNSR(int[] rgb1, int[] rgb2)
  7. rgb_xyz(double r)
  8. rgba(double r, double g, double b, double a)
  9. RGBA(int r, int g, int b, float a)