Created
May 15, 2013 18:43
-
-
Save Daiver/5586252 to your computer and use it in GitHub Desktop.
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
pcl::PointCloud<pcl::PointXYZRGB>::Ptr getColored(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud) | |
{ | |
pcl::search::Search<pcl::PointXYZRGB>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZRGB> > (new pcl::search::KdTree<pcl::PointXYZRGB>); | |
pcl::PointCloud <pcl::Normal>::Ptr normals (new pcl::PointCloud <pcl::Normal>); | |
pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> normal_estimator; | |
normal_estimator.setSearchMethod (tree); | |
normal_estimator.setInputCloud (cloud); | |
normal_estimator.setKSearch (50); | |
normal_estimator.compute (*normals); | |
pcl::RegionGrowing<pcl::PointXYZRGB, pcl::Normal> reg; | |
reg.setMinClusterSize (100); | |
reg.setMaxClusterSize (20000); | |
reg.setSearchMethod (tree); | |
reg.setNumberOfNeighbours (30); | |
reg.setInputCloud (cloud); | |
//reg.setIndices (indices); | |
reg.setInputNormals (normals); | |
reg.setSmoothnessThreshold (10.0 / 180.0 * M_PI); | |
reg.setCurvatureThreshold (1.0); | |
std::vector <pcl::PointIndices> clusters, tmp_clusters; | |
reg.extract (tmp_clusters); | |
for(int i = 0; i < tmp_clusters.size(); i++) | |
{ | |
if(tmp_clusters[i].indices.size() > 0) | |
clusters.push_back(tmp_clusters[i]); | |
} | |
std::cout << "Number of clusters is equal to " << clusters.size () << std::endl; | |
std::cout << "First cluster has " << clusters[0].indices.size () << " points." << endl; | |
std::cout << "These are the indices of the points of the initial" << | |
std::endl << "cloud that belong to the first cluster:" << std::endl; | |
for(int j = 0; j < clusters.size(); j++) | |
{ | |
cv::Mat to_show = cv::Mat::zeros(288, 384, cv::DataType<uchar>::type); | |
for(int i = 0; i < clusters[j].indices.size(); i++) | |
{ | |
to_show.data[clusters[j].indices[i]] = 200; | |
} | |
cv::imshow("", to_show); | |
cv::waitKey(); | |
} | |
return reg.getColoredCloud(); | |
} | |
void reprojectCloud(const cv::Mat& Q, cv::Mat& img_rgb, cv::Mat& img_disparity, pcl::PointCloud<pcl::PointXYZRGB>::Ptr& point_cloud_ptr) | |
{ | |
#ifdef CUSTOM_REPROJECT | |
//Get the interesting parameters from Q | |
double Q03, Q13, Q23, Q32, Q33; | |
Q03 = Q.at<double>(0,3); | |
Q13 = Q.at<double>(1,3); | |
Q23 = Q.at<double>(2,3); | |
Q32 = Q.at<double>(3,2); | |
Q33 = Q.at<double>(3,3); | |
std::cout << "Q(0,3) = "<< Q03 <<"; Q(1,3) = "<< Q13 <<"; Q(2,3) = "<< Q23 <<"; Q(3,2) = "<< Q32 <<"; Q(3,3) = "<< Q33 <<";" << std::endl; | |
#endif | |
#ifndef CUSTOM_REPROJECT | |
//Create matrix that will contain 3D corrdinates of each pixel | |
cv::Mat recons3D(img_disparity.size(), CV_32FC3); | |
point_cloud_ptr->width = img_rgb.cols; | |
point_cloud_ptr->height = img_rgb.rows; | |
//Reproject image to 3D | |
std::cout << "Reprojecting image to 3D..." << std::endl; | |
cv::reprojectImageTo3D( img_disparity, recons3D, Q, false, CV_32F ); | |
#endif | |
double px, py, pz; | |
uchar pr, pg, pb; | |
for (int i = 0; i < img_rgb.rows; i++) | |
{ | |
uchar* rgb_ptr = img_rgb.ptr<uchar>(i); | |
#ifdef CUSTOM_REPROJECT | |
uchar* disp_ptr = img_disparity.ptr<uchar>(i); | |
#else | |
double* recons_ptr = recons3D.ptr<double>(i); | |
#endif | |
for (int j = 0; j < img_rgb.cols; j++) | |
{ | |
//Get 3D coordinates | |
#ifdef CUSTOM_REPROJECT | |
uchar d = disp_ptr[j]; | |
if ( d == 0 ) continue; //Discard bad pixels | |
double pw = -1.0 * static_cast<double>(d) * Q32 + Q33; | |
px = static_cast<double>(j) + Q03; | |
py = static_cast<double>(i) + Q13; | |
pz = Q23; | |
px = px/pw; | |
py = py/pw; | |
pz = pz/pw; | |
#else | |
px = recons_ptr[3*j]; | |
py = recons_ptr[3*j+1]; | |
pz = recons_ptr[3*j+2]; | |
#endif | |
//Get RGB info | |
pb = rgb_ptr[3*j]; | |
pg = rgb_ptr[3*j+1]; | |
pr = rgb_ptr[3*j+2]; | |
//Insert info into point cloud structure | |
pcl::PointXYZRGB point; | |
point.x = px; | |
point.y = py; | |
point.z = pz; | |
uint32_t rgb = (static_cast<uint32_t>(pr) << 16 | | |
static_cast<uint32_t>(pg) << 8 | static_cast<uint32_t>(pb)); | |
point.rgb = *reinterpret_cast<float*>(&rgb); | |
point_cloud_ptr->push_back (point); | |
} | |
} | |
//point_cloud_ptr->width = (int) point_cloud_ptr->points.size(); | |
//point_cloud_ptr->height = 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment