简体   繁体   中英

compare one pixel with all pixels

i want to compare pixel of an image with all pixel of second image and then next pixel with all pixel of second image; i am using this code in which i am comparing pixel (converted in byte) one pixel of one image with second pixel of second but i do not want this approach. Please reply fast. Thanks in advance.

 public static double GetDifferentPercentageSneller(ref Bitmap bmp1, ref Bitmap bmp2)
    {

        //if (bmp1 == null || bmp2 == null)

        //    return 100.0;



        //if (bmp1.Size != bmp2.Size)

        //    return 100.0;



        //if (bmp1.PixelFormat != bmp2.PixelFormat)

        //    return 100.0;



        int iMismatch = 0;

        int iMatch = 0;



        unsafe
        {

            BitmapData data1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), ImageLockMode.ReadOnly, bmp1.PixelFormat);

            BitmapData data2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadOnly, bmp2.PixelFormat);



            int pixelBytes = 0;



            switch (data1.PixelFormat)
            {

                case PixelFormat.Format32bppArgb:

                    pixelBytes = 4;

                    break;

                case PixelFormat.Format24bppRgb:

                    pixelBytes = 3;

                    break;

                default:

                    throw new Exception("Bitmap format not supported");

            }



            int paddingBytes = data1.Stride % pixelBytes;

            byte* location1 = (byte*)data1.Scan0;

            byte* location2 = (byte*)data2.Scan0;



            for (int y = 0; y < data1.Height; ++y)
            {

                for (int x = 0; x < data1.Width; ++x)
                {


                            if (*location1 == *location2)
                            {

                                iMatch++;


                            }
                            else
                            {

                                iMismatch++;

                            }
                    location1 += pixelBytes;

                    location2 += pixelBytes;

                }



                location1 += paddingBytes;

                location2 += paddingBytes;

            }



            bmp1.UnlockBits(data1);

            bmp2.UnlockBits(data2);

        }



        double percent = (double)iMatch/ (double)(iMismatch + iMatch);



        return percent * 100.0;

    }

You have to always compare the LARGER image (both x, y) with the SMALLER. Although I don't know what your are exactly after, you can do it simply like this.

 BitmapImage Image1 = new BitmapImage(ImageStream);
 BitmapImage Image2 = new BitmapImage(ImageStream);
 int X = Image1.Width > Image2.Width ? Image2.Width : Image1.Width;
 int Y = Image1.Hieght > Image2.Height ? Image2.Heigth : Image1.Height;
 for(int x = 0; x < X; x++){
    for(int y = 0; y < Y; y++){
       Color color1 = Image1.GetPixel(x, y);
       Color color2 = Image2.GetPixel(x, y);
       // Do comparison here
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM