Get OpenCV image from PCL Visualizer

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Get OpenCV image from PCL Visualizer

ravijoshi
I am trying to get the image from PCL Visualizer. Below is a sample code-

---------------------------------------------------------------------
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <opencv2/opencv.hpp>
#include <pcl/visualization/pcl_visualizer.h>
#include <vtkRenderWindow.h>

int main(int argc, char** argv)
{
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new
pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::io::loadPCDFile("file.pcd", *cloud);

    pcl::visualization::PCLVisualizer viewer("Point Cloud Viewer");

    pc_viewer.addPointCloud(cloud, "cloud");
    pc_viewer.initCameraParameters();
    pc_viewer.setCameraParameters(camera);

    vtkSmartPointer<vtkRenderWindow> render = pc_viewer.getRenderWindow();
    unsigned char* pixels = render->GetRGBACharPixelData(0, 0,
render->GetSize()[0] - 1, render->GetSize()[1] - 1, 1);

    cv::Mat image = cv::Mat(render->GetSize()[1], render->GetSize()[0],
CV_8UC4, &pixels);
    cv::cvtColor(image, image, cv::COLOR_RGBA2BGRA);
    cv::flip(image, image, 0);

    return (0);
}
---------------------------------------------------------------------

The above code throws segmentation fault. I suspect that the image is not
defined properly and unsigned char* pixels is the culprit.

Well, the point cloud is consisting of PointXYZRGB. Due to project
limitation, I can't use c++11 standards.

Can someone suggest me how to get OpenCV image from PCL Visualizer?

-
Thanks
Ravi



--
Sent from: http://www.pcl-users.org/
_______________________________________________
[hidden email] / http://pointclouds.org
http://pointclouds.org/mailman/listinfo/pcl-users
Reply | Threaded
Open this post in threaded view
|

Re: Get OpenCV image from PCL Visualizer

Stephen McDowell
Where is the segfault? This line seems suspicious

cv::Mat image = cv::Mat(render->GetSize()[1], render->GetSize()[0],
CV_8UC4, &pixels);


Shouldn't it just be pixels (it's already a pointer)?



On Mon, Apr 9, 2018, 7:39 AM ravijoshi <[hidden email]> wrote:
I am trying to get the image from PCL Visualizer. Below is a sample code-

---------------------------------------------------------------------
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <opencv2/opencv.hpp>
#include <pcl/visualization/pcl_visualizer.h>
#include <vtkRenderWindow.h>

int main(int argc, char** argv)
{
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new
pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::io::loadPCDFile("file.pcd", *cloud);

    pcl::visualization::PCLVisualizer viewer("Point Cloud Viewer");

    pc_viewer.addPointCloud(cloud, "cloud");
    pc_viewer.initCameraParameters();
    pc_viewer.setCameraParameters(camera);

    vtkSmartPointer<vtkRenderWindow> render = pc_viewer.getRenderWindow();
    unsigned char* pixels = render->GetRGBACharPixelData(0, 0,
render->GetSize()[0] - 1, render->GetSize()[1] - 1, 1);

    cv::Mat image = cv::Mat(render->GetSize()[1], render->GetSize()[0],
CV_8UC4, &pixels);
    cv::cvtColor(image, image, cv::COLOR_RGBA2BGRA);
    cv::flip(image, image, 0);

    return (0);
}
---------------------------------------------------------------------

The above code throws segmentation fault. I suspect that the image is not
defined properly and unsigned char* pixels is the culprit.

Well, the point cloud is consisting of PointXYZRGB. Due to project
limitation, I can't use c++11 standards.

Can someone suggest me how to get OpenCV image from PCL Visualizer?

-
Thanks
Ravi



--
Sent from: http://www.pcl-users.org/
_______________________________________________
[hidden email] / http://pointclouds.org
http://pointclouds.org/mailman/listinfo/pcl-users

_______________________________________________
[hidden email] / http://pointclouds.org
http://pointclouds.org/mailman/listinfo/pcl-users
Reply | Threaded
Open this post in threaded view
|

Re: Get OpenCV image from PCL Visualizer

ravijoshi
yes. you are right. thanks a lot!



--
Sent from: http://www.pcl-users.org/
_______________________________________________
[hidden email] / http://pointclouds.org
http://pointclouds.org/mailman/listinfo/pcl-users