Function to normalize the images between 0 and 255 - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

Function to normalize the images between 0 and 255

Demo Code


//package com.java2s;

public class Main {
    /**//  w w w. j a va2 s  . c o  m
     *
     * Function to normalize the images between 0 and 255
     *
     **/
    public static void image_normalization(final float[] I1, // first image
            final float[] I2, // second image
            float[] I1n, // first normalized image
            float[] I2n, // second normalized image
            int size // size of each image
    ) {
        // find the max and min of both images
        final float max1 = max_element(I1, size);
        final float max2 = max_element(I2, size);
        final float min1 = min_element(I1, size);
        final float min2 = min_element(I2, size);

        // obtain the absolute max and min
        final float max = max1 > max2 ? max1 : max2;
        final float min = min1 < min2 ? min1 : min2;
        final float den = max - min;

        if (den > 0)
            // normalize both images
            for (int i = 0; i < size; i++) {
                I1n[i] = 255.0f * (I1[i] - min) / den;
                I2n[i] = 255.0f * (I2[i] - min) / den;
            }

        else
            // copy the original images
            for (int i = 0; i < size; i++) {
                I1n[i] = I1[i];
                I2n[i] = I2[i];
            }
    }

    static float max_element(final float[] x, int n) {
        int r = 0;
        for (int i = 1; i < n; i++)
            if (x[i] > x[r])
                r = i;
        return x[r];
    }

    static float min_element(final float[] x, int n) {
        int r = 0;
        for (int i = 1; i < n; i++)
            if (x[i] < x[r])
                r = i;
        return x[r];
    }
}

Related Tutorials