C# 将一个像素与所有像素进行比较

C# 将一个像素与所有像素进行比较,c#,.net,image,image-processing,C#,.net,Image,Image Processing,我想比较一个图像的像素和第二个图像的所有像素,然后比较下一个像素和第二个图像的所有像素; 我正在使用这段代码,其中我比较了一幅图像的一个像素(转换为字节)和一秒钟的第二个像素,但我不想要这种方法。请尽快回复。 提前谢谢 public static double GetDifferentPercentageSneller(ref Bitmap bmp1, ref Bitmap bmp2) { //if (bmp1 == null || bmp2 == null)

我想比较一个图像的像素和第二个图像的所有像素,然后比较下一个像素和第二个图像的所有像素; 我正在使用这段代码,其中我比较了一幅图像的一个像素(转换为字节)和一秒钟的第二个像素,但我不想要这种方法。请尽快回复。 提前谢谢

 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;

    }
公共静态双GetDifferentipPercentageNeller(参考位图bmp1,参考位图bmp2)
{
//如果(bmp1==null | | bmp2==null)
//返回100.0;
//如果(bmp1.Size!=bmp2.Size)
//返回100.0;
//if(bmp1.PixelFormat!=bmp2.PixelFormat)
//返回100.0;
int iMismatch=0;
int-iMatch=0;
不安全的
{
BitmapData data1=bmp1.LockBits(新矩形(0,0,bmp1.Width,bmp1.Height),ImageLockMode.ReadOnly,bmp1.PixelFormat);
BitmapData data2=bmp2.LockBits(新矩形(0,0,bmp2.Width,bmp2.Height),ImageLockMode.ReadOnly,bmp2.PixelFormat);
int pixelBytes=0;
开关(data1.PixelFormat)
{
案例PixelFormat.Format32bppArgb:
像素字节=4;
打破
案例PixelFormat.Format24bppRgb:
像素字节=3;
打破
违约:
抛出新异常(“不支持位图格式”);
}
int paddingBytes=data1.步长%pixelBytes;
字节*位置1=(字节*)数据1.Scan0;
字节*位置2=(字节*)数据2.Scan0;
对于(int y=0;y
您必须始终将较大的图像(x,y)与较小的图像进行比较。虽然我不知道你到底想要什么,但你可以这样做

 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
    }
 }
BitmapImage Image1=新的BitmapImage(ImageStream);
BitmapImage Image2=新的BitmapImage(ImageStream);
int X=Image1.Width>Image2.Width?图2.宽度:图1.宽度;
int Y=Image1.hight>Image2.Height?图2.高度:图1.高度;
对于(int x=0;x
您想要实现什么目标?说明你的问题可能是我能找到更好的方法来解决,你是在尝试比较两幅图像的相似性吗?事实上,我正在这样做,但没有给出准确的结果。。现在我想将一个像素与其他图像的所有像素进行比较。@Shahbaz如果你想匹配两个图像,那么计算两个图像的哈希值并进行比较,但是如果你正在进行模式匹配,你可以使用这个或查找一些现成的库。但是,在最低级别,您至少会有一个嵌套循环,因此我认为不会有更快的方法。请具体说明你想做什么,这样我也许能给你一个更准确的答案。@Shahbaz:如果你在这个嵌套循环的正确时间添加中断,它肯定会快得多,但请记住,我还没有足够的信息来说明你到底想实现什么。GetPixel是出了名的慢。同样的比较应该可以使用锁位来完成。