Integrating PCL and OpenCV – PassThrough Filter Example

Posted: June 28, 2012 in PCL and OpenCV, Technology
Tags: , , , ,

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.

simple_visualizer.cpp


#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");

int
	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");
		viewer.spinOnce();
		viewer.removePointCloud("scene_cloud");
	}

	return (0);
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

project(visualizer_grouping)

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})
Advertisements
Comments
  1. Hi Ramsri,
    i recently installed OpenCV following this tutorial (http://stackoverflow.com/questions/10901905/installing-opencv-2-4-in-visual-c-2010-express). Is there a similar way to use PCL with VS 2010 without resorting to CMake?

    Regards,
    Adriano

  2. Glad to know that you got it working.

    Cheers,
    Ramsri

  3. Ana Perez says:

    Hi Ramsri,

    I’m working on a VS10 project, I’ve been using OpenCV and need to incorporate PCL. I have other .cpp and .h files inside my project, do you know how I could handle this in CMake in order to be able to use PCL?

    Regards,

    Ana

    • Hi Ana,

      I believe you just need to modify the line in cmakelists.txt shown above like this –

      add_executable(simple_visualizer simple_visualizer.cpp
      newfile1.hpp
      newfile2.cpp
      )

      Hope that helps!

      Cheers,
      Ramsri

      • Ana Perez says:

        Hi Ramsri,

        Thanks for replying.
        I’m trying your code first but when I’m running it I get an Assertion error saying: “Vector subscipt out of range”.

        I can see a black window and the trackbars but then I get the error mentioned above and I’m not able to see the results. I’m quite new using these libraries so can’t really see what I’m doing wrong 😦

        Ana

  4. Ana Perez says:

    I was debugging and the error shows up after executing line 89 but I don’t understand why…

    • Ana Perez says:

      I kind of solved the problem. I was using a different OpenCV version. When I changed to opencv 2.3.1 I could run your code. However I didn’t see any changes in the cloud when using the trackbars. Your work has been really helpful though. I will try to incorporate PLC to my project following your steps.

      Thanks 🙂

  5. Ana Perez says:

    Thanks Ramsri

    I’ll try it.

    Best,

    Ana

  6. johannes says:

    hi Ramsri

    i have the same problem as Ana . i get the same massage : “Vector subscipt out of range”.
    i am using opencv version 2.5.1 .
    could you provide some solutions …?

    thanks

  7. Anonymous says:

    needed to put cvWaitKey(10); in the loop for track-bars to display..

  8. rajnish says:

    HI ramsri
    i have small doubt that this CMakeLists.txt works for Linux . i mean to ask weather its a crossplatform

  9. Arun says:

    Hi ramsri,
    I can see the output, but not the trackbar window. I extracted the folder and ran “cmake .” and “make” in the same folder without creating the build folder. It compiled and ran properly. How do i make the trackbar window appear?

  10. Miriam says:

    Hi Ramsri,
    I do the same thing but i don’t have the file simple_visualisation.exe…Please help

  11. cforge says:

    Hi,
    your code found on my nvidia jetson tk1, but i don’t know how to make ply file. Did you say me how to do or where to search?

    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s