1

For read 2 images and get differences between them I use this code in C# :

Bitmap Img1 = new Bitmap("D:\\Imgs\\Img1.png");
    Bitmap Img2 = new Bitmap("D:\\Imgs\\Img2.png");
    public static int[,] MatrisFromBMP(Bitmap bmp)
    {
        int width = bmp.Width, height = bmp.Height;
        int[,] matris = new int[width, height];

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                Color c = bmp.GetPixel(i, j);
                matris[i, j] = (c.R == 0 && c.G == 0 && c.B == 0) ? 0 : 1;
            }
        }

        return matris;
    }
    public static Bitmap BMPFromMatris(int[,] matris, int width, int height)
    {
        Bitmap bmp = new Bitmap(width, height);
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
                bmp.SetPixel(i, j, matris[i, j] == 1 ? Color.White : Color.Black);
        return bmp;
    }

these are my functions and this is my process :

int[,] Matris1;
        int[,] Matris2;
        int[,] Matris3;

        Matris1 = MatrisFromBMP(Img1);
        Matris2 = MatrisFromBMP(Img2);
        Matris3 = MatrisFromBMP(Img2);

        int dist = 0;
        for (int i = 0; i < Img1.Width; i++)
        {
            for (int j = 0; j < Img1.Height; j++)
            {                    
                dist = Math.Abs(Matris1[i, j] - Matris2[i, j]);
                Matris3[i, j] = dist;
            }
        }
        Img3_p.Image = BMPFromMatris(Matris3, Img1.Width, Img2.Height);

This works fine but for each picture with Size(1360*768) take 5s !
This is a very long time for me and i just have less than 0.5 s !
What are the better ways to do this ?
thanks , Arman

Atrak
  • 11
  • 3
  • This is far too broad and subjective to be answered well. Do a bit more research and come back with a focused question is my advice. If you take the time to read the MSDN page for SetPixel for instance you'd have found: `Use SetPixel method to set the color of an individual pixel in an image programmatically. You can also change an image programmatically by using the LockBits method. Typically for large-scale changes, the LockBits method offers better performance.` – asawyer Oct 17 '17 at 18:24
  • Tnkas for your comments . Yes of course I researched ! for 2 weeks im looking forward lockbits but no answer :( then i just Write a prototype to complete that with better ways ! – Atrak Oct 17 '17 at 18:33
  • As usual, an imaging question was closed based on a question that is nowhere near a duplicate to this question. This question asks about extracting diffs between two bitmaps, which is in no way the same as the linked questions. – Craig.Feied Dec 09 '18 at 07:01

0 Answers0