3

I am newbie to Image processing. So I might be asking a noob question here.

I am on VS2010, .Net 4, Windows application.

I want to show the differences between two images by rectangles (or any other shape). I tried to refer the class in the link below.

http://www.aforgenet.com/framework/features/template_matching.html

However, the first photo (of Rose with two yello rectangles) is what I really want, the code in the above link tells you what all things match and not the differences. I actually want to find the differences and highlight/draw rectangles around the differences.

I tried the Difference and ThresholdedDifference classes as well in AForge library, but in that I can create a new image which just shows the differences, however, I want to draw rectangles around those differences. I am not sure how to get the coordinates for the differneces to draw rectangles?

Any input on what classes/functions I can use here?

I've tried EyeOpen library but it does not have many options compared to Aforge unless I am missing anything here. I am okay to try any other library other than Aforge.Net in C#.

Regards, Rumit

leppie
  • 115,091
  • 17
  • 196
  • 297
rumit patel
  • 1,891
  • 5
  • 23
  • 28
  • How exactly are you proposing to draw rectangles around the differences? Consider the nearly trivial case of the differences making up a line drawn at 45 degrees. Do you want one rectangle that encompasses all that change? Or, tiny little rectangles for each little pixel change? – John May 13 '13 at 21:20

2 Answers2

4

Let me know if I understand your question correctly: You would like to compare two images and if there is a difference between them then show this difference by drawing rectangle around it.

If this is the only thing you are trying to achieve then I would not use any external libraries and instead I would just compare the images pixel by pixel and then draw a rectangle around the area that doesn't match.

Assuming the two images are the same size, you can use simple loops to get the coordinates of the pixel that is different:

//the images are loaded in Bitmap image1, image2;
for (int x = 0; x < image1.Width; x++)
{
    for (int y = 0; y < image1.Height; y++)
    {
        if (image1.GetPixel(x, y) != image2.GetPixel(x, y))
        {
            posX = x; posY = y; //position of the pixel that is different
        }
    }
}

Then you just draw a rectangle around that pixel specified by posX and posY using DrawRectangle(). If you want to detect all differences you can create an array of pixel coordinates, add item to it every time you discover a difference and afterwards draw rectangles around all of them.

I am afraid using GetPixel() is pretty slow. If you are aiming for speed then I recommend using LockBits() and UnlockBits() for pixel manipulation (see MSDN Library).

SysGen
  • 604
  • 7
  • 29
  • I tried this method before moving to the external libraries, but in the method above, it is very difficult to identify the areas where to draw rectangles. For example, in the link in my original post has a rose picture with two rectangales. If I go pixel by pixel, it is very difficult to decide where to draw rectangles. Let me know if I am missing anything here? I am looking at an application which would find multiple differences and highlight the same! – rumit patel May 12 '13 at 05:47
  • @rumitpatel I'm sorry but I still don't understand what you want to achieve. If it's [template matching](http://en.wikipedia.org/wiki/Template_matching), that is comparing an image to some template and highlighting the matching areas, then you utilize the exact code written in the link posted in your question. If it's finding [differences in two images](http://en.wikipedia.org/wiki/Image_differencing) then you use the code in my answer or some other differencing technique. You might also want to find some area differences in a single image and compare them but I don't see how that is useful. – SysGen May 12 '13 at 12:06
2

I posted an answer here, which highlights the delta between two images.

enter image description here

Fidel
  • 7,027
  • 11
  • 57
  • 81