简体   繁体   中英

Calculating the mean pixel value of an Image: Java

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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