user2916314
user2916314

Reputation: 53

How to scan an image using a fixed size block in Java?

I'm writing an image analysis program which allows the user to select two coordinates on an image. The program then crops the image to an rectangle based on those two points. The program will then find the histogram distribution of the image.

The issue I am having is that I want to compare the cropped image to another image (imagetwo) that contains that cropped image. My current plan is to scan imagetwo by creating a block the same size as the cropped image then moving across imagetwo, calculating the histogram distribution as it moves through. then the blocks are compared with the cropped image.

How can i achieve this? I know this requires a number of for loops but I struggling to work out the logic.

Code Logic so far:

//Size of cropped image
int width = croppedimage.getWidth();
int height = croppedimage.getHeight();

Image tempImage;
Image imageTwo;

//Match pattern
for(x=0; x<width; x++){
 for(y=0; y<height; y++){
  tempImage.addPixel(imageTwo.get(x,y);
 }
}

This retrieves the first block with the size of the cropped image in image two. However I would like to move the block along imageTwo at one pixel at a time.

Upvotes: 0

Views: 590

Answers (1)

Jason
Jason

Reputation: 13986

Sounds like you are going to need three functions: one to get the histogram of an image, another to compare two histogram, and a third to crop an image. Also, since a histogram only works for a grayscale image (or process color channel separately) I'm going to assume that's what we are working with--a greyscale image.

Let's assume that you have those three functions:

public int[] getHistogram(BufferedImage image){ ... }
public float getRelativeCorrelation(int[] hist1, int[] hist2){ ... }
public BufferedImage cropImage(BufferedImage image, int x, int y, int width, int height){ ... }

Then the loop to look at all comparisons is:

BufferedImage scene = ... ;// The image that you want to test
BufferedImage roi = ... ; //The cropped region of interest selected by your user


int sceneWidth  = scene.getWidth();
int sceneHeight = scene.getHeight();
int roiWidth    = roi.getWidth();
int roiHeight   = roi.getHeight();

int[] histROI = getHistogram(roi);

for (int y=0;y<sceneHeight-roiHeight+1;y++){
    for (int x=0;x<sceneWidth-roiWidth+1;x++){

          BufferedImage sceneROI = cropImage(scene, x,y, roiWidth, roiHeight);
          int[] histSceneROI = getHistogram(sceneROI);

          float comparison  = getRelativeCorrelation(histROI, histSceneROI);

          //todo: something with the comparison.

    }
}

Upvotes: 1

Related Questions