简体   繁体   中英

c++ queue.front (); why not start from the first element?

I want to create a buffer. There are 4 streams. When each one fills 300 frames, I want to show it starting from the first element of all queues. But it all starts from the 301st frame. I cannot access the 300 Frame that I filled at the beginning.Another problem although i upload the queue with different data, .front() and back() operations on the queue returns the same data

Mat checkdata;
cv::subtract(frameQueue9001.back(), frameQueue9001.front(), checkdata, noArray(), -1);
printf("sthg  goes on: %f\n",cv::norm(checkdata, NORM_L2, noArray()));

Resault: 0

I will be grateful if you could help me.

Best regards.

I shared the code below for a single stream example

#include "highgui.hpp"
#include "videoio.hpp"
#include "imgproc.hpp"
#include <opencv2/core/cuda.hpp>
#include <iostream>
#include <string.h>
#include <queue>
#include <opencv2/core.hpp>

#define quelen 300

using namespace cv;
using namespace std;

queue<Mat> frameQueue9001;


int main(int argc, char** argv)
{

    namedWindow("1", WINDOW_OPENGL);
    namedWindow("1");
    resizeWindow("1", 960, 510);
    moveWindow("1", 0, 0);


    string gst_pipe1 = "udpsrc port=9001 caps = application/x-rtp ! rtph264depay ! h264parse ! nvh264dec ! videoconvert ! appsink sync=false";

    VideoCapture cap1(gst_pipe1, CAP_GSTREAMER);

    Mat frame1;

    while (true) {

        cap1 >> frame1;
        frameQueue9001.push(frame1);   

        if(frameQueue9001.size() > quelen){ 

            imshow("1", frameQueue9001.front()); 

            frameQueue9001.pop();

        }
        //if (frame.empty())
        //    break;

        if (waitKey(1) == 'r')
            break;
    }
    destroyWindow("1");
}

Not sure if I understood the question correctly. With your condition:

if(frameQueue9001.size() > quelen)

You perform actions when your queue has 301 frames. If you want the action with 300 frames, you need to correct that to an equals or greater equals condition.

With that correction, the 300th element can be accessed with

imshow("300", frameQueue9001.back()); 

Your query method of the first element seems correct to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM