0

I am trying to create a point cloud using a stereo camera arrangement (calibrated and rectified), a disparity map, and Point Cloud Library. Below is a brief description of my C++ code that is supposed to generate a point cloud using the disparity map.

#include <iostream>
#include <fstream>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/ximgproc.hpp>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <boost/thread/thread.hpp>
#include <pcl/visualization/vtk.h>

using namespace std;
using namespace cv;
using namespace cv::ximgproc;
using namespace pcl;
using namespace pcl::visualization;

boost::shared_ptr<PCLVisualizer> rgbVis (PointCloud<PointXYZRGB>::ConstPtr cloud)
{
  boost::shared_ptr<PCLVisualizer> viewer (new PCLVisualizer ("3D Viewer"));
  viewer->setBackgroundColor (0, 0, 0);
  PointCloudColorHandlerRGBField<PointXYZRGB> rgb(cloud);
  viewer->addPointCloud<PointXYZRGB> (cloud, rgb, "sample cloud");
  viewer->setPointCloudRenderingProperties (PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
  return (viewer);
}

/* Some other functions */

int main()
{
    .....
    .....
    int l, r;
    cout << "Enter CAM index for Left Camera ";
    cin >> l;
    cout << endl << "Enter CAM index for Right Camera ";
    cin >> r;
    VideoCapture leftCam(l);
    VideoCapture rightCam(r);
    ........
    ........
    boost::shared_ptr<PCLVisualizer> viewer;

    bool proceed = true;
    
    while(proceed)
    {
        ........
        ........
        PointCloud<PointXYZRGB>::Ptr pointCloud(new PointCloud<PointXYZRGB>());
        Mat xyz;
        reprojectImageTo3D(disparityMap, xyz, q);
        pointCloud->width = static_cast<uint32_t>(disparityMap.cols);
        pointCloud->height = static_cast<uint32_t>(disparityMap.rows);
        pointCloud->is_dense = false;
        PointXYZRGB tempPoint;
        for(int i = 0; i < disparityMap.rows; i++)
        {
            uchar* rgb_ptr = rightUndistorted.ptr<uchar>(i);
            uchar* disp_ptr = disparityMap.ptr<uchar>(i);
            double* xyz_ptr = xyz.ptr<double>(i);

            for(int j = 0; j < disparityMap.cols; j++)
            {
                uchar d = disp_ptr[j];
                if(d == 0)
                    continue;
                Point3f p = xyz.at<Point3f>(i, j);

                tempPoint.x = p.x;
                tempPoint.y = p.y;
                tempPoint.z = p.z;

                tempPoint.b = rgb_ptr[3 * j];
                tempPoint.g = rgb_ptr[3 * j + 1];
                tempPoint.r = rgb_ptr[3 * j + 2];
                pointCloud->points.push_back(tempPoint);
            }
        }

        viewer = rgbVis(pointCloud);
        if(waitKey(50) == 'q')
            proceed = false;
    }

    destroyAllWindows();
    return 0;
}

I run the following terminal command to compile this .cpp file...

g++ -std=c++14 d2pc.cpp -o d2pc `pkg-config --cflags --libs opencv pcl_io-1.11 pcl_visualization-1.11` -lboost_system

This generates the following error message...

/usr/local/include/pcl-1.11/pcl/visualization/point_cloud_geometry_handlers.h:48:10: fatal error: vtkSmartPointer.h: No such file or directory
 #include <vtkSmartPointer.h>
          ^~~~~~~~~~~~~~~~~~~
compilation terminated.

I thought that installing Vtk from here might help solve the issue, but it didn't help.

How to tackle this issue? I am using OpenCV 3.4.10 in Ubuntu 18.04

  • Did you add the include path to the vtk header directory? – Ted Lyngmo Jul 24 '20 at 18:22
  • Do you mean `#include ` ? I tried, but it didn't help. – Harshit Kaushik Jul 25 '20 at 07:56
  • No, I mean adding the path to the compiler's list of paths to include in the search for header files, but I now see that I missed that you did `pkg-config --cflags --libs opencv pcl_io-1.11 pcl_visualization-1.11`. Does `find $(pkg-config --cflags --libs opencv pcl_io-1.11 pcl_visualization-1.11) -name vtkSmartPointer.h` find the file? – Ted Lyngmo Jul 25 '20 at 08:29
  • It didn't help @TedLyngmo. – Harshit Kaushik Jul 25 '20 at 21:08
  • No, but does the `find` command show the file? I'm guessing it doesn't so you need to add `-I path_to_vtks_include_dir` – Ted Lyngmo Jul 25 '20 at 21:27
  • The output of `find $(pkg-config --cflags --libs opencv pcl_io-1.11 pcl_visualization-1.11) -name vtkSmartPointer.h` is `find: unknown predicate '-I/usr/local/include/opencv'` – Harshit Kaushik Jul 26 '20 at 08:08
  • Aha, ok. My mistake. Skip that. When compiling, you need ot add `-I path_to_vtks_include_dir` – Ted Lyngmo Jul 26 '20 at 08:44
  • Add the commands you used to build and install `vtk` to the question. – Ted Lyngmo Jul 26 '20 at 08:55
  • I have made some changes to the installed libraries. Instead of using PCL 1.11 (from github) and VTK 9.0 (from [here](https://vtk.org/download/) ), I have shifted to using the libraries from ubuntu repsitory. `sudo apt install libpcl-dev` It includes PCL 1.8 and Vtk 6.3. – Harshit Kaushik Jul 26 '20 at 09:16
  • The output of `g++ -std=c++14 d2pc.cpp -o d2pc `pkg-config --cflags --libs opencv pcl_io-1.8 pcl_visualization-1.8` -lboost_system -I/usr/include/vtk-6.3` is as follows... /usr/bin/ld: /tmp/ccdrJEUv.o: undefined reference to symbol '_ZN9vtkProp3D13SetUserMatrixEP12vtkMatrix4x4' //usr/lib/x86_64-linux-gnu/libvtkRenderingCore-6.3.so.6.3: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status – Harshit Kaushik Jul 26 '20 at 09:16
  • You probably need to link with some vtk library too: [DSO missing from command line](https://stackoverflow.com/q/19901934/7582247). I'd stay with the newer versions though. VTK 6 seems to be rather old. – Ted Lyngmo Jul 26 '20 at 09:21

1 Answers1

0

First build and install vtk:

Build

  • tar xf VTK-9.0.1.tar.gz
  • cd VTK-9.0.1
  • mkdir build && cd build
  • cmake ..
  • make -j

Install

(if you have a different preferred place for installations than ~/.local, just change insdir below)

  • insdir=$(echo ~/.local)
  • mkdir $insdir
  • cmake --install . --prefix $insdir

Then add the include path (-I $insdir/include/vtk-9.0), the library path (-L $insdir/lib64 or possibly $insdir/lib on a 32 bit machine) and vtk libraries (-llibname1 ... -llibnameX) to your compilation command.

Example:

  • allvtklibs=$(ls $insdir/lib64/libvtk*.so | sed -E 's,^.*/lib(.*)\.so$,-l\1,')
  • g++ -std=c++14 d2pc.cpp -o d2pc -I $insdir/include/vtk-9.0 -L $insdir/lib64 $(pkg-config --cflags --libs opencv pcl_io-1.11 pcl_visualization-1.11) -Wl,-rpath=$insdir/lib64 $allvtklibs -lboost_system
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108