Not able to create 3D point cloud
I want to convert 2D images to a 3D point cloud data, I have installed OpenCV and PCL libraries in my system. From a post in this website I found some codes to generate point cloud data.
But with that code I am getting errors
1st one is The type 'pcl::visualization::PointCloudColorHandlerRGBField' must implement the inherited pure virtual method 'pcl::visualization::PointCloudColorHandler::getColor'
2nd one bit typical Field 'SADWindowSize' could not be resolvedstrong text** Well I used both StereoBM and StereoSGBM but no luck. Please help me out.
The code:
using namespace cv;
using namespace std;
using namespace pcl;
ofstream out("points.txt");
boost::shared_ptr<pcl::visualization::PCLVisualizer> createVisualizer (pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "reconstruction");
//viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "reconstruction");
viewer->addCoordinateSystem ( 1.0 );
viewer->initCameraParameters ();
return (viewer);
}
int main()
{
Mat img1, img2;
img1 = imread("I1.png");
img2 = imread("I2.png");
Mat g1,g2, disp, disp8;
cvtColor(img1, g1, CV_BGR2GRAY);
cvtColor(img2, g2, CV_BGR2GRAY);
int sadSize = 3;
StereoSGBM *sbm;
sbm->SADWindowSize = sadSize;
sbm->setNumDisparities(144);//144; 128
sbm->setPreFilterCap(10); //63
sbm->setMinDisparity(0);//-39; 0
sbm->setUniquenessRatio(10);
sbm->setSpeckleWindowSize(100);
sbm->setSpeckleRange(32);
sbm->setDisp12MaxDiff(1);
sbm.fullDP = true;
sbm.P1 = sadSize*sadSize*4;
sbm.P2 = sadSize*sadSize*32;
sbm(g1, g2, disp);
normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U);
Mat dispSGBMscale;
disp.convertTo(dispSGBMscale,CV_32F, 1./16);
double cm1[3][3] = {{8.941981e+02, 0.000000e+00, 6.601406e+02}, {0.000000e+00, 8.927151e+02, 2.611004e+02}, {0.000000e+00, 0.000000e+00, 1.000000e+00}};
double cm2[3][3] = {{8.800704e+02, 0.000000e+00, 6.635881e+02 }, {0.000000e+00, 8.798504e+02, 2.690108e+02}, {0.000000e+00, 0.000000e+00, 1.000000e+00}};
double d1[1][5] = {{ -3.695739e-01, 1.726456e-01, -1.281525e-03, 1.188796e-03, -4.284730e-02}};
double d2[1][5] = {{-3.753454e-01, 1.843265e-01, -1.307069e-03, 2.190397e-03, -4.989103e-02}};
Mat CM1 (3,3, CV_64FC1, cm1);
Mat CM2 (3,3, CV_64FC1, cm2);
Mat D1(1,5, CV_64FC1, d1);
Mat D2(1,5, CV_64FC1, d2);
double r[3][3] = {{9.998381e-01, 1.610234e-02, 8.033237e-03},{-1.588968e-02, 9.995390e-01, -2.586908e-02 },{-8.446087e-03, 2.573724e-02, 9.996331e-01}};
double t[3][4] = {{ -5.706425e-01}, {8.447320e-03}, {1.235975e-02}};
Mat R (3,3, CV_64FC1, r);
Mat T (3,1, CV_64FC1, t);
//Mat R, T;
Mat R1, R2, T1, T2, Q, P1, P2;
stereoRectify(CM1, D1, CM2, D2, img1.size(), R, T, R1, R2, P1, P2, Q);
Mat points, points1;
reprojectImageTo3D(disp8, points, Q, true);
imshow("points", points);
cvtColor(points, points1, CV_BGR2GRAY);
imshow("points1", points1);
imwrite("disparity.jpg", disp8);
imwrite("points1.jpg", points1);
imwrite("points.jpg", points);
for(int i=0; i<points.rows; ++i)
{
Point3f* point = points.ptr<Point3f>(i) ;
for(int j=0; j<points.cols; ++j)
{
out<<i<<" "<<j<<" x: "<<(*point).x<<" y: "<<(*point).y<<" z ...
we might not be able , to help you with the pcl error, but which opencv version are you using ?
What are you doing exactly ? 'pcl::visualization::PointCloudColorHandlerRGBField' it is not an opencv method ?
The issues tab of the PCL library might be a better location to address this.
Im on 3.0.0
For my project I need to work on 3D reconstruction. I need to create 3D point cloud data in pcl format so i can work on PCL library......
please insert code in your question using edit button
I am sorry but again, solving PCL issues is NOT the scope of this forum ...
have a look here and here