title: Finding the Mean of an Image Java
body: <p>I am very much a beginner in digital image processing and programming in general. I am trying to find the Mean of an image using a raster. To be completely honest, this is a real stab in the dark but I am really lost at what to do here.</p>
  
  <p>My code currently returns nothing and I am not sure if it is even doing anything. My interpretation of what it is doing is, read the image file and then using a raster to extract the details of that image based on height and width cooridinates. I want it basically to output the mean on the console.</p>
  
  <p>So could anybody tell me what I am doing wrong and why my code is not returning the mean of the image? I have been digging up resources to try and learn but anything linked with image processing doesn't really seem to be for a newbie and I am finding it tough. So if anyone has anywhere that would be good to start it would be appreciated.</p>
  
  <p>Eventually, I want to calculate the mean on one image and I then want to run that image against a file directory of other images. The point of this is to see which images are most similar based on the mean. But I feel like I am some way off where I want to be.</p>
  
  <p>Here is my code</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
   import java.awt.image.BufferedImage;
  import java.awt.image.Raster;
  import java.io.File;
  import java.io.IOException;
  
   import javax.imageio.ImageIO;
  
   public class calculateMean {
  
  static BufferedImage image;
  static int width;
  static int height;
  public static void main(String[] args) throws Exception{
      try{
          File input = new File("C:\\Users\\cbirImages\\jonBon2.jpg");
          image = ImageIO.read(input);
          width = image.getWidth();
          height = image.getHeight();
  }catch (Exception c){}
  }
  
  private double sum;
  
  public double imageToCalculate(){
      int count = 0;
      for(int i=0; i&lt;height; i++){
  
      for(int j=0; j&lt;width; j++){
          count++;
          Raster raster = image.getRaster();
          double sum = 0.0;
  
          for (int y = 0; y &lt; image.getHeight(); ++y){
            for (int x = 0; x &lt; image.getWidth(); ++x){
              sum += raster.getSample(x, y, 0);
            }
          }
          return sum / (image.getWidth() * image.getHeight());
  
          }
  }
      System.out.println("Mean Value of Image is " + sum);
      return sum;
  
  }
  </code></pre>
  
  <p>}</p>