3D clod point to 2D image

asked 2016-07-21 02:08:07 -0600

MariaThomas gravatar image

updated 2016-07-21 02:27:10 -0600

Hello,

I have 3D point cloud data which I want to convert into 2D image and save to the disk as 16 bit png image. I am using projectPoints() function which is giving me the projected points in the form vector<>point2d> which is a 2 channel image which i cannot use in imWrite(). Is there any other way to do this? Kindly help. Code:

  std::vector<cv::Point3d> objectPoints;
  std::vector<cv::Point2d> imagePoints;
  projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, imagePoints);
  depthImage = Mat(imagePoints);
  imwrite("E:/softwares/1.8.0.71/bin/depthImage1.png", depthImage);

I am getting assertion failed error because of the channel value. I have also tried the below code which is a 24 bit png image. Can I convert this to 16 bit?

 Mat depthImage = cv::Mat(height, width, CV_32FC3);
 int i = 0;
    for (int h = 0; h < depthImage.rows; h++)
    {
        for (int w = 0; w < depthImage.cols; w++)
        {
            depthImage.at<cv::Vec3f>(h, w)[0] = imagePoints[i].x ;
                depthImage.at<cv::Vec3f>(h, w)[1] = imagePoints[i].y;
            depthImage.at<cv::Vec3f>(h, w)[2] = 1;

            i++;
        }
    }
edit retag flag offensive close merge delete

Comments

use: depthImage.at<cv::Vec3w>(h,w) , if your image is CV_16UC3

(also, your 3rd chan will be almost black)

berak gravatar imageberak ( 2016-07-21 02:24:00 -0600 )edit

sorry. I was using CV_32FC3. Please see the edited version.

MariaThomas gravatar imageMariaThomas ( 2016-07-21 02:27:46 -0600 )edit

now, you cannot imwrite() a float image.

either use CV_16UC3 and change the at(), or

Mat u16; depthImage.convertTo(u16, CV_16U, some_scale_factor);

berak gravatar imageberak ( 2016-07-21 02:30:46 -0600 )edit

I tried with depthImage.at<cv::vec3w>(h,w) as well. But the bit depth of the resulting image is 48.

MariaThomas gravatar imageMariaThomas ( 2016-07-21 02:31:52 -0600 )edit

yes, with 16 bits it's 48 for 3 channels, with8 bits 24.

if you want 24 bits, you need CV_8UC3 and cv::Vec3b, also you need to downscale the values you write into the image, if it's larger than 256x256

berak gravatar imageberak ( 2016-07-21 02:38:30 -0600 )edit

I want 16 bit png

MariaThomas gravatar imageMariaThomas ( 2016-07-21 02:43:35 -0600 )edit