3 Replies Latest reply on Aug 11, 2017 9:58 AM by MartyG

    SR300: duplicates in depth data


      Hello everyone! Newbie RealSense user here.


      I am trying to import SR300 data into MATLAB using librealsense. The imported frame seems to capture a much smaller picture and duplicate what it does capture.


      This is what cpp-capture example shows:


      This is a lamp next to my computer screen.

      This is the same lamp (from a slightly different angle), captured with my code:


      I cannot exactly follow what the example in cpp-capture shows because it goes too deeply into GLFW and becomes unreadable. I've been following the example of the tutorials, but when running them they all have runtime errors.


      Here is my code. Notice that all functions starting with "mx", "mex" or "mw" are used as part of the MATLAB interface. A similar code works perfectly for the colour stream and I have no reason to worry about problems with the MATLAB interface.


      rs::context ctx;


          if (ctx.get_device_count() == 0) mexErrMsgTxt("No device detected. Is it plugged in?");


          rs::device *device_ptr = ctx.get_device(0);

          rs::device & dev = *device_ptr;


          dev.enable_stream(rs::stream::depth, rs::preset::best_quality);



          const rs::intrinsics depth_intrin = dev.get_stream_intrinsics(rs::stream::depth);


      // MATLAB STUFF, not interesting

      mxArray *captured_stream;
      plhs[0] = mxCreateCellMatrix(1, 5);

          mwSize Rows = depth_intrin.height;

          mwSize Columns = depth_intrin.width;

          mwSize Dims2[] = { Rows, Columns };

          captured_stream = mxCreateNumericArray(2, Dims2, mxUINT16_CLASS, mxREAL);


          uint16_t *pdata = (uint16_t *)mxGetData(captured_stream);

      // End of uninteresting MATLAB stuff



          const uint16_t *depth_frame = (const uint16_t *)dev.get_frame_data(rs::stream::depth);


          memcpy(pdata, depth_frame, Rows*Columns*mxGetElementSize(captured_stream));


          mxSetCell(plhs[0], 2, mxDuplicateArray(captured_stream));




      As you see all I am doing it copying the frame data into a MATLAB structure.

      Thank you in advance to anyone who can help.



        • 1. Re: SR300: duplicates in depth data

          Until recently, there was not much information on RealSense to MATLAB conversion.  There have been a couple of cases in the last month though, so the knowledge base has grown a little. 


          The most common issue reported is that - like your image - the measurements of the RealSense image and the .mat image are not the same.  Also, differences in the colors occur.  The discussion linked to below talks about this.


          Question about depth data


          Although I am not a MATLAB expert, some research into the duplication issue suggests that it is influenced by the Rows and Ciolumns variables. 


          Your script uses the instruction mxCreateCellMatrix(1, 5);  - my guess is that changing '5' to '1' may remove the mirroring (i.e 1 row, 1 column).

          1 of 1 people found this helpful
          • 2. Re: SR300: duplicates in depth data

            Hi MartyG! Thank you very much for your answer.


            There is indeed a problem with the rows and columns, just not the one you mentioned.

            The MATLAB cell array structure is similar to a Python list. It can contain any sort of data type as its elements. I am using it to store a 2D matrix, and the cell array's shape shouldn't influence the 2D matrix. However your comment about the rows and columns made me go back to my notes and I remembered that MATLAB organizes the matrices column by column and not row by row like realsense.


            This is actually a bit embarrassing since I added this solution to the RGB frames. I also included it in my first function for the depth frames, but somewhere along the way I deleted it for some reason.


            Anyway, for whoever will read this in the future, I solved this problem by adding these lines:


            uint16_t *Reorganized_Frame = (uint16_t *)mxMalloc(3 * Rows * Columns * mxGetElementSize(captured_stream));

                int x, y;

                for (y = 0; y < Rows; y++) {

               for (x = 0; x < Columns; x++)
               Reorganized_Frame[x*Rows + y] = depth_frame[y * Columns + x];



            Here is me waving hello to you from MATLAB.


            1 of 1 people found this helpful
            • 3. Re: SR300: duplicates in depth data

              That's the best MATLAB image I've ever seen!   


              Thanks so much for sharing your knowledge with the community.  Good luck with the rest of your project!