2

I would like to compare 2 images similarity with percentage. I want to detect 90% same images. Each image size is 16x16 pixel. I need some clue, help about it. Right now i am able to detect 100% same images when comparing with the code below

for (; x < irMainX; x++)
{

    for (; y < irMainY; y++)
    {
        Color pixelColor = image.GetPixel(x, y);
        if (pixelColor.A.ToString() != srClickedArray[x % 16, y % 16, 0])
        {
            blSame = false;
            y = 16;
            break;
        }
        if (pixelColor.R.ToString() != srClickedArray[x % 16, y % 16, 1])
        {
            blSame = false;
            y = 16;
            break;
        }
        if (pixelColor.G.ToString() != srClickedArray[x % 16, y % 16, 2])
        {
            blSame = false;
            y = 16;
            break;
        }
        if (pixelColor.B.ToString() != srClickedArray[x % 16, y % 16, 3])
        {
            blSame = false;
            y = 16;
            break;
        }
    }

    y = y - 16;

    if (blSame == false)
        break;
}

For example i would like to recognize these 2 images as same. Currently the software recognizes them as different images since they are not exactly same

enter image description here enter image description here

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • you have point. i will update question one second – Furkan Gözükara Jun 23 '12 at 23:32
  • question updated with example 2 images – Furkan Gözükara Jun 23 '12 at 23:36
  • 2
    That's just one example. You need to define what counts as "90% similar". For instance, what about changes in colour? Changes in rotation? Changes in size? Changes in position? etc. etc. – Oliver Charlesworth Jun 23 '12 at 23:37
  • Please see "[Stack Overflow does not allow tags in titles](http://meta.stackexchange.com/a/130208)" – John Saunders Jun 24 '12 at 01:22
  • @OliverCharlesworth If the image changes in colour, changes in rotation changes in size & changes in position, how to identify those? I have asked question on this. [This is the link.](http://stackoverflow.com/questions/31261456/how-to-compare-image-similarity-using-php-regardless-of-scale-rotation/) Can you look at into that question and give me an answer? – Tharu Jul 08 '15 at 02:57

2 Answers2

2

Use a count for the number of pixels that don't match:

public const double PERCENT_MATCH = 0.9;

int noMatchCount = 0;
for (int x = 0; x < irMainX; x++)
{
    for (int y = 0; y < irMainY; y++)
    {
       if ( !pixelsMatch( image.GetPixel(x,y), srClickedArray[x%16, y%16] )
       {
           noMatchCount++;
           if ( noMatchCount > ( 16 * 16 * ( 1.0 - PERCENT_MATCH ))
              goto matchFailed;
       }
    }
}
Console.WriteLine("images are >=90% identical");
return;
matchFailed:
Console.WriteLine("image are <90% identical");

You could count matching pixels, but that will be slower. Consider measuring how much two pixels differ. For most purposes - you could have ALL the pixels not match exactly - yet have the images look visually identical

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Rafael Baptista
  • 11,181
  • 5
  • 39
  • 59
  • 2
    goto statements shouldn't be used here. – Cole Tobin Jun 23 '12 at 23:46
  • @Cole: pedantic. Its pseudo code meant to illustrate the principle. – Rafael Baptista Jun 23 '12 at 23:47
  • -1 Since OP's ask for a c# solution, another -1: assume you shifted the image 1 pixel right and 1 pixel down, Now run your algorithm to compare these two images :) 0% similar? – L.B Jun 23 '12 at 23:48
  • @ColeJohnson Of course, Just show me how you find it programmatically. – L.B Jun 23 '12 at 23:52
  • @L.B. I dont think the OP asked for that. He just asked to see if 2 16*16 images are >90% similar. – Cole Tobin Jun 23 '12 at 23:53
  • @ColeJohnson I exactly say that. This answer can't do that. Just post an answer you believe it does. – L.B Jun 23 '12 at 23:58
  • 1
    Actually there is a whole framework suggested here http://stackoverflow.com/questions/152028/are-there-any-ok-image-recognition-libraries-for-net but i don't know which dlls should be added as a reference – Furkan Gözükara Jun 24 '12 at 00:01
  • @MonsterMMORPG comparing two images for equality is simple as you already found out, but when you say `X%` then you have to find a very good library or read a lof of papers for its theory. It is more than writing two loops and executing a function – L.B Jun 24 '12 at 00:07
  • There is a whole world of image similarity algorithms. OP asked for a 90% pixel identical. If you want to get fancy about it define an error measurement for two pixels, then compute mean squared error of both images. Images shifted by one pixels could look radically different. I would skip trying to do that. Imagine a 16x16 patch that is all white with a 1 pix stipple black border that is shifted left by one one pixel. It would look VERY different, yet pixel count wise it would be more than 90% identical. – Rafael Baptista Jun 24 '12 at 00:08
  • Alright i tried the solution here http://stackoverflow.com/questions/152028/are-there-any-ok-image-recognition-libraries-for-net. I mean i implemented http://code.google.com/p/aforge/ and it is extremely slow. So i will stick with this suggestion. – Furkan Gözükara Jun 26 '12 at 13:25
2

I wouldn't use image.GetPixel(x,y), as it's a lot slower than utilizing unsafe code to check specific bytes associated with each image.

Check out Lockbits

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Jimmy Bong
  • 29
  • 1