1

我正在尝试计算 java 中图像的标准偏差。我正在使用 rast.Sample 来获取每个灰度级,然后将其从均值中取出并对其进行平方。

我的代码是:

public int standardDeviation(BufferedImage source)
    {
    int width = source.getWidth();
    int height = source.getHeight();
    int totalPixels = width * height;
    int temp = 0;
    int VarianceSum = 0;
    double SumSquared = 0;
    int mean = mean(source);
    double variance = 0;
    int StandardDeviation = 0;
    Raster rast = source.getRaster();

    //Loop through rast getting each grey level.
    for(int i =0; i<width; i++)
        {
            for(int j=0; j<width; j++)
            {
                temp = rast.getSample(i,j,0);
                VarianceSum += (temp - mean);
                SumSquared = Math.pow(VarianceSum,2);
            }
        }

    variance = SumSquared/totalPixels;


    System.out.println(temp);
    System.out.println(VarianceSum);
    //System.out.println(SumSquared);
    System.out.println(totalPixels);



    return StandardDeviation;

    }

我在做傻事!请帮忙

4

1 回答 1

1

您在宽度上迭代两次。如果 width != height 会崩溃。另见我的评论

于 2012-04-19T11:42:59.460 回答