Negative value when calculating the variance of an image -java-

385 views Asked by At

Here I try to calculate variance of an image, starting with the varince of each color in this image (I have caluculated the variance of the red color but the result is -1)

I use the following formula "var (x) = E (x²) -E (x) ²".

    private void openActionPerformed(java.awt.event.ActionEvent evt) {                                     
    LoadImage loadImage = new LoadImage(null);
    int w, h;
    BufferedImage image;
    float somR = 0;
    int somG = 0;
    int somB = 0;
    int valR[][];
    int[][] valG;
    int valB[][];
    double meanR = 0;
    double meanG = 0;
    double meanB = 0;
    int nbpixl = 0;
    try {
        if (loadImage.loadImage(jScrollPane1)) {
            int pixel = 0;

            image = loadImage.getImage();
            w = image.getWidth();
            h = image.getHeight();
            System.out.println("width, height: " + w + ", " + h);
            nbpixl = w * h;

            valR = new int[h][w];
            valB = new int[h][w];
            valG = new int[h][w];
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {

                    pixel = image.getRGB(j, i);
                    //rgb argb

                    //int alpha = (pixel >> 24) & 0xff;
                    int red = (pixel >> 16) & 0xff;
                    int green = (pixel >> 8) & 0xff;
                    int blue = (pixel) & 0xff;

                    valR[i][j] = red;
                    somR += red;

                    valG[i][j] = green;
                    somG += green;

                    valB[i][j] = blue;
                    somB += blue;

                    System.out.println("");

                }

            }

            System.out.println("red " + somR);
            System.out.println("green " + somG);
            System.out.println("blue " + somB);
            meanR = somR / nbpixl;
            meanG = somG / nbpixl;
            meanB = somB / nbpixl;
            System.out.println("meanR" + meanR);
            System.out.println("meanV" + meanG);
            System.out.println("meanB" + meanB);



            float summSquareR = somR * somR;
            float meanSquareR = summSquareR / nbpixl;

            System.out.println("summSquareR" + summSquareR);
            System.out.println("meanSquareR" + meanSquareR);
            //float varRrr=(float) (meanSquareR-meanR*meanR);

            //var (x) = E (x²) -E (x) ²

            byte varR = (byte) (255f * (meanSquareR - meanR * meanR));
            // byte varG=(byte)(225f*((somG*somG)/nbpixl)-((somG/nbpixl)*(somG/nbpixl)));
            System.out.println(somB + "\t" + somR + "\t" + somG);
            System.out.println("varR\t" + varR);

        }
    } catch (IOException ex) {
        Logger.getLogger(var_img.class.getName()).log(Level.SEVERE, null, ex);
    }
}

And for the result width, height: 259, 194

red 5137653.0

green 4165933

blue 4142841

meanR102.24999237060547

meanV82.0

meanB82.0

summSquareR2.63954786E13

meanSquareR5.2532496E8

4142841 5137653.0 4165933

varR -1

2

There are 2 answers

1
gpasch On BEST ANSWER

You have to understand the formula:

E[X^2]=(x1*x1+x2*x2+...+xn*xn)/n

A correct way to use this formula is the following - d and d2 should come out the same:

int[] x={1, 2, 5, 7};
int s=0;
for(int i=0; i<x.length; i++) s+=x[i];
double m=s/4.;
double d=0;
for(int i=0; i<x.length; i++) d+=(x[i]-m)*(x[i]-m);
d/=4.;
int s2=0;
for(int i=0; i<x.length; i++) s2+=x[i]*x[i];
double m2=s2/4.;
double d2=m2-m*m;

System.out.println(m+" "+d+" "+d2+" "+m2);
1
Younes On

enter code here

    private void openActionPerformed(java.awt.event.ActionEvent evt) {
    LoadImage loadImage = new LoadImage(null);
    int w = 0, h = 0;
    BufferedImage image;
    float somR = 0;
    int somG = 0;
    int somB = 0;
    int valR[][];
    int[][] valG;
    int valB[][];
    float meanR = 0;
    float meanG = 0;
    float meanB = 0;
    int nbpixl = 0;

    float square_Red = 0, square_Blue = 0, square_Green = 0;
    //The sum of pixels for each color
    int sum_Square_Red = 0, sum_Square_Blue = 0, sum_Square_Green = 0;
    // The square sum of the pixels for each color
    float varR = 0, varG = 0, varB = 0;//The variance of each color
    try {
        if (loadImage.loadImage(jScrollPane1)) {
            int pixel = 0;

            image = loadImage.getImage();
            w = image.getWidth();
            h = image.getHeight();
            System.out.println("width, height: " + w + ", " + h);
            nbpixl = w * h;

            valR = new int[h][w];
            valB = new int[h][w];
            valG = new int[h][w];
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    pixel = image.getRGB(j, i);
                    int red = (pixel >> 16) & 0xff;
                    int green = (pixel >> 8) & 0xff;
                    int blue = (pixel) & 0xff;

                    valR[i][j] = red;
                    somR += red;

                    square_Red = red * red;
                    sum_Square_Red += square_Red;

                    valG[i][j] = green;
                    somG += green;

                    square_Green = green * green;
                    sum_Square_Green += square_Green;

                    valB[i][j] = blue;
                    somB += blue;

                    square_Blue = blue * blue;
                    sum_Square_Blue += square_Blue;

                }

            }

            //la moyenne de pixels dechaque couleurs
            meanR = somR / nbpixl;
            meanG = somG / nbpixl;
            meanB = somB / nbpixl;
            //meanB = somB / nbpixl * 255.0f;

            //1  method of calculating the variance
            System.out.println("1 method of calculating the variance\n");
            float var[] = new float[3];
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {

                    varR += (meanR - valR[i][j]) * (meanR - valR[i][j]);
                    varG += (meanG - valG[i][j]) * (meanG - valG[i][j]);
                    varB += (meanR - valB[i][j]) * (meanB - valB[i][j]);

                }

            }

            var[0] = (varR / nbpixl);

            var[1] = (varG / nbpixl);

            var[2] = (varB / nbpixl);

            float var_img = ((var[0] + var[1] + var[2]) / nbpixl);

             System.out.println("var=\t"+var[0] + "\t"+ var[1] + "\t"+                                                                  var[2]);
            System.out.println("var of image=\t" + var_img);

            //2 method of calculating the variance
            System.out.println("2 method of calculating the variance\n");

            float varR1 = sum_Square_Red / nbpixl - (meanR * meanR);
            float varG1 = sum_Square_Green / nbpixl - (meanG * meanG);
            float varB1 = sum_Square_Blue / nbpixl - (meanB * meanB);
            float mean_img1 = ((varR1 + varG1 + varB1) / nbpixl);

       System.out.println("var=\t" + varR1 + "\t" + varG1 + "\t" + varB1);
            System.out.println("var of image=\t" + mean_img1);

        }

    } catch (IOException ex) {
        Logger.getLogger(var_img1.class.getName()).log(Level.SEVERE, null, ex);
    }
}

And for the result of image 1

Width, height: 259, 194

1 method of calculating the variance

varR= 7598.217 varG= 5033.452 varB= 4910.8223

Var of picture = 0.34913212

2 method of calculating the variance

2 method of calculating the variance

varR= 7597.9395 varG= 5183.0 varB= 4994.0

Var of image = 0.3537583



And for image 2

width, height: 620, 349 1 method of calculating the variance

varR= 991.2626 varG= 1525.1627 varB= 1280.9857

var of image= 0.017549733

2 method of calculating the variance

varR= -52462.875 varG= -50101.0 varB= -50592.0

var of image= -0.70780975


And here are the 2 images I used image1 image