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

问题描述 投票:0回答:4

我有两组具有相同大小和像素的图像。现在我必须将第一张图像 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 没有任何价值

c# emgucv
4个回答
6
投票

您可以使用图像 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 

跟进这个示例以更好地理解。


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();

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 也能够捕获非常小的差异


0
投票

使用 AbsDiff 我创建了一个代码,可以使用 emgu cv 比较两个图像。

涉及使用 3 个图片框和菜单条。

private void minusToolStripMenuItem_Click(对象发送者,EventArgs e) { 尝试 { OpenFileDialog ofd1 = new OpenFileDialog(); OpenFileDialog ofd2 = new OpenFileDialog(); if (ofd1.ShowDialog() == DialogResult.OK && ofd2.ShowDialog() == DialogResult.OK) { Mat Img1 = CvInvoke.Imread(ofd1.FileName); CvInvoke.Resize(Img1, Img1, 5);

         pictureBox1.Image = Img1.ToBitmap();

         Mat Img2 = CvInvoke.Imread(ofd2.FileName);
         pictureBox2.Image = Img2.ToBitmap();
         if (pictureBox1 != null && pictureBox2 != null)
         {
             Mat SubImg = new Mat();
             CvInvoke.AbsDiff(Img1, Img2, SubImg);

             pictureBox3.Image = SubImg.ToBitmap();
         }
     }
 }
 catch
 {

 }

}

© www.soinside.com 2019 - 2024. All rights reserved.