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
                        
You have to understand the formula:
A correct way to use this formula is the following - d and d2 should come out the same: