用emgucv测量两幅图像的差值

本文关键字:图像 两幅 emgucv 测量 | 更新日期: 2023-09-27 18:17:45

我需要比较两张图片,并以百分比来识别它们的差异。emgucv上的"Absdiff"功能对此没有帮助。我已经在emgucv wiki上做过比较的例子了。我真正想要的是如何在数字格式中得到两个图像的差异?

//emgucv wiki compare example
//acquire the frame
Frame = capture.RetrieveBgrFrame(); //aquire a frame
 Difference = Previous_Frame.AbsDiff(Frame);
//what i want is
double differenceValue=Previous_Frame."SOMETHING";

如果你需要更多的细节,请询问。

用emgucv测量两幅图像的差值

基于EmguCV MatchTemplate的比较

Bitmap inputMap = //bitmap  source image
Image<Gray, Byte> sourceImage = new Image<Gray, Byte>(inputMap);
Bitmap tempBitmap = //Bitmap template image
Image<Gray, Byte> templateImage = new Image<Gray, Byte>(tempBitmap);
Image<Gray, float> resultImage = sourceImage.MatchTemplate(templateImage, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed);
double[] minValues, maxValues;
Point[] minLocations, maxLocations;
resultImage.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
double percentage = maxValues[0] * 100; //this will be percentage of difference of two images

两个图像需要具有相同的宽度和高度,否则MatchTemplate将抛出异常。以防我们想要完全匹配。或模板图像应该小于源图像,以获得模板图像在源图像

上出现的次数。

基于EmguCV AbsDiff的比较

Bitmap inputMap = //bitmap  source image
Image<Gray, Byte> sourceImage = new Image<Gray, Byte>(inputMap);
Bitmap tempBitmap = //Bitmap template image
Image<Gray, Byte> templateImage = new Image<Gray, Byte>(tempBitmap);
Image<Gray, byte> resultImage = new Image<Gray, byte>(templateImage.Width, 
templateImage.Height);
CvInvoke.AbsDiff(sourceImage, templateImage, resultImage);
double diff = CvInvoke.CountNonZero(resultImage);
diff = (diff / (templateImage.Width * templateImage.Height)) * 100; // this will give you the difference in percentage
根据我的经验,与基于MatchTemplate的比较相比,这是最好的方法。匹配模板未能捕获两个图像中非常小的变化。但是AbsDiff也可以捕捉到非常小的差异