0
为了好玩,我试着做这个工作: 如果我离开PC,它应该在几秒钟后锁定它。两张图片看起来几乎完全相同
所以我正在初始化一个计时器并每3秒拍一张照片。我计算Hashvalue并将其与旧图片进行比较。这里是我的测试代码:
Timer _timer;
WebCam _webCam;
Bitmap _bitmap;
public CameraChecker()
{
_webCam = new WebCam();
_webCam.Connect();
_timer = new Timer(TimerCb, null, 0, 3000);
}
private void TimerCb(Object stateInfo)
{
_webCam.Update();
Bitmap newBitmap = _webCam.CalcBitmap();
if (_bitmap == null)
_bitmap = newBitmap;
else
{
ImageConverter conv = new ImageConverter();
byte[] bytesNew = (byte[])conv.ConvertTo(newBitmap, typeof(byte[]));
byte[] bytesOld = (byte[])conv.ConvertTo(_bitmap, typeof(byte[]));
//IStructuralEquatable eqa1 = bytesNew;
//bool eq = eqa1.Equals(bytesOld, StructuralComparisons.StructuralEqualityComparer);
//Compute a hash for each image
SHA256Managed shaM = new SHA256Managed();
byte[] hash1 = shaM.ComputeHash(bytesNew);
byte[] hash2 = shaM.ComputeHash(bytesOld);
//Compare the hash values
bool eq = true;
for (int i = 0; i < hash1.Length && i < hash2.Length; i++)
{
if (hash1[i] != hash2[i])
{
eq = false;
break;
}
}
if (eq)
{
System.Diagnostics.Debugger.Break();
}
else
{
_bitmap = newBitmap;
}
}
}
我正在使用MetriCam SDK来拍照,这似乎工作得很好。
我的问题:它从来没有发生,两个图片具有完全相同的值。即使我尝试变暗相机,它似乎也不起作用。
正如你所看到的,我尝试使用StructuralEquatable,但它没有工作。
我的测试项目是徒劳的吗?
像往常一样,感谢您的任何建议或提示!
http://en.wikipedia.org/wiki/Entropy –
如果您使用太阳作为光源,它会移动;即图片是不同的。 –
这种情况类似于浮点计算,因为我们不检查两个数之间的相等性,只是差值低于某个阈值。 –