Title

Saturday, 7 February 2015

3D reconstruction with opencv and point cloud library


I am working on a project which involve 3d reconstruction and rendering of the 3D scene , I have done up to disparity image and generated the 3D coordinates using opencv 2.1 and after that I am trying to render the 3D scene using point cloud library here I am gtting a crash in the code . I am using windows 7 32 bit.

I am not getting why this error is comming , please help me.

Here is my code

 ......       Mat imgDisparity16S ;  IplImage stub, *dst_img;     Mat imgDisparity8U ;    int ndisparities = 16*5;  int SADWindowSize = 5;    StereoBM sbm(StereoBM::BASIC_PRESET,ndisparities,SADWindowSize);    sbm( object, image, imgDisparity16S, CV_16S );    double minVal;  double maxVal;    minMaxLoc( imgDisparity16S, &minVal, &maxVal );    printf("Min disp: %f Max value: %f \n", minVal, maxVal);    imgDisparity16S.convertTo( imgDisparity8U, CV_8UC1, 255/(maxVal - minVal));          IplImage ipl_from_mat((IplImage)imgDisparity8U);  cvNamedWindow("window", CV_WINDOW_AUTOSIZE);  cvShowImage("window", &ipl_from_mat);    cvSaveImage("disparity.jpg",&ipl_from_mat);  cvWaitKey(0);    Mat xyz ;  cout<<"Here"<<endl;  cv::Mat img_disparity = cv::imread("disparity.jpg", CV_LOAD_IMAGE_GRAYSCALE);    double q[] =   {     1, 0, 0, -2.9615028381347656e+02,   0, 1, 0, -2.3373317337036133e+02,   0, 0, 0, 5.6446880931501073e+02,   0, 0, -1.1340974198400260e-01, 4.1658568844268817e+00,     };   cv::Mat Q = Mat(4, 4, CV_64F, q);      cv::Mat recons3D(img_disparity.size(), CV_32FC3);     cv::reprojectImageTo3D(imgDisparity8U,recons3D,Q,true);  cout<<"Here1"<<endl;  cout<<"Here2"<<endl;    saveXYZ("clouds.txt", recons3D);           std::cout<<"Creating point cloud"<<std::endl;    pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);    double px, py, pz;  uchar pr, pg, pb;  double Q03, Q13, Q23, Q32, Q33;    uint32_t rgb1;    #ifdef CUSTOM_REPROJECT   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);  #endif  cout<<"Before"<<endl;    cv::Mat img_rgb = Mat(imgDisparity8U.size(),CV_32FC3);    CvMat* img_rgb1 = cvLoadImageM("frame1_0.jpg",1);    cout<<object_filename<<endl;  img_rgb = img_rgb1;  for(int i=0;i<img_rgb.rows ;i++)   {     uchar* rgb_ptr = img_rgb.ptr<uchar>(i);             uchar* disp_ptr = imgDisparity8U.ptr<uchar>(i);   double* recons_ptr = recons3D.ptr<double>(i);     for(int j = 0; j < img_rgb.cols; j++)   {   uchar d = disp_ptr[j];   if ( d == 0 ) continue;   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;   px = recons_ptr[3*j];   py = recons_ptr[3*j+1];   pz = recons_ptr[3*j+2];     pb = rgb_ptr[3*j];   pg = rgb_ptr[3*j+1];   pr = rgb_ptr[3*j+2];     pcl::PointXYZRGB point;   point.x = px;   point.y = py;   point.z = pz;         rgb1 = (static_cast<uint32_t>(pr) << 16 || static_cast<uint32_t>(pg) << 8 || static_cast<uint32_t>(pb));   point.rgb = *reinterpret_cast<float*>(&rgb1);   point_cloud_ptr->points.push_back(point);     }    }      point_cloud_ptr->width = (int)point_cloud_ptr->points.size();  point_cloud_ptr->height = 1;  cout<<"outside loop"<<endl;  boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;  cout<<"outside loop11"<<endl;    viewer = createVisualizer(point_cloud_ptr); //Error Here    while(!viewer->wasStopped())  {   viewer->spinOnce(100);   boost::this_thread::sleep (boost::posix_time::microseconds (100000));  }    return 0;  
Answer

It´s very complicated find the error in your code.

But I can suggest you documentation about 3D reconstruction in PCL

A very common project about that, is KinFu and you can download it from PCL here

And here there is other project, he shows his project of 3D reconstruction

Finally, in the documentation of PCL, there are samples of surface reconstruction here

I hope these links can help you

Happy code !

No comments:

Post a Comment