【问题标题】:How to compare two images and extract its difference?如何比较两张图像并提取其差异?
【发布时间】:2020-08-05 14:27:29
【问题描述】:

我有两组大小和像素相同的图像。现在我必须将第一个图像 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


    【解决方案1】:

    您可以使用 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 以便更好地理解。

    【讨论】:

    【解决方案2】:

    这对我有用。

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

    【讨论】:

      【解决方案3】:

      基于 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 也能够捕捉到非常小的差异

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-20
        相关资源
        最近更新 更多