3D clod point to 2D image
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++;
}
}
use:
depthImage.at<cv::Vec3w>(h,w)
, if your image is CV_16UC3(also, your 3rd chan will be almost black)
sorry. I was using CV_32FC3. Please see the edited version.
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);
I tried with depthImage.at<cv::vec3w>(h,w) as well. But the bit depth of the resulting image is 48.
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
I want 16 bit png