Reputation: 107
I am using this method to calculate the mean pixel value of an image. Is there a better or more complex way of doing this particular method?I am pretty new to this and I am happy that I have got this far but I have been tasked with finding a better, more mathematically complex way of doing this and I am stumped!
public static double meanValue(BufferedImage image) {
Raster raster = image.getRaster();
double sum = 0.0;
for (int y = 0; y < image.getHeight(); ++y){
for (int x = 0; x < image.getWidth(); ++x){
sum += raster.getSample(x, y, 0);
}
}
return sum / (image.getWidth() * image.getHeight());
}
Upvotes: 0
Views: 1467
Reputation: 554
Maybe something like this (if you can use Java 8)
public static double meanValue(BufferedImage image)
{
final Raster raster = image.getRaster();
return( IntStream.range(0, image.getHeight())
.parallel()
.mapToDouble(y -> IntStream.range(0, image.getWidth())
.parallel()
.map(x -> raster.getSample(x, y, 0))
.average().getAsDouble())
.average()
.getAsDouble() );
}
Upvotes: 1
Reputation: 1738
You could divide image into chunks and make parallel computing.
Here is example of how you could do this: https://stackoverflow.com/a/5686581/1814524
Upvotes: 1