-1

I am trying to reconstruct world coordinates from the depth buffer in MATLAB and was wondering if someone could please help me figure out a way to get depth buffer values from a rendered image in MATLAB as there are no built-in functions that can accomplish this.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
snkhan42
  • 1
  • 1
  • I think you may be looking for this? It uses openGL to get the depth buffer through an MEX call... https://stackoverflow.com/questions/8862467/using-opengl-in-matlab-to-get-depth-buffer – WoodyDev Apr 19 '18 at 07:41
  • Thank you for your response. There seem to be errors in the sense that glGetIntegerv and glReadPixels are not being recognized by MATLAB. How should I define them? – snkhan42 Apr 20 '18 at 01:57
  • How are you calling the functions? Can you attach the code? Just to double check are you calling them from C/C++ in an MEX file or trying to call them in MATLAB (they won't be recognized directly in MATLAB code) – WoodyDev Apr 20 '18 at 06:31
  • Thank you for your response. I called them exactly it stated on the link you sent me (the shorter version of the code created by twerdster). I am sorry, I won't be able to show the code as I do not have permission to release it due to it being a research project. I hope you can still help me with this, please. – snkhan42 Apr 21 '18 at 02:10
  • I am using his code in Visual Studio 2017 (C++) file and then using mex mexGetDepth.cpp to use the function. The only errors I get is glReadPixels and glGetIntegerv identifier not found – snkhan42 Apr 21 '18 at 03:45

1 Answers1

0

It seems that the best way to do this is via an MEX interface to openGL function calls. Thanks to Twerdster he has written a cross platform MEX file for this.

#include "mex.h"   

#define GL_VIEWPORT                       0x0BA2
#define GL_DEPTH_COMPONENT                0x1902
#define GL_FLOAT                          0x1406

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    int viewport[4], i, x, y;
    int colLen;
    float *data;
    double *matrix;

    glGetIntegerv(GL_VIEWPORT, viewport);
    data = (float*)malloc(viewport[2] * viewport[3] * sizeof(float));
    glReadPixels(0, 0, viewport[2], viewport[3], GL_DEPTH_COMPONENT, GL_FLOAT, data);

    plhs[0] = mxCreateNumericMatrix(viewport[3], viewport[2], mxDOUBLE_CLASS, mxREAL);
    matrix = mxGetPr(plhs[0]);
    colLen = mxGetM(plhs[0]);

    for(x = 0; x < viewport[2]; ++ x) {
        for(y = 0; y < viewport[3]; ++ y)
            matrix[x * colLen + y] = data[x + (viewport[3] - 1 - y) * viewport[2]];
    }

    free(data);
    return;
 }

The only errors I get is glReadPixels and glGetIntegerv identifier not found

This is most likely because the library is not being linked correctly during compilation.

Assuming you are using this MEX file (the compilation will be the same method for whatever other MEX you have written); to compile this MEX file open the MATLAB command prompt / terminal and enter the following commands (Assuming you have saved your MEX file as getDepth.c) :

Windows: mex getDepth.c "path to OpenGL32.lib"

Unix: mex getDepth.c "path to opengl32.a"

This will compile this C code into an MEX callable function to be ran in MATLAB. Example:

depthData=getDepth;
figure
imshow(depthData);

I hope this helps.

WoodyDev
  • 1,386
  • 1
  • 9
  • 19
  • Thank you for your response. I tried using the compiler method, you have shown me above. It still seems to not compile due to errors with glReadPixels and glGetIntegerv identifiers not found. The "path to OpenGL32.lib", is that a general command (is it build into MATLAB or is it system specific?) Because I tried using it and it was not able to compile the code. Thank you for time and help. – snkhan42 Apr 23 '18 at 17:24
  • Apologies if that was unclear : when I wrote **path to OpenGL32.lib** It was intended that you find your own system's version of OpenGL32.lib thats installed (I found mine super quick looking through the start menu search typing exactly in OpenGL32.lib). If you cant find it you will need to install the windows SDK tools. It will be something like *C:\Program Files (x86)\Windows Kits* – WoodyDev Apr 24 '18 at 07:09
  • I looked through the Windows Kits and found the OpenGL32.Lib file. I tried compiling it with my cpp code by typing (mexGetDepth.cpp is my file name): mex mexGetDepth.cpp "path to OpenGL32.lib" but still keeping getting the same errors for some reason. I even tried it by putting OpenGL32.lib in the same folder as my code and ended up with the same errors. I am using MATLAB R2017b, is there a different way to type that command perhaps? Thank you for your continuous help. – snkhan42 Apr 24 '18 at 19:12
  • If its in the same folder then the command would be `mex mexGetDepth.cpp -lOpenGL32.lib`. Please follow [this link](http://www.mathworks.com/help/releases/R2012a/techdoc/ref/mex.html) if you are unsure about linking (the -l command) whilst compiling MEX. – WoodyDev Apr 25 '18 at 06:43