librealsense icon indicating copy to clipboard operation
librealsense copied to clipboard

Unable to compute aligned cloud from Color Frame

Open hrdkjain opened this issue 7 years ago • 5 comments


Required Info
Camera Model D415
Firmware Version 05.10.06.00
Operating System & Version Win 10
Platform PC
SDK Version 2.17.0
Language C++
Segment others

Issue Description

I am capturing a frameset and exporting it to ply. Then I am using the very next frameset (without moving the camera) to recreate the captured ply from color Frame by:

  1. Projecting color pixel to depth pixel using rs2_project_color_pixel_to_depth_pixel
  2. Finding depth value at projected depth pixel using DepthFrame.get_distance(depth_pixel[0],depth_pixel[1])
  3. Finally deprojecting depth pixel to 3D point using rs2_deproject_pixel_to_point. Below is the complete code:
static std::string cloudPath;
static struct rs2_intrinsics depth_intrin;
static struct rs2_intrinsics color_intrin;
static struct rs2_extrinsics depth_extrin_to_color;
static struct rs2_extrinsics color_extrin_to_depth;
static float depth_scale;
static rs2::pipeline pipe;

int main(int argc,char* argv)  {
  std::cout << " Configuring" << std::endl;
  try {
    rs2::config cfg;
    cfg.enable_stream(RS2_STREAM_COLOR,1280,720);
    cfg.enable_stream(RS2_STREAM_DEPTH,1280,720);
    cfg.enable_stream(RS2_STREAM_INFRARED,1);
    cfg.enable_stream(RS2_STREAM_INFRARED,2);

    rs2::frameset frameset;
    rs2::pointcloud pc;		
    rs2::points points;		

    std::cout << " Starting pipe" << std::endl;
    rs2::pipeline_profile profile  = pipe.start(cfg);	
    auto depth_sensor = profile.get_device().first<rs2::depth_sensor>();
    depth_sensor.set_option(RS2_OPTION_EMITTER_ENABLED,1.f);	// Enable laser
    depth_sensor.set_option(rs2_option::RS2_OPTION_VISUAL_PRESET,rs2_rs400_visual_preset::RS2_RS400_VISUAL_PRESET_DEFAULT);
    depth_scale = depth_sensor.get_depth_scale();
    // Acquire camera parameters
    const rs2::stream_profile color_profile = profile.get_stream(RS2_STREAM_COLOR);
    const rs2::stream_profile depth_profile = profile.get_stream(RS2_STREAM_DEPTH);
    depth_intrin = depth_profile.as<rs2::video_stream_profile>().get_intrinsics();
    color_intrin = color_profile.as<rs2::video_stream_profile>().get_intrinsics();
    depth_extrin_to_color = depth_profile.as<rs2::video_stream_profile>().get_extrinsics_to(color_profile);
    color_extrin_to_depth = color_profile.as<rs2::video_stream_profile>().get_extrinsics_to(depth_profile);

    //rs2::temporal_filter temporalFilter;
    std::cout << " Capturing 10 framesets to settle for auto exposure" << std::endl;
    for (int fr = 0; fr < 10; ++fr)
      pipe.wait_for_frames();
    std::cout << " Capturing a frameset for saving" << std::endl;
    frameset = pipe.wait_for_frames();

    auto colorFr = frameset.get_color_frame();
    pc.map_to(colorFr);
    rs2::depth_frame depthFr = frameset.get_depth_frame();
    points = pc.calculate(depthFr);

    frameset = pipe.wait_for_frames();
    rs2::frameset capturedFrameset = frameset;

    std::string timestring = time_in_Y_m_d_H_M_S_MMM();
    cloudPath = "./Data/" + timestring;
    std::string imagesnameend = ".ply";
    points.export_to_ply(cloudPath+imagesnameend,colorFr);
    std::cout << " stored " << cloudPath+imagesnameend << std::endl;

    std::string rgbImgPath = cloudPath + ".jpg";
    cv::Mat rgbImg = frame_to_mat(colorFr);
    cv::imwrite(rgbImgPath,rgbImg);
    recomputeCloud(capturedFrameset);
  }
  catch (const rs2::error & e)
  {
    std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n    " << e.what() << std::endl;
    std::cin.get();
    return EXIT_FAILURE;
  }
  catch (const std::exception& e)
  {
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
  }
}


void recomputeCloud(rs2::frameset &capturedFrameset) {
  rs2::depth_frame DepthFrame = capturedFrameset.get_depth_frame();
  rs2::video_frame ColorFrame = capturedFrameset.get_color_frame();
  cv::Mat ColorMat = frame_to_mat(ColorFrame);

  std::ofstream off_FS(cloudPath+"_cloud.off");
  off_FS << "COFF\n";
  off_FS << 862500 << " 0 0\n";

  // recreate complete cloud from image
  for (int r=15; r<ColorFrame.get_height()-15; r++)  {
    for (int c=15; c<ColorFrame.get_width()-15; c++)  {
      float rgb_pixel[2],depth_pixel[2];

      rgb_pixel[0] = float(c);
      rgb_pixel[1] = float(r);
      rs2_project_color_pixel_to_depth_pixel(depth_pixel,
        reinterpret_cast<const uint16_t*>(capturedFrameset.get_data()),
        depth_scale,
        0,2,
        &depth_intrin,
        &color_intrin,
        &color_extrin_to_depth,&depth_extrin_to_color,rgb_pixel);

      float udist;
      float Point[3];
      cv::Vec3b Color;
      if (depth_pixel[0] > 0)  {
        udist = DepthFrame.get_distance(depth_pixel[0],depth_pixel[1]);
        rs2_deproject_pixel_to_point(Point,&depth_intrin,depth_pixel,udist);
      } else
      {
        udist = 0;
        Point[0] = 0.0f; 
        Point[1] = 0.0f;
        Point[2] = 0.0f;
      }
      Color = ColorMat.at<cv::Vec3b>(r,c);
      off_FS << Point[0] << " " << Point[1] << " " << Point[2] << " " << int(Color(2)) << " " << int(Color(1)) << " " << int(Color(0)) << std::endl;
    }
  }
  off_FS.close();
  pipe.stop();
  std::cout << " Stopping pipe" << std::endl;
  }

