简体   繁体   English

计算Java中两个图像直方图之间的卡方距离

[英]Calculate chi-squared distance between two image histograms in Java

I have calculated the histograms of two images in Java (Code modified and shortened): 我已经用Java计算了两个图像的直方图(修改并缩短了代码):

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: 图片1

R 10 count 1000 R 10数1000

R 20 count 100 R 20数100

R 30 count 100 R 30数100

G 20 count 600 G 20计数600

G 255 count 600 G 255计数600

B 0 count 800 B 0计数800

B 200 count 400 B 200数400

Image 2: 图片2:

R 10 count 1000 R 10数1000

R 20 count 200 R 20数200

G 20 count 600 G 20计数600

G 255 count 600 G 255计数600

B 0 count 800 B 0计数800

B 100 count 200 B 100数200

B 200 count 200 B 200数200

you have just summed up the r,g,b values, and not computed a histogram. 您刚刚对r,g,b值求和,但未计算直方图。 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 首先,正确计算直方图,然后可将卡方距离计算为d(x,y)= sum((xi-yi)^ 2 / /(xi + yi))/ 2,其中x和y是您的直方图,并且i是直方图的bin索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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