Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Time delay in VideoCapture opencv due to capture buffer

I use a camera to get frames continuesly.In my code,i have use VideoCapture to do that. But when i debug,i find that the frame i got is the old frame. I have asked some guys, they told me that some old frames will store in the buffer and i should use a worker thread to get the latest frame.Also there is a similar question here. Similar question

Here, i have two questions,

If the buffer can store 5 frames and now have stored 5 frames, When does the buffer update?After 5 frames have been got out or just 1 frame? Because i want to know whether i got old frames all the time or i got one new frame then four old frames then change to one lastest frame...

I have debug my code, set a breakpoint at cap >> frame. First frame(loop) Put my hand in and second loop put my hand out of the camera, I found that the image will changed after few frames. Does it mean i got one new frame then four old frames then change to one lastest frame...?

2.Because it's my first time to use thread, i am not sure my code is wrong or not, here is my code,

void task(VideoCapture cap, Mat& frame) {
    while (true) {
          cap >> frame;
    }
}
int main() {
    Mat frame, image;
    VideoCapture cap;
    cap.open(0);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1600);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
    cap >> frame;
    thread t(task, cap, frame);
    while (true) {
          frame.copyTo(image);
          ...//image processing loop
    }
}

I also have set breakpoint in task() to check the frame, but is the same condition. The image will changed after few frames.I don't know why. Is my code wrong or I can not set breakpoint to check like this as using thread? Can somebody help?Thank you very much.

Time delay in VideoCapture opencv due to capture buffer

I use a camera to get frames continuesly.In my code,i have use VideoCapture to do that. But when i debug,i find that the frame i got is the old frame. I have asked some guys, they told me that some old frames will store in the buffer and i should use a worker thread to get the latest frame.Also there is a similar question here. Similar question

Here, i have two questions,

If the buffer can store 5 frames and now have stored 5 frames, When does the buffer update?After 5 frames have been got out or just 1 frame? Because i want to know whether i got old frames all the time or i got one new frame then four old frames then change to one lastest frame...

I have debug my code, set a breakpoint at cap >> frame. First frame(loop) Put my hand in and second loop put my hand out of the camera, I found that the image will changed after few frames. Does it mean i got one new frame then four old frames then change to one lastest frame...?

2.Because it's my first time to use thread, i am not sure my code is wrong or not, here is my code,

void task(VideoCapture cap, Mat& frame) {
    while (true) {
          cap >> frame;
    }
}
int main() {
    Mat frame, image;
    VideoCapture cap;
    cap.open(0);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1600);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
    cap >> frame;
    thread t(task, cap, frame);
    while (true) {
        initUndistortRectifyMap(
            cameraMatrix,  // computed camera matrix
            distCoeffs,    // computed distortion matrix
            Mat(),     // optional rectification (none) 
            Mat(),     // camera matrix to generate undistorted
            Size(1920 * 1.3, 1080 * 1.3),
            //            image.size(),  // size of undistorted
            CV_32FC1,      // type of output map
            map1, map2);   // the x and y mapping functions
    remap(frame, frame, map1, map2, cv::INTER_LINEAR);
          frame.copyTo(image);
          ...//image processing loop
    }
}

I also have set breakpoint in task() to check the frame, but is the same condition. The image will changed after few frames.I don't know why. Is my code wrong or I can not set breakpoint to check like this as using thread? Can somebody help?Thank you very much.

click to hide/show revision 3
retagged

Time delay in VideoCapture opencv due to capture buffer

I use a camera to get frames continuesly.In my code,i have use VideoCapture to do that. But when i debug,i find that the frame i got is the old frame. I have asked some guys, they told me that some old frames will store in the buffer and i should use a worker thread to get the latest frame.Also there is a similar question here. Similar question

Here, i have two questions,

If the buffer can store 5 frames and now have stored 5 frames, When does the buffer update?After 5 frames have been got out or just 1 frame? Because i want to know whether i got old frames all the time or i got one new frame then four old frames then change to one lastest frame...

I have debug my code, set a breakpoint at cap >> frame. First frame(loop) Put my hand in and second loop put my hand out of the camera, I found that the image will changed after few frames. Does it mean i got one new frame then four old frames then change to one lastest frame...?

2.Because it's my first time to use thread, i am not sure my code is wrong or not, here is my code,

void task(VideoCapture cap, Mat& frame) {
    while (true) {
          cap >> frame;
    }
}
int main() {
    Mat frame, image;
    VideoCapture cap;
    cap.open(0);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1600);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
    cap >> frame;
    thread t(task, cap, frame);
    while (true) {
        initUndistortRectifyMap(
            cameraMatrix,  // computed camera matrix
            distCoeffs,    // computed distortion matrix
            Mat(),     // optional rectification (none) 
            Mat(),     // camera matrix to generate undistorted
            Size(1920 * 1.3, 1080 * 1.3),
            //            image.size(),  // size of undistorted
            CV_32FC1,      // type of output map
            map1, map2);   // the x and y mapping functions
    remap(frame, frame, map1, map2, cv::INTER_LINEAR);
          frame.copyTo(image);
          ...//image processing loop
    }
}

I also have set breakpoint in task() to check the frame, but is the same condition. The image will changed after few frames.I don't know why. Is my code wrong or I can not set breakpoint to check like this as using thread? Can somebody help?Thank you very much.