只是想知道是否有办法在 WPF 中比较两个不同的位图源?
我的场景是我试图在网络摄像头上制作一个简单的运动传感器,并且只是定期从网络摄像头拍摄照片。现在需要获取从镜头中检索到的 bitmapSources 并检查它们之间是否有任何增量(即相机馈送中的某些东西已移动)。
提前致谢
只是想知道是否有办法在 WPF 中比较两个不同的位图源?
我的场景是我试图在网络摄像头上制作一个简单的运动传感器,并且只是定期从网络摄像头拍摄照片。现在需要获取从镜头中检索到的 bitmapSources 并检查它们之间是否有任何增量(即相机馈送中的某些东西已移动)。
提前致谢
你可以尝试这样的事情: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;
}
}
}
这是上一个讨论线程中的一个更快的示例。看看是否效果更好。
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;
}
}