And below are the four attachments:

2018_12_29_17_21_40_9341. Color Image

snapshot00_l012. Exported surface: obtained by export_to_ply

snapshot00_l003. Recomputed cloud: obtained using function recomputeCloud(rs2::frameset &capturedFrameset)

image4. Overlapped exported surface and recomputed cloud

Issues

  1. Why the recomputed cloud in Image 3 is not complete (left bottom points are missing)?
  2. Why the recompted cloud doesn't exactly match the exported surface?

P.S.: I tried to align the frameset to color frame but that didn't help.

Thanks for reading.

hrdkjain avatar Dec 29 '18 15:12 hrdkjain

Hello @hrdkjain , several comments:

rs2_project_color_pixel_to_depth_pixel(depth_pixel, reinterpret_cast<const uint16_t*>(capturedFrameset.get_data()),

A frameset does not guarantee the first frame to be a depth frame - use explicit assignment to depth.

Verify that the rgb indexes are not swapped:

// recreate complete cloud from image for (int r=15; r<ColorFrame.get_height()-15; r++) { for (int c=15; c<ColorFrame.get_width()-15; c++) { float rgb_pixel[2],depth_pixel[2]; rgb_pixel[0] = float(C); rgb_pixel[1] = float(R);

The video frame data alignment is always raw-major, hence rgb_pixel(i,j) refers to (row,column) .

I don't have a specific knowledge of how the Object file format works, but does the hard-coded value accounts for the 15 pixels margin that you impose in the repro?

off_FS << 862500 << " 0 0\n"

Also note: -Pointcloud generates a color coordinate for each depth pixel -recomputeCloud(...) calculates depth coordinate for each color pixel These are not the same

ev-mp avatar Dec 30 '18 11:12 ev-mp

Thanks for your response

rs2_project_color_pixel_to_depth_pixel(depth_pixel, reinterpret_cast<const uint16_t*>(capturedFrameset.get_data()),

A frameset does not guarantee the first frame to be a depth frame - use explicit assignment to depth.

I think you mean this modification: reinterpret_cast<const uint16_t*>capturedFrameset.first(RS2_STREAM_DEPTH).get_data())

Verify that the rgb indexes are not swapped:

// recreate complete cloud from image for (int r=15; r<ColorFrame.get_height()-15; r++) { for (int c=15; c<ColorFrame.get_width()-15; c++) { float rgb_pixel[2],depth_pixel[2]; rgb_pixel[0] = float(C); rgb_pixel[1] = float(R);

The video frame data alignment is always raw-major, hence rgb_pixel(i,j) refers to (row,column) .

I reverified it, using

rgb_pixel(i,j) refers to (row,column)

maps the transposed image.

I don't have a specific knowledge of how the Object file format works, but does the hard-coded value accounts for the 15 pixels margin that you impose in the repro?

off_FS << 862500 << " 0 0\n"

Well that's not a smart way of doing it, but this value is calculated before hand and accounts for the 15 pixel margin.

Also note: -Pointcloud generates a color coordinate for each depth pixel -recomputeCloud(...) calculates depth coordinate for each color pixel These are not the same

That's an interesting highlight, meaning that both the methods might not be able to actually give the same number of points or exactly same location of points, however don't you think that the surfaces created by both the methods should be similar. Looking at the image 4: Overlapped exported surface and recomputed cloud , its hard for me to imagine that the two surfaces would be similar but rather seems to shifted wrt to one another. Could you shed some light on that, if thats bound to happen or if there is something wrong in the recomputeCloud(...)

My aim is to detect the markers in the rgb image and locate them precisely in the 3D point cloud.

hrdkjain avatar Jan 05 '19 18:01 hrdkjain

On the last point:

  • The depth and RGB sensors are mostly overlap, but due to the horizontal displacement the rightest depth pixel columns are not in RGB's FOV. Hence when mapping depth to color, the most left columns of color pixels will not be utilized and vice versa.
  • The number of points generated is a function of the input frame resolution only and is not affected by the displacement.
  • The theoretic limit of Depth->RGB mapping precision is 0.5 pixel due to discretization of continuous (0..1) texture coordinates*. Which means that the full circle of Depth(i,j)->RGB(m,n)->Depth(x,y) mappings may certainly result in (i,j) and (x,y) being within 1 pixel radius.
  • On top of it you should add that the Depth and RGB calibrations exhibit intrinsically a certain error tolerance, which will add up for the displacements.

With all that's said, the image (4) in the above post produces quite consistent correlation, which suggests that your device is properly calibrated.

ev-mp avatar Jan 08 '19 08:01 ev-mp


Hi @hrdkjain,

Did you resolve the issue per @ev-mp's comment?


Hi @hrdkjain,

If no further question, will close this.