Last active
May 15, 2017 11:16
-
-
Save r9y9/3ed139a503c58751c68e to your computer and use it in GitHub Desktop.
[PCLVisualizer] Render to png, and then convert it to cv::Mat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <pcl/io/pcd_io.h> | |
#include <pcl/visualization/pcl_visualizer.h> | |
#include <vtkActor.h> | |
#include <vtkPNGWriter.h> | |
#include <vtkPolyData.h> | |
#include <vtkPolyDataMapper.h> | |
#include <vtkRenderWindow.h> | |
#include <vtkRenderer.h> | |
#include <vtkSmartPointer.h> | |
#include <vtkWindowToImageFilter.h> | |
#include <opencv2/opencv.hpp> | |
std::vector<unsigned char> | |
renderToVec(vtkSmartPointer<vtkRenderWindow> &renderWindow) { | |
renderWindow->Render(); | |
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = | |
vtkSmartPointer<vtkWindowToImageFilter>::New(); | |
windowToImageFilter->SetInput(renderWindow); | |
windowToImageFilter->Update(); | |
vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New(); | |
writer->SetWriteToMemory(1); | |
writer->SetInputConnection(windowToImageFilter->GetOutputPort()); | |
writer->Write(); | |
auto rawPngBuffer = writer->GetResult(); | |
auto rawPointer = rawPngBuffer->GetPointer(0); | |
auto total_size = | |
rawPngBuffer->GetDataSize() * rawPngBuffer->GetDataTypeSize(); | |
std::vector<unsigned char> buffer(rawPointer, rawPointer + total_size); | |
return buffer; | |
} | |
int main() { | |
auto cloud = | |
pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>); | |
assert(pcl::io::loadPCDFile("table_scene_lms400.pcd", *cloud) == 0); | |
auto viewer = pcl::visualization::PCLVisualizer::Ptr( | |
new pcl::visualization::PCLVisualizer("Test", false)); | |
viewer->addPointCloud(cloud, "test"); | |
auto renderWindow = viewer->getRenderWindow(); | |
auto buf = renderToVec(renderWindow); | |
cv::Mat mat = cv::imdecode(buf, 1); | |
std::cout << buf.size() << std::endl; | |
std::cout << mat.rows << " " << mat.cols << std::endl; | |
// comment out if unneccesarry | |
cv::imshow("Test with cv::Mat", mat); | |
cv::waitKey(0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: