简体   繁体   中英

Calculate chi-squared distance between two image histograms in Java

I have calculated the histograms of two images in Java (Code modified and shortened):

for (int posX = 0; posX < image.getWidth(); posX++)
{
    for (int posY = 0; posY < image.getHeight(); posY++)
    {
        Color c1 = new Color(image.getRGB(posX, posY));
        cummulative[0] = cummulative[0] + c1.getRed();
        cummulative[1] = cummulative[1] + c1.getGreen();
        cummulative[2] = cummulative[2] + c1.getBlue();
        numPixels++;
    }
}        
    r1 = cummulative[0] / numPixels;
    g1 = cummulative[1] / numPixels;
    b1 = cummulative[2] / numPixels;

and then calculated the Euclidean Distance of the histograms:

tempDist = Math.sqrt((r1 - r2) * (r1 - r2) + (g1 - g2) * (g1 - g2) + (b1 - b2) * (b1 - b2));

Now I want to calculate the Chi-Squared Distance distance instead the Euclidean Distance. But I have no idea how to implement that. Please can someone give an introduction for that?

Edit:

I have now the following code to generate the histogram:

float[] histogram = new float[256];

for (int i = 0; i < input.getWidth(); i++) {
for (int j = 0; j < input.getHeight(); j++) {
    int color = 0;
    switch (colorVal) {
        case 1:
            color = new Color(input.getRGB(i, j)).getRed();
            break;
        case 2:
            color = new Color(input.getRGB(i, j)).getGreen();
            break;
        case 3:
            color = new Color(input.getRGB(i, j)).getBlue();
            break;
    }
    histogram[color]++;
}

}

How can I continue supposed I have the following Data:

Image 1:

R 10 count 1000

R 20 count 100

R 30 count 100

G 20 count 600

G 255 count 600

B 0 count 800

B 200 count 400

Image 2:

R 10 count 1000

R 20 count 200

G 20 count 600

G 255 count 600

B 0 count 800

B 100 count 200

B 200 count 200

you have just summed up the r,g,b values, and not computed a histogram. First, compute the histogram correctly, then Chi squared distance can be computed as d(x,y) = sum( (xi-yi)^2 / (xi+yi) ) / 2, where x and y are your histograms, and i is the bin index of the histogram

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM