Posts Tagged ‘PCL’

This is an example showing integration of OpenCV and PCL(Point Cloud Library) in using trackbars of OpenCV(highgui) to adjust the x,y,z limits of passthrough filter of PCL .

It is assumed that you have already installed OpenCV and PCL . Make sure that you have OpenCV version> 2.3 & PCL version > 1.1.  In my case I am using OpenCV 2.3.1 and PCL 1.6 ( Compiled from current trunk).

Before downloading the code and running, you might want to see how the output looks like.

Running the code:

This code takes a PLY file as input and applies passthrough filters to it and visualizes it. You can adjust the X,Y,Z  limits of passthrough filters using trackbars and see the output on the fly.

Step1: Download the code folder and extract it.

Step2: Create a folder named “build” inside it. Open CMake and in the field where is the source code provide the path of folder. eg: C:/Users/Sanmarino/Downloads/Integrating_Opencv_PCL_PassthroughFilters. In the field where to build the binaries provide the path to the empty build folder you created. eg:C:/Users/Sanmarino/Downloads/Integrating_Opencv_PCL_PassthroughFilters/build. Then Click configure at the bottom. Choose the compiler you want to use when prompted. In my case I chose Visual Studio 10


Step3: If everything is fine after configure , click generate. Else sometimes you might get an error like this –

“CMake Error at CMakeLists.txt:6 (find_package): By not providing “FindOpenCV.cmake” in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by “OpenCV”, but CMake did not find one.

Could not find a package configuration file provided by “OpenCV” with any of the following names: OpenCVConfig.cmake opencv-config.cmake

Add the installation prefix of “OpenCV” to CMAKE_PREFIX_PATH or set “OpenCV_DIR” to a directory containing one of the above files. If “OpenCV” provides a separate development package or SDK, be sure it has been installed.

If this is the case then we have to set the OpenCV_DIR to the place where it can find OpenCVConfig.cmake. So  click on OpenCV_DIR-NOTFOUND and change it to the path where OpenCVConfig.cmake exists. In my case it was  C:\OpenCV\build  where I had built the binaries of OpenCV. Now hit Configure again and hit Generate.

Changed path of OpenCV_DIR:

Step4: Now open the generated solution and build it in release/debug mode.  Place scene_mesh.ply from the unzipped code folder in the folder where simple_visualizer.exe is generated. Run simple_visualizer.exe in command prompt by giving scene_mesh.ply as argument.

eg: C:\Users\Sanmarino\Downloads\Integrating_Opencv_PCL_PassthroughFilters\build\Release>    simple_visualizer.exe  scene_mesh.ply. Change the trackbars to apply passThrough filter on the pointcloud and see the output.


#include <iostream>

// Point cloud library
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>
#include <pcl/visualization/pcl_visualizer.h>

// Opencv
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

// These are track bar initial settings adjusted to the given pointcloud to make it completely visible.
//  Need to be adjusted depending on the pointcloud and its xyz limits if used with new pointclouds.

int a = 22;
int b = 12;
int c=  10;

// PCL Visualizer to view the pointcloud
pcl::visualization::PCLVisualizer viewer ("Simple visualizing window");

	main (int argc, char** argv)
	pcl::PointCloud::Ptr cloud (new pcl::PointCloud);
	pcl::PointCloud::Ptr cloud_filtered (new pcl::PointCloud);

	if (pcl::io::loadPLYFile (argv[1], *cloud) == -1) //* load the ply file from command line
		PCL_ERROR ("Couldn't load the file\n");
		return (-1);

	pcl::copyPointCloud( *cloud,*cloud_filtered);

	float i ;
	float j;
	float k;

	cv::namedWindow( "picture");

	// Creating trackbars uisng opencv to control the pcl filter limits
	cvCreateTrackbar("X_limit", "picture", &a, 30, NULL);
	cvCreateTrackbar("Y_limit", "picture", &b, 30, NULL);
	cvCreateTrackbar("Z_limit", "picture", &c, 30, NULL);

	// Starting the while loop where we continually filter with limits using trackbars and display pointcloud
	char last_c = 0;
	while(true && (last_c != 27))

		pcl::copyPointCloud(*cloud_filtered, *cloud);

		// i,j,k Need to be adjusted depending on the pointcloud and its xyz limits if used with new pointclouds.

		i = 0.1*((float)a);
		j = 0.1*((float)b);
		k = 0.1*((float)c);

		// Printing to ensure that the passthrough filter values are changing if we move trackbars.

		cout << "i = " << i << " j = " << j << " k = " << k << endl;

		// Applying passthrough filters with XYZ limits

		pcl::PassThrough pass;
		pass.setInputCloud (cloud);
		pass.setFilterFieldName ("y");
		//  pass.setFilterLimits (-0.1, 0.1);
		pass.setFilterLimits (-k, k);
		pass.filter (*cloud);

		pass.setInputCloud (cloud);
		pass.setFilterFieldName ("x");
		// pass.setFilterLimits (-0.1, 0.1);
		pass.setFilterLimits (-j, j);
		pass.filter (*cloud);

		pass.setInputCloud (cloud);
		pass.setFilterFieldName ("z");
		//  pass.setFilterLimits (-10, 10);
		pass.setFilterLimits (-i, i);
		pass.filter (*cloud);

		// Visualizing pointcloud
		viewer.addPointCloud (cloud, "scene_cloud");

	return (0);


cmake_minimum_required(VERSION 2.8 FATAL_ERROR)


find_package(PCL 1.4 REQUIRED)
find_package(OpenCV REQUIRED)

include_directories(${PCL_INCLUDE_DIRS} )
link_directories(${PCL_LIBRARY_DIRS} )
add_definitions(${PCL_DEFINITIONS} )

add_executable (simple_visualizer simple_visualizer.cpp)
target_link_libraries (simple_visualizer ${PCL_LIBRARIES} ${OpenCV_LIBS})