如何比较两个图像并提取其差异?

时间:2016-02-02 02:06:38

标签: c# emgucv

我有两组具有相同大小和像素的图像。现在我必须将第一个图像的selectedFrame与第二个图像的backImageFrame进行比较。我需要在图像中获取差异并提取它,以便我可以在ImageBox中输出它。现在,我正在使用EmguCV的AbsDiff函数

 selectedFrame.ROI = recArray[random];
 backImageFrame.ROI = recArray[random];
 // backImageFrame = selectedFrame.AbsDiff(backImageFrame);
 CvInvoke.AbsDiff(selectedFrame, backImageFrame, backImageFrame)
 imgTry.Image = backImageFrame;
 imageBox1.Image = selectedFrame;

imgTry ImageBox中没有任何值

3 个答案:

答案 0 :(得分:5)

您可以使用Image API查找一个图像与另一个图像之间的差异,然后您可以定义要考虑的差异的阈值并应用它。

代码如下:

Image<Bgr, Byte> Frame; //current Frame from camera
Image<Bgr, Byte> Previous_Frame; //Previiousframe aquired
Image<Bgr, Byte> Difference; //Difference between the two frames
int Threshold = 60; //stores threshold for thread access



Difference = Previous_Frame.AbsDiff(Frame); //find the absolute difference 
                 /*Play with the value 60 to set a threshold for movement*/    
Difference = Difference.ThresholdBinary(new Bgr(Threshold, Threshold, Threshold), new Bgr(255,255,255)); //if value > 60 set to 255, 0 otherwise 

跟进this example以便更好地理解。

答案 1 :(得分:0)

这适合我。

Image<Gray, Byte> img1 = picPrev.Convert<Gray, Byte>();
Image<Gray, Byte> img2 = picCurrent.Convert<Gray, Byte>();
Image<Gray, Byte> img3;
img3 = img1 - img2; //Here the difference is applied.
pictureBox3.Image = img3.ToBitmap();

答案 2 :(得分:0)

基于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也将能够捕获很小的差异