我正在尝试计算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;
}
我正在做些傻事!请帮忙
答案 0 :(得分:1)
您在宽度上迭代两次。如果宽度!=高度会崩溃。另见我的评论