.net 比较两个位图源

.net 比较两个位图源,.net,wpf,image,bitmap,motion-detection,.net,Wpf,Image,Bitmap,Motion Detection,只是想知道是否有一种方法可以在WPF中比较两个不同的位图源 我的设想是,我试图在网络摄像头上制作一个简单的运动传感器,而摄像头只是周期性地从网络摄像头上拍照。现在需要获取从快照中检索到的位图源,并检查它们之间是否存在任何增量(即,相机馈送中有东西移动) 提前感谢您可以尝试以下内容: 样本: using System; using System.Drawing; using System.Drawing.Imaging; using System.Security.Cryptography;

只是想知道是否有一种方法可以在WPF中比较两个不同的位图源

我的设想是,我试图在网络摄像头上制作一个简单的运动传感器,而摄像头只是周期性地从网络摄像头上拍照。现在需要获取从快照中检索到的位图源,并检查它们之间是否存在任何增量(即,相机馈送中有东西移动)


提前感谢

您可以尝试以下内容:

样本:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Security.Cryptography;

namespace Imagio
{
    public class ComparingImages
    {
        public enum CompareResult
        {
            ciCompareOk,
            ciPixelMismatch,
            ciSizeMismatch
        };

        public static CompareResult Compare(Bitmap bmp1, Bitmap bmp2)
        {
            CompareResult cr = CompareResult.ciCompareOk;

            //Test to see if we have the same size of image
            if (bmp1.Size != bmp2.Size)
            {
                cr = CompareResult.ciSizeMismatch;
            }
            else
            {
                //Convert each image to a byte array
                System.Drawing.ImageConverter ic = 
                       new System.Drawing.ImageConverter();

                byte[] btImage1 = new byte[1];
                btImage1 = (byte[])ic.ConvertTo(bmp1, btImage1.GetType());
                byte[] btImage2 = new byte[1];
                btImage2 = (byte[])ic.ConvertTo(bmp2, btImage2.GetType());

                //Compute a hash for each image
                SHA256Managed shaM = new SHA256Managed();
                byte[] hash1 = shaM.ComputeHash(btImage1);
                byte[] hash2 = shaM.ComputeHash(btImage2);

                //Compare the hash values
                for (int i = 0; i < hash1.Length && i < hash2.Length 
                              && cr == CompareResult.ciCompareOk; i++)
                {
                    if (hash1[i] != hash2[i])
                        cr = CompareResult.ciPixelMismatch;
                }
            }
            return cr;
        }
    }
}
使用系统;
使用系统图;
使用系统、绘图、成像;
使用System.Security.Cryptography;
名称空间意象
{
公共类比较图像
{
公共枚举比较结果
{
ciCompareOk,
Cipixelmissmatch,
一致性匹配
};
公共静态比较结果比较(位图bmp1、位图bmp2)
{
CompareResult cr=CompareResult.ciCompareOk;
//测试看我们是否有相同大小的图像
如果(bmp1.Size!=bmp2.Size)
{
cr=比较结果;Crizemismatch;
}
其他的
{
//将每个图像转换为字节数组
System.Drawing.ImageConverter ic=
新系统.Drawing.ImageConverter();
字节[]btImage1=新字节[1];
btImage1=(字节[])ic.ConvertTo(bmp1,btImage1.GetType());
字节[]btImage2=新字节[1];
btImage2=(字节[])ic.ConvertTo(bmp2,btImage2.GetType());
//计算每个图像的哈希值
SHA256Managed shaM=新的SHA256Managed();
字节[]hash1=shaM.ComputeHash(btImage1);
字节[]hash2=shaM.ComputeHash(btImage2);
//比较散列值
对于(int i=0;i
这是上一个讨论主题中深入讨论的一个更快的示例。看看效果是否更好

public sealed class ImageHash
{
    private static SHA256Managed _shaM;
    private static SHA256Managed shaM
    {
        get
        {
            if (_shaM == null)
                _shaM = new SHA256Managed();

            return _shaM;
        }
    }

    private static System.Drawing.ImageConverter _imageConverter;
    private static System.Drawing.ImageConverter imageConverter
    {
        get
        {
            if (_imageConverter == null)
                _imageConverter = new System.Drawing.ImageConverter();

            return _imageConverter;
        }
    }

    public Image Image { get; private set; }

    private byte[] _Hash;
    public byte[] Hash
    {
        get
        {
            if (_Hash == null)
            {
                _Hash = (byte[])imageConverter.ConvertTo(Image, typeof(byte[]));
                _Hash = shaM.ComputeHash(_Hash);
            }

            return _Hash;
        }
    }

    public ImageHash(Image image)
    {
        this.Image = image;
    } 
}

public static class ComparingImages
{
    public enum CompareResult
    {
        ciCompareOk,
        ciPixelMismatch,
        ciSizeMismatch
    };

    public static CompareResult Compare(ImageHash img1, ImageHash img2)
    {
        CompareResult cr = CompareResult.ciCompareOk;

        //Test to see if we have the same size of image
        if (img1.Image.Size != img2.Image.Size)
        {
            cr = CompareResult.ciSizeMismatch;
        }
        else
        {
            for (int i = 0; i < img1.Hash.Length && i < img2.Hash.Length
                              && cr == CompareResult.ciCompareOk; i++)
            {
                if (img1.Hash[i] != img2.Hash[i])
                    cr = CompareResult.ciPixelMismatch;
            }
        }
        return cr;
    }
}
公共密封类ImageHash
{
私人静态sha256管理(u shaM);;
私人静态SHA256管理假
{
得到
{
如果(_shaM==null)
_shaM=新的SHA256Managed();
返回(u shaM);;
}
}
专用静态系统.Drawing.ImageConverter\u ImageConverter;
专用静态系统.Drawing.ImageConverter ImageConverter
{
得到
{
如果(_imageConverter==null)
_imageConverter=新系统.Drawing.imageConverter();
返回图像转换器;
}
}
公共映像映像{get;private set;}
专用字节[]_散列;
公共字节[]散列
{
得到
{
if(_Hash==null)
{
_Hash=(字节[])imageConverter.ConvertTo(图像,类型)(字节[]);
_Hash=shaM.ComputeHash(_Hash);
}
返回散列;
}
}
公共图像哈希(图像)
{
这个。图像=图像;
} 
}
公共静态类比较图像
{
公共枚举比较结果
{
ciCompareOk,
Cipixelmissmatch,
一致性匹配
};
公共静态比较结果比较(ImageHash img1、ImageHash img2)
{
CompareResult cr=CompareResult.ciCompareOk;
//测试看我们是否有相同大小的图像
if(img1.Image.Size!=img2.Image.Size)
{
cr=比较结果;Crizemismatch;
}
其他的
{
对于(int i=0;i
我不知道为什么,但每次位图源都是像素不匹配的,即使它们看起来是相同的。我收集的图像可能有肉眼看不到的细微差别。是否需要设置一个阈值,以便将小的增量考虑在内?这可能与头信息有关。如果他们有不同的时间戳,我想这就足够了。我认为作者没有提到这一点。