0

I want to compare the differences by pixels maybe to convert the images first to black and white and then to compare the pixels to see if they are not the same.

I created this class. I'm not sure if I need to change the project settings to use unsafe ?

using System;
 using System.Collections.Generic;
 using System.Drawing;
 using System.Drawing.Imaging;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
    
 namespace Test
 {
     class ImagesComparison
     {
         public static void ProcessUsingLockbitsAndUnsafe(Bitmap processedBitmap)
         {
             unsafe
             {
                 BitmapData bitmapData = processedBitmap.LockBits(new Rectangle(0, 0, processedBitmap.Width, processedBitmap.Height), ImageLockMode.ReadWrite, processedBitmap.PixelFormat);
                 int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(processedBitmap.PixelFormat) / 8;
                 int heightInPixels = bitmapData.Height;
                 int widthInBytes = bitmapData.Width * bytesPerPixel;
                 byte* ptrFirstPixel = (byte*)bitmapData.Scan0;
    
                 for (int y = 0; y < heightInPixels; y++)
                 {
                     byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
                     for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
                     {
                         int oldBlue = currentLine[x];
                         int oldGreen = currentLine[x + 1];
                         int oldRed = currentLine[x + 2];
    
                         // calculate new pixel value
                         currentLine[x] = (byte)oldBlue;
                         currentLine[x + 1] = (byte)oldGreen;
                         currentLine[x + 2] = (byte)oldRed;
                     }
                 }
                 processedBitmap.UnlockBits(bitmapData);
             }
         }
     }
 }

Then in form1

private void ComparingImages()
        {
            Bitmap bmp1 = new Bitmap(@"d:\comparison1.gif");
            Bitmap bmp2 = new Bitmap(@"d:\comparison2.gif");
            ImagesComparison.ProcessUsingLockbitsAndUnsafe(bmp1);
            ImagesComparison.ProcessUsingLockbitsAndUnsafe(bmp2);
        }

And using it with a button click event

private void button1_Click(object sender, EventArgs e)
        {
            ComparingImages();
        }

but how do I make the comparison between the two gif's ? and how can I create a new image showing the differences, for example the new image will show only the differences pixels in green and red or something that will show the differences ?

For example this two images to compare between them and find if they are not the same not by size but by the content of pixels.

image 1

and

image 2

  • 1
    What do you want to compare? If it is just the files are equal than do not convert to a bitmap. Comparing the contents of images is not simple. – jdweng Jan 20 '22 at 14:15
  • Doing an exact comparison is fairly trivial, just loop thru all the bytes in the image buffer and compare them. But this is often not very useful since any difference in processing or compression will make the images not equal. Doing an [Image similarity](https://peltarion.com/blog/data-science/image-similarity-explained) might be more useful, but is far from trivial. – JonasH Jan 20 '22 at 14:51
  • @jdweng my logic thought was to convert may be the two images to black and white then to compare by pixels. – Daniel Hershkovitz Jan 20 '22 at 17:10
  • Pixel comparison never really make a lot of sense. It just ells you there are differences but not what is different. For what you are doing you need to get the objects in the pictures. You are really looks to analyze the clouds. – jdweng Jan 20 '22 at 17:52
  • Maybe to check out this question's answers: https://stackoverflow.com/a/35153895/12003724 – TheProgrammer Jan 24 '22 at 02:11

0 Answers0