0

I am comparing 2 similar images and would like to see if both are similar .Currently I used the code:

public void foo(Bitmap bitmapFoo) {
    int[] pixels;

    int height = bitmapFoo.getHeight();
    int width = bitmapFoo.getWidth();

    pixels = new int[height * width];

    bitmapFoo.getPixels(pixels, 0, width, 1, 1, width - 1, height - 1); 


}

and I call the function : foo(img1) where :

img1=(Bitmap)data.getExtras().get("data");

I would like to know how to get the above getpixel,I tried assigning it to variable but did not work .Should it have a return type ?? and in format it is ?

And also how do I compare 2 images??

Also both the images may be of different dimensions based on the mobile camera the snapshot is taken from .

Also can it recognize if the same image is shot in the morning and night ???

Thanks in Advance.

Jason Wood
  • 89
  • 3
  • 14
  • Check this post, where they suggested some options: http://stackoverflow.com/questions/6120439/comparing-bitmap-images-in-android – Juangcg Aug 21 '13 at 21:46
  • how do I retrieve this : bitmapFoo.getPixels(pixels, 0, width, 1, 1, width - 1, height - 1); – Jason Wood Aug 21 '13 at 21:58

2 Answers2

1

This code will compare pixel from base image with other image.

  1. If both pixel at location (x,y) are same then add same pixel in result image without change. Otherwise it modifies that pixel and add to our result image.

  2. In case of baseline image height or width is larger than other image then it will add red color for extra pixel which is not available in other images.

  3. Both image file format should be same for comparison.

  4. Code will use base image file format to create resultant image file and resultant image contains highlighted portion where difference observed.

Here is a Link To Code with sample example attached.

Neeraj V
  • 41
  • 3
0

If you want to copy the pixels of the bitmap to a byte array, the easiest way is:

int height = bitmapFoo.getHeight();
int width = bitmapFoo.getWidth();

pixels = new int[height * width];

bitmapFoo.copyPixelsToBuffer(pixels);

See the documentation

I should warn you that you will have to handle this with care, any other way you will get OutOfMemoryError.

To get All Pixels

bitmapFoo.copyPixelsToBuffer(pixels);

or

bitmapFoo.getPixels(pixels, 0, width, 0, 0, width, height);

To get One Pixel

The two arguments must be two integers within the range [0, getWidth()-1] and [0, getHeight()-1]

int pix = bitmapFoo.getPixel(x, y);
Juangcg
  • 1,038
  • 9
  • 14
  • but there already getpixel function : bitmapFoo.getPixels(pixels, 0, width, 1, 1, width - 1, height - 1); how to get this ?? – Jason Wood Aug 21 '13 at 22:16
  • bitmapFoo.getPixels(pixels, 0, width, 0, 0, width, height); – Juangcg Aug 21 '13 at 22:23
  • int pxl = bitmapFoo.getPixel(bitmapFoo.getHeight(), bitmapFoo.getWidth()); is givig error .Why? Thank you for your time. – Jason Wood Aug 21 '13 at 22:27
  • Check the answer again, but you are getting an error because you are using index out of the bitmap. – Juangcg Aug 21 '13 at 22:32
  • Sorry again .I have 3 q's w.r.t to the above topic. 1. bitmapFoo.getPixels(pixels, 0, width, 0, 0, width, height); how to get the value for the above statement into variable? or array? or return type or just compare 2 bitmaps .basically I am looking for sample code or project which is working > i am on it for hours now.Unable to solve – Jason Wood Aug 21 '13 at 22:38
  • what should the value for height ,width in eq:bitmapFoo.getHeight(), bitmapFoo.getWidth() be ?? the h=800 w=480 – Jason Wood Aug 21 '13 at 22:42
  • when you call this method bitmapFoo.getPixels(pixels, 0, width, 0, 0, width, height);, the pixels of bitmapFoo are copied into the array pixels. – Juangcg Aug 21 '13 at 22:43
  • 3. I used equals as suggested in the link but how do I call it ??I tried Arrays ar=equals(img1, img2); but I get an error.Thanks again for looking into it. – Jason Wood Aug 21 '13 at 22:43
  • Oh into int[] pixels ?? coz i thought it was h*w ?? before right ? – Jason Wood Aug 21 '13 at 22:48
  • I saw it but how do I use it in IF Condition.Thanks again for your support – Jason Wood Aug 21 '13 at 22:54
  • You must know how to use it in IF Condition. Hope I have helped you. Bye. – Juangcg Aug 21 '13 at 23:00
  • Can you please tell me how ?? – Jason Wood Aug 21 '13 at 23:01