0

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());
     }

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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() );
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.