在当前帧之前存储视频帧?

Storing the frame of a video before the current frame?

我目前有一段显示视频的代码,我正在尝试扩展它,以便它显示当前帧和上一帧之间的差异,从而检测运动。我知道我将不得不使用 absdiff() 函数来显示两个图像之间的对比度,但我不确定如何将前一帧存储为 Mat。有人可以看看我下面的代码并告诉我应该在哪里添加这个 'previous frame' 代码,以及我应该写什么。我假设它不会花很长时间,我只是找不到任何在线教程...

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.VideoCapture;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

// ********************************************************

public class CaptureVideo {

public static void main(String[] args) throws InterruptedException {

    // load the Core OpenCV library by name

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    // create video capture device object

    VideoCapture cap = new VideoCapture();

    // try to use the hardware device if present

    int CAM_TO_USE = 0;

    // create a new image object

    Mat matFrame = new Mat();

    // try to open first capture device (0)

    try {
        cap.open(CAM_TO_USE);
    } catch (Exception e1) {
        System.out.println("No webcam attached");

        // otherwise try opening a video file 

        try{
            cap.open("files/video.mp4");
        } catch (Exception e2) {
            System.out.println("No video file found");
        }
    }

    // if the a video capture source is now open

    if (cap.isOpened())
    {

        // create a new window object

        Imshow ims = new Imshow("From video source ... ");

        boolean keepProcessing = true;

        while (keepProcessing)
        {   
            // grab the next frame from video source

            cap.grab();

            // decode and return the grabbed video frame

            cap.retrieve(matFrame);

            // if the frame is valid (not end of video for example)

            if (!(matFrame.empty()))
            {
                // *** to any processing here*** 

                // display image with a delay of 40ms (i.e. 1000 ms / 25 = 25 fps)

                ims.showImage(matFrame);
                Thread.sleep(40);
            } else { 
                keepProcessing = false;
            }
        }

    } else {
        System.out.println("error cannot open any capture source - exiting");

    }

    // close down the camera correctly

    cap.release();

  }
}

我已经尝试在 else 语句之后添加以下代码,但生成的只是黑屏(我认为这是因为 'previousFrame' 只是 [= 的副本23=]):

            previousFrame = matFrame;

            Core.absdiff(matFrame, previousFrame, diffFrame);

            ims.showImage(diffFrame);

谁能指出我哪里错了?

previousFrame = matFrame; 使 previousFrame 指向 matFrame。您没有 复制 矩阵 - previousFramematFrame 都指的是同一个对象。因此,您实际上是在减去矩阵本身,这就是为什么您会得到一个黑框。

您需要进行适当的复制,因此您需要使用Mat.clone()克隆矩阵。但是,如果您想找出两帧之间的差异,请记住我们至少需要读入两帧才能做到这一点。当我们只读取视频的第一帧时,我们无法真正找到差异帧,因此我们应该检查一下这是否是第一帧。如果它是第一帧,则相应地显示正常帧。在那之后,我们可以通过确保在读取下一帧之前复制上一帧来显示差异帧。

因此,为您的循环执行此操作:

   // ****** Change - add a flag to check to see if the first frame has been read
   boolean firstFrame = true;

   // ****** Change - declare previous frame here
   Mat previousFrame;
   while (keepProcessing)
    {   
        // ****** Change - Save previous frame before getting next one
        // Only do this if the first frame has passed
        if (!firstFrame)
            previousFrame = matFrame.clone();

        // grab the next frame from video source
        cap.grab();

        // decode and return the grabbed video frame
        cap.retrieve(matFrame);

        // if the frame is valid (not end of video for example)
        if (!(matFrame.empty()))
        {
            // **** Change - If we are on the first frame, only show that and
            // set the flag to false
            if (firstFrame) {
                ims.showImage(matFrame);
                firstFrame = false;
            }
            // ***** Change - now show absolute difference after first frame
            else {                
                Core.absdiff(matFrame, previousFrame, diffFrame);
                ims.showImage(diffFrame);                 
            }

            // display image with a delay of 40ms (i.e. 1000 ms / 25 = 25 fps)                
            Thread.sleep(40);
        } else { 
            keepProcessing = false;
        }
    }