-3

I have an image located in

C:\ImageOne.png

And I have lets say 20 images in the directory

C:\Images

How can I compare ImageOne.png against all those images?

Example:

Imagine i got one reCaptcha image saved as C:\ImageOne.png

And in a folder located at C:\Images i have other reCaptcha images.

I then need a code that can find a identical image inside C:\Images

Current Code:

public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage)
{
    MemoryStream ms = new MemoryStream();
    firstImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    String firstBitmap = Convert.ToBase64String(ms.ToArray());
    ms.Position = 0;

    secondImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    String secondBitmap = Convert.ToBase64String(ms.ToArray());

    if (firstBitmap.Equals(secondBitmap))
    {
        return true;
    }
    else
    {
        return false;
    }
}

Bitmap img2 = new Bitmap(@"C:\ImageOne");

private void CheckCaptcha()
{
    foreach (string s in Directory.GetFiles(@"C:\Images"))
    {
        Bitmap img1 = new Bitmap(s);

        if (ImageCompareString(img1, img2) == true)
        {
            Logging("Identical");
        }
        else
        {
            Logging("Not Identical");
        }
        img1.Dispose();
    }
}
Merceyz
  • 25
  • 1
  • 3
  • 12
  • 1
    How precise do you want this image comparison to be? Up to every single bit? Or you need some more intelligent algorithm? – Darin Dimitrov Dec 28 '13 at 21:54
  • Can you be a bit more concrete on what help you need - finding/iterating over files in a directory, reading files, comparing files as byte sequences/fuzzy image comparison or maybe even OCR and comparing resulting text? – Alexei Levenkov Dec 28 '13 at 22:03
  • As good as possible, images isn't over 300x300 – Merceyz Dec 28 '13 at 22:04
  • Imagine i have one reCaptcha image and then a folder of other reCaptcha images, i need to find a image from that folder that is identical to the first one – Merceyz Dec 28 '13 at 22:05
  • `As good as possible` doesn't answer my question. You should realize that without specifying your problem precisely it will be hard to resolve it. Because you don't even know what you need to resolve. So I repeat my question once again: do you need an exact match (bit by bit comparison) or do you need some other algorithm to compare them? Also don't forget to include in your question the progress you have made so far and show the code you attempted. – Darin Dimitrov Dec 28 '13 at 22:05
  • @DarinDimitrov - now with last comment the goal and necessary precision is somewhat clear - "OCR captcha" via [mechanical turk](http://en.wikipedia.org/wiki/The_Turk) :) If it is the question and approach it is to broad for SO in my opinion. – Alexei Levenkov Dec 28 '13 at 22:07
  • Added my current code to OP – Merceyz Dec 28 '13 at 22:12
  • 1
    Why do you need to find recaptcha images? This seems bogus to say the least. – Lasse V. Karlsen Dec 28 '13 at 22:49

1 Answers1

0

Maybe this will help:

Bitmap img2 = new Bitmap(@"C:\ImageOne.png");
ImageConverter converter = new ImageConverter();
byte[] img2Bytes = (byte[])converter.ConvertTo(img2, typeof(byte[]));

foreach (string s in Directory.GetFiles(@"C:\Images"))
{
    Bitmap img1 = new Bitmap(s);
    byte[] img1Bytes = (byte[])converter.ConvertTo(img1, typeof(byte[]));
    if (CompareImages(img1Bytes,img2Bytes))
    {
        Logging("Identical");
    }
    else
    {
        Logging("Not Identical");
    }
    img1.Dispose();
}

public bool CompareImages(byte[] b1, byte[] b2)
{
   EqualityComparer<byte> comparer = EqualityComparer<byte>.Default;
   if (b1.Length == b2.Length)
   {
       for (int i = 0; i < b1.Length; i++)
       {
           if (!comparer.Equals(b1[i], b2[i])) return false;
       }
    } else { return false; }

     return true;
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184