0

I use ZR300 to have depth data.

I took depth data in two ways.

The first one is directly take depth stream

const uint16_t * depth_image = (const uint16_t *)dev->get_frame_data(rs::stream::depth);

Full script is

        const uint16_t * depth_image = (const uint16_t *)dev->get_frame_data(rs::stream::depth);
        float scale = dev->get_depth_scale();
        float **data;
        data = new float *[HEIGHT];
        for(int dy=0; dy<depth_intrin.height; ++dy)
        {
            data[dy] = new float[WIDTH];
            for(int dx=0; dx<depth_intrin.width; ++dx)
            {
                 uint16_t depth_value = depth_image[dy * depth_intrin.width + dx];
                 float depth_in_meters = depth_value * scale;
                  if(depth_value == 0) continue;
                 data[dy][dx] = depth_in_meters;
            }
        }
        Mat depthimage(HEIGHT, WIDTH, CV_32FC1, *data);
        Mat object;
        threshold(depthimage, object, 0.5, 255, THRESH_BINARY);
        imshow("object", object);
        waitKey(1);

The second approach is as shown in the example as

        glPixelTransferf(GL_RED_SCALE, 0xFFFF * dev->get_depth_scale() / 2.8f);
        glDrawPixels(WIDTH, HEIGHT, GL_RED, GL_UNSIGNED_SHORT, dev->get_frame_data(rs::stream::depth));
        glPixelTransferf(GL_RED_SCALE, 1.0f);

But the two images do not match. Two images are shown side by side and the first one is using the first method. Why the first method does not show correct image. enter image description here

batuman
  • 7,066
  • 26
  • 107
  • 229

1 Answers1

0

I think the problem is from how you initiate the array and put into the Matrix, you can check the link to see where the problem is.

Attached the working sample below as well.

float* d = new float[w*h];
for (int dy = 0; dy<intrins.height; ++dy)
{
    for (int dx = 0; dx<intrins.width; ++dx)
    {
        uint16_t depth_value = depth_image[dy * intrins.width + dx];
        float depth_in_meters = depth_value * scale;
        if (depth_value == 0) { d[dy * intrins.width + dx] = 0;  continue; }
        d[dy * intrins.width + dx] = depth_in_meters;
    }
}
Mat depthimage(h, w, CV_32FC1, d);
Mat object;
threshold(depthimage, object, 0.5, 255, THRESH_BINARY);
imshow(window_name, object);
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135