美文网首页
pcl将图片RGB转为点云信息

pcl将图片RGB转为点云信息

作者: zjh3029 | 来源:发表于2017-09-16 16:47 被阅读0次

1.普通照片:

#include <pcl/visualization/cloud_viewer.h>  
#include <iostream>  
#include <pcl/io/io.h>  
#include <pcl/io/pcd_io.h>  
#include <opencv2/opencv.hpp>  

int user_data;

void viewerOneOff(pcl::visualization::PCLVisualizer& viewer)
{
    viewer.setBackgroundColor(0.0, 0.0, 0.0);
}

void viewerPsycho(pcl::visualization::PCLVisualizer& viewer)
{
    user_data = 9;
}

int main()
{
    pcl::PointCloud<pcl::PointXYZRGB> cloud_a;
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);


    cv::Mat image = cv::imread("1.jpg");

    int rowNumber = image.rows;
    int colNumber = image.cols;

    cloud_a.width = rowNumber;
    cloud_a.height = colNumber;
    cloud_a.points.resize(cloud_a.width*cloud_a.height);

    cv::Mat_<cv::Vec3b>::iterator it = image.begin<cv::Vec3b>();
    cv::Mat_<cv::Vec3b>::iterator itend = image.end<cv::Vec3b>();

    for (unsigned int i = 0; i<cloud_a.points.size(); ++i)
    {
        cloud_a.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud_a.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud_a.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);

        cloud_a.points[i].r = (int)(*it)[2];
        cloud_a.points[i].g = (int)(*it)[1];
        cloud_a.points[i].b = (int)(*it)[0];

        ++it;
    }

    *cloud = cloud_a;

    pcl::visualization::CloudViewer viewer("Cloud Viewer");

    viewer.showCloud(cloud);

    viewer.runOnVisualizationThreadOnce(viewerOneOff);

    viewer.runOnVisualizationThread(viewerPsycho);

    while (!viewer.wasStopped())
    {
        user_data = 9;
    }

    return 0;
}

2.摄像头中的图片:

#include <pcl/io/io.h>  
#include <pcl/io/pcd_io.h>  
#include <opencv2/opencv.hpp>  
#include <pcl/visualization/cloud_viewer.h>  

using namespace pcl;
using namespace cv;

int main()
{
    RNG rng;
    PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
    Mat img;
    VideoCapture cap(0);    cap >> img;
    if (!cap.isOpened())return -1;
    int sz = img.rows*img.cols;
    cloud->width = sz;
    cloud->points.resize(sz);

    visualization::CloudViewer viewer("Cloud Viewer");

    for (; waitKey(1) != 27;)
    {
        cap >> img;
        for (unsigned int i = 0; i < sz; i += 3)
        {
            cloud->points[i].x = rng.uniform(-1.0f, 1.0f);
            cloud->points[i].y = rng.uniform(-1.0f, 1.0f);
            cloud->points[i].z = rng.uniform(-1.0f, 1.0f);

            cloud->points[i].r = img.data[i + 2];
            cloud->points[i].g = img.data[i + 1];
            cloud->points[i].b = img.data[i];
        }
        viewer.showCloud(cloud);
    }

    return 0;
}

相关文章

网友评论

      本文标题:pcl将图片RGB转为点云信息

      本文链接:https://www.haomeiwen.com/subject/qzfusxtx.html