Push & Pop a frame out of Mat vector

asked 2016-03-05 08:42:07 -0600

MarKS9 gravatar image

updated 2016-03-05 09:59:51 -0600

I have a vector frame_queue which i am using to store incoming frames from a USB camera. I want to pop out one single frame at a time to further process those frames. I followed this link https://putuyuwono.wordpress.com/2015...

The issue is i am using 2 USB cameras and i want to grab both frames in frame_queue vector. But i am not able to use this code to push

frame_queue[i].pushback(frame);

Here is a sample code i am using :

//Make an instance of CameraStreamer
CameraStreamer cam(capture_index);

while (waitKey(20) != 27)
{
    //Retrieve frames from each camera capture thread
    for (size_t i = 0; i < capture_index.size(); i++)
    {
        Mat frame;
        //Pop frame from queue and check if the frame is valid
        cam.frame_queue[i].push_back(frame);
        //if (cam.frame_queue[i]->try_pop(frame)){
        //Show frame on Highgui window
            imshow(label[i], frame);
        //}
    }
}

frame_queue is declared as vector<mat> frame_queue. And how to pop a frame from the frame_queue if i successfully push frames into it? How do i push and pop frames into and from frame_queue[i] ?

edit retag flag offensive close merge delete

Comments

it's a bit unclear, what you're doing, perhaps it helps, if you show us your real code ?

berak gravatar imageberak ( 2016-03-05 08:50:05 -0600 )edit

please check the edited question.

MarKS9 gravatar imageMarKS9 ( 2016-03-05 08:56:19 -0600 )edit
  • it's push_back(), not pushback()
  • vector has a pop_back() member, too.
berak gravatar imageberak ( 2016-03-05 09:32:21 -0600 )edit

Sorry it was a typo. But i got to know that there is pop_back(). i can write cam.frame_queue.push_back() but not cam.frame_queue[i].push_back(). I am not sure why.

MarKS9 gravatar imageMarKS9 ( 2016-03-05 10:02:02 -0600 )edit
2

hmm, if you want a vector<Mat>per cam, shouldn't it be: vector<vector<Mat>> frame_queue(2); ?

berak gravatar imageberak ( 2016-03-05 10:22:40 -0600 )edit
1

push_back() and pop_back() do not work over a specific index i of your vector. They append and remove, respectively, one element at the end of vector, increasing or decreasing the vector size by one. If you want to access the i-th element of the vector, just do a simple assignation of Mat my_frame = frame_queue[i-th index] (maybe with a .clone() at the end depending on your scope).

LorenaGdL gravatar imageLorenaGdL ( 2016-03-05 11:55:29 -0600 )edit

well thank you for response. I figured out how to use concurrent_queue.h by installing TBB. I know this is not the answer for how to do it with vector<mat>. But i will try your suggestions for sure.

MarKS9 gravatar imageMarKS9 ( 2016-03-05 12:41:37 -0600 )edit