baidu / boteye

210 stars 72 forks source link

How to convert the depth image of 8UC3 format to 16UC1? #28

Open MengNan-Li opened 6 years ago

mingyux commented 6 years ago

@MengNan-Li I assume you want to retrieve the disparity map. We actually expose the depth (xyz in the camera coordinate) converted from the disparity map. Below is the example code of how the conversion is done. You can simply undo it. We will take note and may include exposing the disparity map in the next release.

 depth_image_.setTo(cv::Vec3f(0, 0, 0));
    for (int y = 0; y < disparity_result_.rows; ++y) {
      for (int x = 0; x < disparity_result_.cols; ++x) {
        int16_t disp = disparity_result_.at<int16_t>(y, x);
        if (disp <= 0) continue;
        // http://docs.opencv.org/3.0.0/d9/d0c/group__calib3d.html#ga1bc1152bd57d63bc524204f21fde6e02
        // [XYZW]T=𝚀∗[x y 𝚍𝚒𝚜𝚙𝚊𝚛𝚒𝚝𝚢(x,y) 1]T
        cv::Vec4f xyz_homo = duo_calib_param_.Camera.Q *
            cv::Vec4f(x, y, static_cast<float>(disp) / 16.f, 1);
        cv::Vec3f xyz_C(xyz_homo[0] / xyz_homo[3],
                        xyz_homo[1] / xyz_homo[3],
                        xyz_homo[2] / xyz_homo[3]);
        depth_image_.at<cv::Vec3f>(y, x) = xyz_C;
      }
    }