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

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

Description

Calculate the peak signal-to-noise ratio

License

Open Source License

Parameter

Parameter Description
rgb1 first RGB data
rgb2 second RGB data

Return

the peak signal to noise ratio (dB)

Declaration

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

Method Source Code

//package com.java2s;
/*//w w w . j a v  a 2 s.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 peak signal-to-noise ratio
     *
     * @param rgb1 first RGB data
     * @param rgb2 second RGB data
     * @return the peak signal to noise ratio (dB)
     */
    public static double RGB_PNSR(int[] rgb1, int[] rgb2) {

        return PNSR(RGB_MSE(rgb1, rgb2), 255);
    }

    /**
     * Calculate the peak signal-to-noise ratio
     *
     * @param mse the mean square error
     * @param max the maximum signal intensity
     * @return the peak signal to noise ratio (dB)
     */
    public static double PNSR(double mse, int max) {

        return 10.0 * Math.log(sqr(max) / mse) / Math.log(10);
    }

    /**
     * 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 pixel)
  2. rgb(int r, int g, int b)
  3. rgb(int red, int green, int blue)
  4. RGB_GREEN(int rgb)
  5. RGB_MSE(int[] rgb1, int[] rgb2)
  6. rgb_xyz(double r)
  7. rgba(double r, double g, double b, double a)
  8. RGBA(int r, int g, int b, float a)
  9. RGBA(int r, int g, int b, int a)