C# – Compare 2 16×16 pixel images similarity

ccomparisonimage processingpixelsimilarity

I would like to compare 2 images similarity with percentage. I want to detect 90% same images. Each image size is 16×16 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

Best Answer

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