<p>I am trying to retrieve OpenGL's color buffer with the following code. My program is crasing in the line that makes the call to <strong>glReadPixels()</strong> apparently <code>image.data</code> has not enough memory but how could it be if i'm getting the size from <strong>GL_VIEWPORT</strong> ?</p>

<p>Am I missing something else? I already checked for errors with <strong>glGetError()</strong> and it returns 0 right before the call to glReadPixels.</p>

<p>Any ideas?</p>

<pre><code>void GLWidget::readGLBuffer(cv::Mat&amp; image)
{
    GLint vp[4];
    glGetIntegerv(GL_VIEWPORT, vp);

    const GLint&amp; cols = vp[2];
    const GLint&amp; rows = vp[3];

    if (rows * cols &lt;= 0)
        return;

    image = cv::Mat(rows, cols, CV_8UC3);

    glReadBuffer(GL_FRONT); // glReadPixels reads the back buffer by default
    glReadPixels(vp[0], vp[1], cols, rows, GL_RGB, GL_UNSIGNED_BYTE, image.data);
}
</code></pre>

CC BY-SA 4.0
2018-08-06T09:05:44.927Z
Check if your `Mat` is continuous in memory and not strided by using the `isContinuous` flag. Maybe OpenCV allocates extra bytes for padding, that's why it's running short?. Also check this link.
https://stackoverflow.com/questions/9097756/converting-data-from-glreadpixels-to-opencvmat
gallickgunner
CC BY-SA 4.0
2018-08-06T11:35:20.293Z
@gallickgunner thanks a ton it was indeed a problem with cv::Mat and glPixelStorei, your link was really helpful, how can I mark this as answered or make it point to the link?
BRabbit27
CC BY-SA 4.0
2018-08-06T11:47:48.540Z
Just quote part or whole of the answer and post the link and mark it yourself :)
gallickgunner
<opengl>
How to compute size of OpenGL's image buffer?
BRabbit27
# How to compute size of OpenGL's image buffer?

(Asked by: username_0 on 2018-08-06)

I am trying to retrieve OpenGL's color buffer with the following code. My program is crasing in the line that makes the call to **glReadPixels()** apparently `image.data` has not enough memory but how could it be if i'm getting the size from **GL\_VIEWPORT** ?

Am I missing something else? I already checked for errors with **glGetError()** and it returns 0 right before the call to glReadPixels.

Any ideas?

```
void GLWidget::readGLBuffer(cv::Mat& image)
{
    GLint vp[4];
    glGetIntegerv(GL_VIEWPORT, vp);

    const GLint& cols = vp[2];
    const GLint& rows = vp[3];

    if (rows * cols <= 0)
        return;

    image = cv::Mat(rows, cols, CV_8UC3);

    glReadBuffer(GL_FRONT); // glReadPixels reads the back buffer by default
    glReadPixels(vp[0], vp[1], cols, rows, GL_RGB, GL_UNSIGNED_BYTE, image.data);
}

```

**Tags:** opengl

> **Comments:**
> **username_1**: Check if your `Mat` is continuous in memory and not strided by using the `isContinuous` flag. Maybe OpenCV allocates extra bytes for padding, that's why it's running short?. Also check this link. https://stackoverflow.com/questions/9097756/converting-data-from-glreadpixels-to-opencvmat  
> **username_0**: @username_1 thanks a ton it was indeed a problem with cv::Mat and glPixelStorei, your link was really helpful, how can I mark this as answered or make it point to the link?  
> **username_1**: Just quote part or whole of the answer and post the link and mark it yourself :)  

---

## 0 answers

