Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 推动及;将帧弹出到Mat向量_C++_Vector_Opencv3.0 - Fatal编程技术网

C++ 推动及;将帧弹出到Mat向量

C++ 推动及;将帧弹出到Mat向量,c++,vector,opencv3.0,C++,Vector,Opencv3.0,我试图用线程从2个USB摄像头抓取多个帧。问题是我需要将帧推入帧队列向量中,然后从向量中一次弹出一帧以进行进一步的图像处理。 我跟踪了这个链接 此链接中的代码使用concurrent_queue.h对Mat帧进行排队,但我想使用vector Mat来存储传入的帧 头文件CameraStreamer.h: #include <iostream> #include <string> #include <thread> #include <vector>

我试图用线程从2个USB摄像头抓取多个帧。问题是我需要将帧推入帧队列向量中,然后从向量中一次弹出一帧以进行进一步的图像处理。 我跟踪了这个链接

此链接中的代码使用concurrent_queue.h对Mat帧进行排队,但我想使用vector Mat来存储传入的帧

头文件CameraStreamer.h:

#include <iostream>
#include <string>
#include <thread>
#include <vector>
//#include <concurrent_queue.h>
#include <boost/thread.hpp>
#include "opencv2/videoio.hpp"

using namespace std;
using namespace cv;
//using namespace concurrency;

class CameraStreamer{
public:
//this holds camera stream urls
vector<string> camera_source;
//this holds usb camera indices
vector<int> camera_index;
//this holds OpenCV VideoCapture pointers
vector<VideoCapture*> camera_capture;
//this holds queue(s) which hold images from each camera
vector<Mat*> frame_queue;
//this holds thread(s) which run the camera capture process
vector<thread*> camera_thread;

//Constructor for IP Camera capture
CameraStreamer(vector<string> source);
//Constructor for USB Camera capture
CameraStreamer(vector<int> index);
//Destructor for releasing resource(s)
~CameraStreamer();

private:
   bool isUSBCamera;
   int camera_count;
   //initialize and start the camera capturing process(es)
   void startMultiCapture();
   //release all camera capture resource(s)
   void stopMultiCapture();
   //main camera capturing process which will be done by the thread(s)
   void captureFrame(int index);
   };
但它不起作用


因此,我不知道如何将一个帧推入并弹出到带有索引的帧队列向量中。

我不知道什么是
CameraStreamer::frame\u queue
。请按照问题中的链接查看整个代码。哦,谢谢,这很方便。“我有一个Mat vector帧队列”。似乎您没有
std::vector
。因为我使用vector,它会抛出一个错误cv::Mat没有名为try\u pop的成员。我猜它是并发的一个成员,但我没有使用它。
#include "camerastreamer.h"

CameraStreamer::CameraStreamer(vector<string> stream_source)
{
camera_source = stream_source;
camera_count = camera_source.size();
isUSBCamera = false;

startMultiCapture();
}

CameraStreamer::CameraStreamer(vector<int> capture_index)
{
camera_index = capture_index;
camera_count = capture_index.size();
isUSBCamera = true;

startMultiCapture();
}

CameraStreamer::~CameraStreamer()
{
stopMultiCapture();
}

void CameraStreamer::captureFrame(int index)
{
VideoCapture *capture = camera_capture[index];
while (true)
{
    Mat frame;
    //Grab frame from camera capture
    (*capture) >> frame;
    //Put frame to the queue
    frame_queue[index]->push(frame);
    //relase frame resource
    frame.release();
}
}

void CameraStreamer::startMultiCapture()
{
VideoCapture *capture;
thread *t;
vector<Mat> *q;
for (int i = 0; i < camera_count; i++)
{
    //Make VideoCapture instance
    if (!isUSBCamera){
    string url = camera_source[i];
            capture = new VideoCapture(url);
            cout << "Camera Setup: " << url << endl;
        }
        else{
            int idx = camera_index[i];
            capture = new VideoCapture(idx);
            cout << "Camera Setup: " << to_string(idx) << endl;
        }

        //Put VideoCapture to the vector
        camera_capture.push_back(capture);

        //Make thread instance
        t = new thread(&CameraStreamer::captureFrame, this, i);

        //Put thread to the vector
        camera_thread.push_back(t);

        //Make a queue instance
        q = new vector<Mat>;

        //Put queue to the vector
        frame_queue.push_back(q);
    }
}

void CameraStreamer::stopMultiCapture()
{
    VideoCapture *cap;
    for (int i = 0; i < camera_count; i++)
    {
        cap = camera_capture[i];
        if (cap->isOpened()){
            //Relase VideoCapture resource
            cap->release();
            cout << "Capture " << i << " released" << endl;
        }
    }
    }
int main()
{

//IP camera URLs
vector<string> capture_source = {
    "rtsp://192.168.2.100/profile2/media.smp",
    "rtsp://192.168.0.100/profile2/media.smp"
};

//USB Camera indices
vector<int> capture_index = { 0, 1 };

//Highgui window titles
vector<string> label;
for (size_t i = 0; i < capture_index.size(); i++)
{
    string title = "CCTV " + to_string(i);
    label.push_back(title);
}

//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].pushback(frame);
        //Show frame on Highgui window
            imshow(label[i], frame);
        }
    }
 }
cam.frame_queue[i].pushback(frame);