user1167596
user1167596

Reputation: 107

Finding the Mean of an Image Java

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.

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.

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.

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.

Here is my 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<height; i++){

    for(int j=0; j<width; j++){
        count++;
        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());

        }
}
    System.out.println("Mean Value of Image is " + sum);
    return sum;

}

}

Upvotes: 1

Views: 1345

Answers (1)

FiReTiTi
FiReTiTi

Reputation: 5898

You go through all the pixels twice in your method imageToCalculate. This simple code is enough to compute the average of an image:

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

But usually, it's better to give the image as a parameter of the method:

public double Average(BufferedImage image)

And for the final purpose of your project, there is no way that the average can give you a good result. Imagine that you have two images: the first with all the pixels at 127, and the second with half of the pixels at 0 and the other at 255.

Upvotes: 1

Related Questions