Quantcast
Channel: magick Discussions Rss Feed
Viewing all articles
Browse latest Browse all 3693

New Post: Image Compare in C# is returning erroneous results for matching .png images

$
0
0
Thank you for sending me those images. You are getting this odd value because you are comparing images that have different dimensions. This means that the smaller image will get so called 'virtual pixels' that will have the value of the background color. You will need to add an extra check to make sure that the images are equal and I would also advise you to use a different ErrorMetric. I use ErrorMetric.RootMeanSquared most of the time. The closer the value is to zero the better the match. Below is an example of what you could do instead:
void compareScreenshotImages(string file1, string file2, double accuracy)
{
  TakeScreenshot(driver, file2);

  using (MagickImage image1 = new MagickImage(file1))
  {
    using (MagickImage image2 = new MagickImage(file2))
    {
      if (image1->Width != image2->Width || image1->Height != image2->Height)
        WriteToFile("ERROR: Invalid dimensions");
      else
      {
        using (MagickImage compare= new MagickImage())
        {
          Double result = image1.Compare(image2, ErrorMetric.RootMeanSquared, compare);
          compare.Write(compareresult);

          if (result > accuracy)
            WriteToFile(String.Format("  PASS: Images are duplicate: {0:P2} - ({0})", result));
          else
            WriteToFile(String.Format("ERROR: Result similarity: {0:P2} - ({0})", result));
        }
      }
   }
}

Viewing all articles
Browse latest Browse all 3693

Trending Articles