# Std::queue of Mat

**URL:** <https://forum.opencv.org/t/std-queue-of-mat/6680>\
**Category:** Uncategorized\
**Created:** [December 12, 2021, 1:29pm UTC](https://forum.opencv.org/t/std-queue-of-mat/6680 "2021-12-12T13:29:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![makoe91](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@makoe91](https://forum.opencv.org/u/makoe91)\
**Post date:** [December 12, 2021, 1:29pm UTC](https://forum.opencv.org/t/std-queue-of-mat/6680/1 "2021-12-12T13:29:05Z")

</div>

Because I’am new to OpenCV, I have another, maybe silly, question: I added a queue of the standard template library to my program, which stores a number of into `cv::Mat` converted frames. Other methods which do the processing part (reading RGB values, drawing a histogram etc.) should read the frames from this queue for further processing. Is this an acceptable solution?

```auto

/**
* Defines a Queue that stores a constant number of frames
*/
std::queue<cv::Mat> ConvertVideoFormat::StoreFrame(cv::Mat frame) {

    std::queue<cv::Mat> frameQueue;

    //store only 1 sec into the queue
    for (int i = 1; i <= 25; ++i) {
        frameQueue.push(frame);
    }

    qDebug() << "FrameQueue: " << frameQueue.empty();
    return frameQueue;
}

cv::Mat ConvertVideoFormat::DeckLinkToCv(CComPtr<IDeckLinkVideoFrame> theFrame) {

    void* data; 
    theFrame->GetBytes(&data); //allows direct access to the data buffer of a video frame

    int frameHeight = theFrame->GetHeight();
    int frameWidth = theFrame->GetWidth();
    
    m_inputFrame = cv::Mat(frameHeight, frameWidth, CV_8UC2, data, theFrame->GetRowBytes()); //Ctor 11/25
    m_outputFrame = cv::Mat(frameHeight, frameWidth, CV_8UC4);
    cv::cvtColor(m_inputFrame, m_outputFrame, cv::COLOR_YUV2BGR_UYVY);
    m_copyConvFrame = m_outputFrame.clone();    

    StoreFrame(m_copyConvFrame);
    
    return m_copyConvFrame;
}

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [December 12, 2021, 2:34pm UTC](https://forum.opencv.org/t/std-queue-of-mat/6680/2 "2021-12-12T14:34:15Z")

</div>

> [@makoe91](#):
>
> should read the frames from this queue for further processing. Is this an acceptable solution?

why do you need a queue ? why put 25 times the sme frame into it ?

> [@makoe91](#):
>
> `m_outputFrame = cv::Mat(frameHeight, frameWidth, CV_8UC4);`

please dont preallocate output Mat’s. it will be overwritten anyway, and the `cvtColor()` result will be CV\_8UC3, because you said so in the flag

---

<div class="post-metadata">

**Author:** ![makoe91](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@makoe91](https://forum.opencv.org/u/makoe91)\
**Post date:** [December 13, 2021, 9:09am UTC](https://forum.opencv.org/t/std-queue-of-mat/6680/3 "2021-12-13T09:09:42Z")

</div>

Hi @berak,

> [@berak](#):
>
> why do you need a queue ? why put 25 times the sme frame into it ?

Maybe a misunderstanding. I thought `ConvertVideoFormat::DeckLinkToCv()` creates a cv::Mat of every incoming single frame (for example: if the incoming stream has 25 fps, 25 cv::Mat’s are created).  
Based on this understanding, my idea was to to create a “manual buffer” that caches the frames. This allows me stop the videostream and anaylize the video, because the video rendering and the conversion process requires lot of CPU power on my system.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [December 13, 2021, 11:11am UTC](https://forum.opencv.org/t/std-queue-of-mat/6680/4 "2021-12-13T11:11:42Z")

</div>

> [@makoe91](#):
>
> if the incoming stream has 25 fps, 25 cv::Mat’s are created

there is no reason to assume that _one second_ of buffers is required or beneficial, assuming your code is effective in achieving that goal (it is not).

apart from that, your code just discards the function call’s return value (that queue), so it’s all a waste.

decklink doesn’t need these acrobatics nor could it make use of them. just remove the code.

if this code is intended to be read or used by anyone other than yourself (work? college?), get them to review your code thoroughly and ask for honest feedback.

---

<div class="post-metadata">

**Author:** ![makoe91](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@makoe91](https://forum.opencv.org/u/makoe91)\
**Post date:** [December 13, 2021, 11:36am UTC](https://forum.opencv.org/t/std-queue-of-mat/6680/5 "2021-12-13T11:36:17Z")

</div>

> [@crackwitz](#):
>
> decklink doesn’t need these acrobatics nor could it make use of them. just remove the code.

Do you mean the queue is a waste or the format conversion from Decklink to `cv::Mat` with `cvtColor`?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [December 13, 2021, 1:04pm UTC](https://forum.opencv.org/t/std-queue-of-mat/6680/6 "2021-12-13T13:04:44Z")

</div>

> [@makoe91](#):
>
> `StoreFrame`

I’m saying that function does nothing, so remove it.
