1

I want to read an RGB image and want to extract the image pixels. Then I want to compare each and every pixels to check any matching pixels in other part of image. If that pixel matches the original pixel and matched pixel should be replace with red and yellow in java.

I searched a lot from javaforums and image processing websites. Still no perfect solution I got.

Give some pixel extractor and pixel matcher examples to proceed further.

Ameer
  • 600
  • 1
  • 12
  • 27
  • 1
    this might be helpful http://stackoverflow.com/questions/7292208/image-comparison-in-java – AJM Mar 25 '13 at 10:38

2 Answers2

2

the following getRGBA method would extract the RGBA array at position (x, y) of image img:

private final int ALPHA = 24;
private final int RED = 16;
private final int GREEN = 8;
private final int BLUE = 0;

public int[] getRGBA(BufferedImage img, int x, int y)
{   
    int[] color = new int[4];
    color[0]=getColor(img, x,y,RED);
    color[1]=getColor(img, x,y,GREEN);
    color[2]=getColor(img, x,y,BLUE);
    color[3]=getColor(img, x,y,ALPHA);
    return color;
}

public int getColor(int x, int y, int color)
{       
    int value=img.getRGBA(x, y) >> color & 0xff;
    return value;
}

Pixel matcher? maybe you simply want to run a loop.. considering you are taking the (0,0) pixel as the original pixel, you could do the following:

    int[] originalPixel =  getRGBA(img,0,0);
    for (int i=0;i<img.getWidth();i++)
    {
        for (int j=0;j<img.getHeight();j++)
        {
           int[] color1 =  getRGBA(img,i,j);
           if (originalPixel[0] == color1[0] && originalPixel[1] == color1[1] && originalPixel[2] == color1[2] && originalPixel[3] == color1[3]) {
               img.setRGB(i, j,Color.red.getRGB());
           } 
           else {
               img.setRGB(i, j,Color.yellow.getRGB());
           }
        }
    }
Alexi Akl
  • 1,934
  • 1
  • 21
  • 20
0

This Marvin algorithm does exactly what you want.

Gabriel Archanjo
  • 4,547
  • 2
  • 34
  • 41