Ask Your Question

Take frame after some millisecond

asked 2015-09-11 14:27:21 -0500

Nikre93 gravatar image

updated 2015-09-12 02:41:44 -0500

berak gravatar image

Hi, this is my problem: I wuold like to implement a motion detection. In Internet i found that the motion is captured using two different frame, and with the function Core.absdiff() put the differce in a new Mat. I haven't undestand how i take a frame, and after 0.1 second take another frame to do the difference. How i can do?

Thanks for yuor time.

edit retag flag offensive close merge delete


In a first way I used an Array list, but doesn't work, because i done a difference of two equal frame: result, a black screen.

Nikre93 gravatar imageNikre93 ( 2015-09-11 14:38:39 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2015-09-20 01:24:08 -0500

RolandC gravatar image

You could do it e. g. this way:


VideoCapture videoCapture = new VideoCapture();;


Mat mat = new Mat();
Mat prevMat = null;
Mat diffMat = new Mat();

while( true) {

    // get frame;

    // difference
    if( prevMat != null) {
        Core.absDiff(mat, prevMat, diffMat);

    // remember mat
    prevMat = mat.clone();

    // wait

The difference image is in diffMat.

edit flag offensive delete link more


if i would use the mat, and camera input without using the VideoCapture. Iìm working in real time...

Nikre93 gravatar imageNikre93 ( 2015-10-16 08:46:38 -0500 )edit

answered 2015-09-11 16:23:57 -0500

albertJ gravatar image

Something like this will get you two images and save them to two matrixes then you can always pass them to functions from inside the loop for processing.

  VideoCapture stream(1);
    cout << "Video stream unable to be opened exiting.." << endl;

  Mat img1, img2;

  namedWindow("curImg", CV_WINDOW_AUTOSIZE);

  int w_width = 200;
  int w_height = 75;
  int counter=0;
  int delay = 100; // delay in milliseconds

    cap >> img;
    Mat frame = Mat(w_height, w_width, CV_8UC3, Scalar(255,255,255));

    if(counter%2 ==0){

    imshow("curImg", frame);
edit flag offensive delete link more


i try, i forget to add in the ask that i program in java, an app for android

Nikre93 gravatar imageNikre93 ( 2015-09-12 04:15:47 -0500 )edit

oh i've only worked with the c++ api, so cant help you there

albertJ gravatar imagealbertJ ( 2015-09-12 05:31:43 -0500 )edit

For java you can use HighGui.waitKey(delay);

Vardan16 gravatar imageVardan16 ( 2018-07-14 22:54:06 -0500 )edit

Question Tools

1 follower


Asked: 2015-09-11 14:27:21 -0500

Seen: 1,076 times

Last updated: Sep 20 '15