Comparing Images with variation in transparency level and comparing texts in it

883 views Asked by At

I have multiple labels produced at multiple folders by diffrent softwares. the sizes of labels are same but their transparency labels varies.Ideally they are same, but when i compare using my internal application, it throws me error saying they mismatch.

I know similar questions asked, but they are not exactly matching the questions i have. I need to implement related algorithm using c#.

I am exploring few API AForge.Net, ImageMagick.

Till now i have compared bytes

filename1 = Path.Combine(directory.ToString(), new1[i].ToString());
filename2 = Path.Combine(directory1.ToString(), new2[i].ToString());

using (Bitmap bm1 = new Bitmap(filename1))
{
    using (Bitmap bm2 = new Bitmap(filename2))
    {
        // Make a difference image.
        int wid = Math.Min(bm1.Width, bm2.Width);
        int hgt = Math.Min(bm1.Height, bm2.Height);
        Bitmap bm3 = new Bitmap(wid, hgt);

        // Create the difference image.
        bool are_identical = true;
        Color eq_color = Color.White;
        Color ne_color = Color.Red;
        for (int x = 0; x < wid; x++)
        {
            for (int y = 0; y < hgt; y++)
            {
                //if (bm1.GetPixel(x, y).Equals(bm2.GetPixel(x, y)))
                if (bm1.GetPixel(x, y) != (bm2.GetPixel(x, y)))
                {
                    //bm3.SetPixel(x, y, eq_color);
                    bm3.SetPixel(x, y, ne_color);
                    are_identical = false;

                }
                else
                {
                    //bm3.SetPixel(x, y, ne_color);
                    //are_identical = false;

                }
            }
            //I kept the code here before
        }
        if (!are_identical)
        {
            bm3.Save(@"C:\Users\XPS Files\DiffrenceofImages" + new1[i]);
        }
        // Display the result.
        //picResult.Image = bm3;
        //bm3.Save(@"C:\Users\XPS Files\DiffrenceofImages\" + new1[i]);
        this.Cursor = Cursors.Default;
        if ((bm1.Width != bm2.Width) || (bm1.Height != bm2.Height)) are_identical = false;
        if (are_identical)
        {
            //richTextBox1.Text = string.Format("Images are identical", "\r\n");
            sb.AppendLine("Image Name=" + new1[i] + " are identical at both folder");
        }
        else
        {
            //richTextBox1.Text = string.Format("Images are NOT MATCHING", "\r\n");
            sb.AppendLine("Image Name=" + new1[i] + " are Not Matching at both folder");
        }

        //sb.AppendLine(f1.Name + ": " + (equal ? "Images are equal" : "Images are NOT equal"));

    }
}
result.Add(sb.ToString());
1

There are 1 answers

1
Mark Setchell On

I am not sure from your question if you do, or do not, want to use ImageMagick. If you do, you can disable the alpha/transparency channel with +matte or -alpha off.

So, if I create two red images, one with a radial gradient for transparency like this:

convert -size 500x500 xc:red \( radial-gradient:black-gray90 -sigmoidal-contrast 10,50% \) -compose copy-opacity -composite radial.png

enter image description here

and one with a straight gradient for transparency like this:

convert -size 500x500 xc:blue \( gradient:white-gray40 \) -compose copy-opacity -composite straight.png

enter image description here

I can then compare them using ImageMagick but ignoring the difference in transparency like this:

convert radial.png straight.png +matte -metric AE -compare format "Count of differing pixels:%[distortion]" info:
Count of differing pixels:0

That command says... load radial.png and straight.png into the image stack and remove the transparency from both, then count and report the total number of differing pixels - which is zero as the transparency has been removed.