比较两个BitmapSources

时间:2011-12-14 01:18:45

标签: .net wpf image bitmap motion-detection

只是想知道是否有办法在WPF中比较两个不同的bitmapsource?

我的情况是我试图在网络摄像头上制作一个简单的运动传感器,我只是定期从网络摄像头拍摄。现在需要从镜头中检索bitmapSource并检查它们之间是否有任何增量(即在相机源中移动了某些东西)。

提前致谢

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情: http://www.codeproject.com/KB/GDI-plus/comparingimages.aspx

样品:

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;
        }
    }
}

答案 1 :(得分:0)

这是最后一个讨论主题深入的一个更快的例子。看看它是否有效。

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;
    }
}