The acquisition point cloud of PCL learning

Source: Internet
Author: User

Just getting started learning PCL Point Cloud Library, some of the basic things are quite vacant. For example, there are already RGB image and depth image obtained by rgb-d sensor, how to convert it into a point cloud. Below, to compare the common point cloud Type PCL::P Ointxyzrgb, for example, describes how to obtain the corresponding point cloud data.

* * Code:

**
PCL::P Ointcloud::P tr depth2cloud (Cv::mat rgb_image, Cv::mat depth_image)
{
float F = 570.3;
float cx = 320.0, cy = 240.0;

PCL::P OINTCLOUD<PCL::P ointxyzrgb>::P tr cloud_ptr (new PCL::P OINTCLOUD<PCL::P ointxyzrgb> ());
Cloud_ptr->width = Rgb_image.cols;
Cloud_ptr->height = rgb_image.rows;

Cloud_ptr->is_dense = false; for (int y = 0; y < rgb_image.rows; + + y) {for (int x = 0; x < rgb_image.cols; + + x) {PCL::P ointxy
        Zrgb pt; if (depth_image.at<unsigned short> (y, x)! = 0) {pt.z = depth_image.at<unsigned short> (y
            , x)/1000.0;
            Pt.x = (x-cx) *pt.z/f;
            Pt.y = (y-cy) *pt.z/f;
            PT.R = rgb_image.at<cv::vec3b> (y, x) [2];
            PT.G = rgb_image.at<cv::vec3b> (y, x) [1];
            pt.b = rgb_image.at<cv::vec3b> (y, x) [0];
        Cloud_ptr->points.push_back (PT);
            } else {pt.x = Std::numeric_limits<float>::quiet_nan ();
            Pt.y = Std::numeric_limits<float>::quiet_nan (); Pt.z = Std::numeric_limits<floAt>::quiet_nan ();
            PT.R = rgb_image.at<cv::vec3b> (y, x) [2];
            PT.G = rgb_image.at<cv::vec3b> (y, x) [1];
            pt.b = rgb_image.at<cv::vec3b> (y, x) [0];
        Cloud_ptr->points.push_back (PT);
 }}} return cloud_ptr;

}
Above is a function where the input is RGB image and depth image, and the output is a boost pointer to the PCL::P ointxyzrgb type Point cloud. The point here is that since I'm getting RGB image and depth image through rgb-d sensor, the depth of the pixel location corresponding to the depth missing is set to 0, so in the conversion function, I judge the depth loss by whether the depth value is 0, Readers can make corresponding changes according to their own data, mainly to distinguish the depth of the missing or not, the impact is small. Main function

int main (int argc,char* argv[])
{

Cv::mat depth;
Cv::mat image;
Image=cv::imread (argv[1]);
Depth=cv::imread (argv[2]);
if (!image.data| |! Depth.data)
    return-1;
PCL::P OINTCLOUD<PCL::P ointxyzrgb>::P TR Cloud (New PCL::P OINTCLOUD<PCL::P ointxyzrgb>);
Cloud=depth2cloud (image,depth);

return 0;

}

Since I was in Ubuntu under the experiment, so for the PCL configuration did not spend too much effort (mainly before Windows, a few configurations are unsuccessful, and then to Ubuntu, the feeling is more convenient.) )

* * CMakeLists.txt


Because the direct copy and paste, text display will have a problem, I simply simply take a screenshot for everyone to see. The total code is fully accounted for, if the reader is not particularly clear under the Ubuntu compiler program, you can search for reference "Cmake practice" Baidu.

**

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.