opencv +openni2+pcl在ubuntu14.04下的安装

一、  Opencv

这个网上有很多教程,

1. 安装依赖包和预备环境

  1. $sudo apt-get install build-essential  
  2. $sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev  
  3. $sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev  

2.从官网下载opencv3.1源码,并解压



3.创建编译目录,编译

  1. $cd ~/opencv-3.1.0  
  2. $mkdir release  
  3. $cd release  
  4. $cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..  
  5. $make  
  6. $sudo make install  

4.测试opencv
  1. $mkdir ~/opencv-lena  
  2. $cd ~/opencv-lena  
  3. $gedit DisplayImage.cpp  

  1. #include <stdio.h>  
  2. #include <opencv2/opencv.hpp>  
  3. using namespace cv;  
  4. int main(int argc, char** argv )  
  5. {  
  6.   if ( argc != 2 )  
  7.   {  
  8.     printf("usage: DisplayImage.out <Image_Path>\n");  
  9.     return -1;  
  10.   }  
  11.   Mat image;  
  12.   image = imread( argv[1], 1 );  
  13.   if ( !image.data )  
  14.   {  
  15.     printf("No image data \n");  
  16.     return -1;  
  17.   }  
  18.   namedWindow("Display Image", WINDOW_AUTOSIZE );  
  19.   imshow("Display Image", image);  
  20.   waitKey(0);  
  21.   return 0;  
  22. }  

创建cmakelist编译文件

  1. cmake_minimum_required(VERSION 2.8)  
  2. project( DisplayImage )  
  3. find_package( OpenCV REQUIRED )  
  4. add_executable( DisplayImage DisplayImage.cpp )  
  5. target_link_libraries( DisplayImage ${OpenCV_LIBS} )  

执行:
  1. $cd ~/opencv-lena  
  2. $cmake .  
  3. $make  

  1. $./DisplayImage lena.jpg  


至此opencv配置完毕

 

二、openni2.0

以下内容是我在youtube上看到的视频:

https://www.youtube.com/watch?v=Bn9WqbYtNBw

 

1.安装 OpenNI2依赖项
  1. $sudo apt-get install -y g++ python libusb-1.0-0-dev freeglut3-dev doxygen graphviz  
  2. $sudo apt-get install libudev-dev  

2.从github将openni2源码clone下来
  1. $git clone https://github.com/occipital/OpenNI2.git  
  2. $cd OpenNI2  

3.修改两处配置Platform.Arm和CommonCppMakefile,适用于arm设备
  1. $gedit ThirdParty/PSCommon/BuildSystem/Platform.Arm  

Change:

CFLAGS+= -march=armv7-a -mtune=cortex-a9 -mfpu=neon -mfloat-abi=softfp#-mcpu=cortex-a8

to:

CFLAGS+= -march=armv7-a -mtune=cortex-a15 -mfpu=neon-vfpv4 -mfloat-abi=hard


  1. $gedit ThirdParty/PSCommon/BuildSystem/CommonCppMakefile  

---OpenNI2-2.2.0.30/ThirdParty/PSCommon/BuildSystem/CommonCppMakefile.old2014-03-28 19:09:11.572263107 -0700

+++OpenNI2-2.2.0.30/ThirdParty/PSCommon/BuildSystem/CommonCppMakefile 2014-03-2819:09:55.600261937 -0700

@@-95,6 +95,9 @@

OUTPUT_NAME= $(EXE_NAME)

# Wewant the executables to look for the .so's locally first:

LDFLAGS+= -Wl,-rpath ./

+ifneq ("$(OSTYPE)","Darwin")

+LDFLAGS += -lpthread

+endif

OUTPUT_COMMAND= $(CXX) -o $(OUTPUT_FILE) $(OBJ_FILES) $(LDFLAGS)

endif

ifneq "$(SLIB_NAME)" ""



4.修改makefile,增加sample,即在makefile文件末尾添加:

  1. core_samples: $(CORE_SAMPLES)  
  2. tools: $(ALL_TOOLS)  


5.编译

  1. $make  
  2. $make core_samples # this probably isn't necessary, they should already be built  
  3. $GLUT_SUPPORTED=1 make tools#GLUT_SUPPORTED tells the make to compile NiViewer for OpenGL  

6.安装libfreenect,否则无法驱动primesensor

安装依赖项

  1. $sudo apt-get install cmake freeglut3-dev pkg-config build-essential libxmu-dev libxi-dev libusb-1.0-0-dev –y  

下载源码并编译

  1. $ git clone git://github.com/OpenKinect/libfreenect.git  
  2. $ cd libfreenect  
  3. $ mkdir build  
  4. $ cd build  
  5. $ cmake ..  
  6. $ make  
  7. $ sudo make install  
  8. # Build the OpenNI2 driver  
  9. $ cmake .. -DBUILD_OPENNI2_DRIVER=ON  
  10. $ make  

将libfreenect的so拷贝到OpenNI的驱动文件夹下

  1. $ Repository=../../Bin/Arm-Release/OpenNI2/Drivers  
  2. $ cp -L lib/OpenNI2-FreenectDriver/libFreenectDriver* ${Repository}  

设置使用传感器的权限

  1. $ sudo usermod -a -G video Ubuntu  

7. 回到 openni2 目录,将头文件和 so 文件拷贝到 /usr 目录下

  1. $ sudo cp -r Include /usr/include/openni2  
  2. $ sudo cp -r Bin/Arm-Release/OpenNI2 /usr/lib/  
  3. $ sudo cp Bin/Arm-Release/libOpenNI2.* /usr/lib/  

8. 创建 packageconfig 文件

  1. # this will enable ubuntu to find the location of the drivers, libraries and include files.  
  2. $ sudo gedit /usr/lib/pkgconfig/libopenni2.pc  
  3.   
  4. and fill it with this:  
  5.   
  6. prefix=/usr  
  7. exec_prefix=${prefix}  
  8. libdir=${exec_prefix}/lib  
  9. includedir=${prefix}/include/openni2  
  10.   
  11. Name: OpenNI2  
  12. Description: A general purpose driver for all OpenNI cameras.  
  13. Version: 2.2.0.0  
  14. Cflags: -I${includedir}  
  15. Libs: -L${libdir} -lOpenNI2 -L${libdir}/OpenNI2/Drivers -lDummyDevice -lOniFile -lPS1080.so  

检验:

  1. # To make sure it is correctly found, run  
  2. $ pkg-config --modversion libopenni2  
  3.   
  4. # which should give the same version as defined in the file above (2.2.0.0)  

最后一步:

  1. # Linux has used the udev system to handle devices such as USB connected peripherals.  
  2.   
  3. $ cd Packaging/Linux  
  4. $ sudo cp primesense-usb.rules /etc/udev/rules.d/557-primesense-usb.rules  

翻译的如果卡不懂和其他注意事项,

请直接看:http://jetsonhacks.com/2014/08/28/building-openni2-structure-sensor/

运行结果:






三、PCL安装

参考:

http://larrylisky.com/2014/03/03/installing-pcl-on-ubuntu/

安装依赖环境

  1. sudo apt-get install g++  
  2. sudo apt-get install cmake cmake-gui  
  3. sudo apt-get install doxygen    
  4. sudo apt-get install mpi-default-dev openmpi-bin openmpi-common    
  5. sudo apt-get install libflann1 libflann-dev  
  6. sudo apt-get install libeigen3-dev  
  7. sudo apt-get install libboost-all-dev  
  8. sudo apt-get install libvtk5.8-qt4 libvtk5.8 libvtk5-dev  
  9. sudo apt-get install libqhull*  
  10. sudo apt-get install libusb-dev  
  11. sudo apt-get install libgtest-dev  
  12. sudo apt-get install git-core freeglut3-dev pkg-config  
  13. sudo apt-get install build-essential libxmu-dev libxi-dev   
  14. sudo apt-get install libusb-1.0-0-dev graphviz mono-complete  
  15. sudo apt-get install qt-sdk openjdk-7-jdk openjdk-7-jre  
  16. sudo apt-get install phonon-backend-gstreamer  
  17. sudo apt-get install phonon-backend-vlc  

从github上下载pcl最新版本并编译

  1. $git clone https://github.com/PointCloudLibrary/pcl.git pcl-trunk ln -s pcl-trunk pcl(我是直接下载了压缩包,解压的,不然太慢了)  
  2. $mkdir release  
  3. $cd release  
  4. $cmake -DCMAKE_BUILD_TYPE=None -DBUILD_GPU=ON -DBUILD_apps=ON -DBUILD_examples=ON ..  
  5. $make  
  6. $sudo make install  

至此完成安装,但是是VTK有问题的,留着下次解决---->这么解决:

大家如何cmake时,发现有类似The imported target "vtk***" reference the file "/usr/bin/vtk**"的错误,需要安装如下两个东西:

  1. sudo apt-get install libvtk-java  
  2. sudo apt-get install python-vtk  

然后再重新编译,时间非常久
  1. cmake -DCMAKE_BUILD_TYPE=None -DBUILD_GPU=ON -DBUILD_apps=ON -DBUILD_examples=ON ..  
  2. make  
  3. make install  

编译完成后,执行他的pcd_viewer,结果如下:


  1. #include <pcl/visualization/cloud_viewer.h>  
  2. #include <iostream>  
  3. #include <pcl/io/io.h>  
  4. #include <pcl/io/pcd_io.h>  
  5. using namespace std;  
  6. using namespace pcl;  
  7.       
  8. int main ()  
  9. {  
  10.     pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);  
  11.   
  12.     if(pcl::io::loadPCDFile<pcl::PointXYZRGB>("color_5.pcd",*cloud)==-1)//*打开点云文件  
  13.     {  
  14.         PCL_ERROR("Couldn't read file test_pcd.pcd\n");  
  15.         return(-1);  
  16.     }  
  17.     std::cout<<"Loaded "<<cloud->width*cloud->height<<" data points from test_pcd.pcd with the following fields: "<<std::endl;  
  18.   
  19.     pcl::visualization::CloudViewer viewer("My First Cloud Viewer");  
  20.     viewer.showCloud(cloud);  
  21.     while(!viewer.wasStopped())  
  22.     {  
  23.   
  24.     }  
  25. }  

CMakeLists.txt
  1. cmake_minimum_required(VERSION 2.8 FATAL_ERROR)  
  2.   
  3. project(cloud_viewer)  
  4.   
  5. find_package(PCL 1.8 REQUIRED)  
  6.   
  7. include_directories(${PCL_INCLUDE_DIRS})  
  8. link_directories(${PCL_LIBRARY_DIRS})  
  9. add_definitions(${PCL_DEFINITIONS})  
  10.   
  11. add_executable (cloud_viewer test.cpp)  
  12. target_link_libraries (cloud_viewer ${PCL_LIBRARIES})  



四、最后再给出三个库同时使用的情形:

CMakeLists.txt

  1. # This CmakeLists include both OpenNI and OpenCV Libraries  
  2. cmake_minimum_required(VERSION 2.8)  
  3. project( TestOpenNI )  
  4.   
  5.   
  6. # OpenCV  
  7. find_package( OpenCV REQUIRED )  
  8. include_directories( ${OpenCV_INCLUDE_DIRS} )  
  9. MESSAGE(STATUS "The Opencv's include directory is:" ${OpenCV_INCLUDE_DIRS})  
  10.   
  11. #OpenNI  
  12. FIND_PATH(OpenNI2_INCLUDE_DIRS OpenNI.h HINTS  $ENV{OPENNI2_INCLUDE} PATH_SUFFIXES openni2)  
  13. FIND_LIBRARY(OpenNI2_LIBRARY NAMES OpenNI2 HINTS  $ENV{OPENNI2_LIB} $ENV{OPENNI2_REDIST})  
  14. include_directories( ${OpenNI2_INCLUDE_DIRS} )  
  15.   
  16.   
  17. IF (OpenNI2_INCLUDE_DIRS AND OpenNI2_LIBRARY)  
  18.    SET(OpenNI2_FOUND TRUE)  
  19. ENDIF (OpenNI2_INCLUDE_DIRS AND OpenNI2_LIBRARY)  
  20.   
  21. IF (OpenNI2_FOUND)  
  22.    # show which OpenNI2 was found only if not quiet  
  23.    SET(OpenNI2_LIBRARIES ${OpenNI2_LIBRARY})  
  24.    MESSAGE(STATUS "Found OpenNI2: ${OpenNI2_LIBRARIES}")  
  25. ELSE (OpenNI2_FOUND)  
  26.    # fatal error if OpenNI2 is required but not found  
  27.    IF (OpenNI2_FIND_REQUIRED)  
  28.       MESSAGE(FATAL_ERROR "Could not find OpenNI2. Environment variables OPENNI2_INCLUDE (directory containing OpenNI.h) and OPENNI2_LIB (directory containing OpenNI2 library) could bet set.")  
  29.    ENDIF (OpenNI2_FIND_REQUIRED)  
  30. ENDIF (OpenNI2_FOUND)  
  31.   
  32. #set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")  
  33. #set (OPENNI_H /usr/include/openni2/OpenNI.h)  
  34. # ---------------------------------------------------------  
  35.   
  36. #PCL  
  37. find_package(PCL 1.8 REQUIRED)  
  38. include_directories(${PCL_INCLUDE_DIRS})  
  39. link_directories(${PCL_LIBRARY_DIRS})  
  40. add_definitions(${PCL_DEFINITIONS})  
  41. add_executable(TestOpenNI test.cpp)  
  42. target_link_libraries(TestOpenNI ${OpenNI2_LIBRARIES} ${OpenCV_LIBS} ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES})  
c++文件:


  1. #include <pcl/io/pcd_io.h>  
  2. #include <pcl/io/ply_io.h>  
  3. #include <pcl/point_types.h>  
  4. // 标准库头文件  
  5. #include <iostream>  
  6. #include <string>  
  7. #include <vector>   
  8. // OpenCV头文件  
  9. #include <opencv2/photo.hpp>  
  10. #include <opencv2/highgui.hpp>   
  11. // OpenNI头文件  
  12. #include <OpenNI.h>   
  13. typedef unsigned char uint8_t;  
  14. // namespace  
  15. using namespace std;  
  16. using namespace openni;  
  17. using namespace cv;  
  18. using namespace pcl;  
  19.   
  20. void CheckOpenNIError( Status result, string status )    
  21. {     
  22.     if( result != STATUS_OK )     
  23.         cerr << status << " Error: " << OpenNI::getExtendedError() << endl;    
  24. }   
  25.   
  26. int main( int argc, char **argv )  
  27. {  
  28.     Status result = STATUS_OK;  
  29.     int i,j;  
  30.     float x=0.0,y=0.0,z=0.0,xx=0.0;    
  31.     //IplImage *test,*test2;  
  32.     IplImage *test2;  
  33.     char filename[20] = {0};  
  34.   
  35.     //point cloud   
  36.     PointCloud<PointXYZ> cloud;  
  37.     PointCloud<PointXYZRGB> color_cloud;  
  38.   
  39.     //opencv image  
  40.     Mat cvBGRImg;   
  41.     Mat cvDepthImg;    
  42.   
  43.     //OpenNI2 image    
  44.     VideoFrameRef oniDepthImg;    
  45.     VideoFrameRef oniColorImg;  
  46.   
  47.     namedWindow("depth");    
  48.     namedWindow("image");   
  49.   
  50.     char key=0;  
  51.   
  52.     // 初始化OpenNI    
  53.     result = OpenNI::initialize();  
  54.     CheckOpenNIError( result, "initialize context" );   
  55.       
  56.     // open device      
  57.     Device device;    
  58.     result = device.open( openni::ANY_DEVICE );   
  59.     CheckOpenNIError( result, "open device" );  
  60.   
  61.   
  62.     // create depth stream     
  63.     VideoStream oniDepthStream;    
  64.     result = oniDepthStream.create( device, openni::SENSOR_DEPTH );  
  65.     CheckOpenNIError( result, "create depth stream" );  
  66.     
  67.     // set depth video mode    
  68.     VideoMode modeDepth;    
  69.     modeDepth.setResolution( 640, 480 );    
  70.     modeDepth.setFps( 30 );    
  71.     modeDepth.setPixelFormat( PIXEL_FORMAT_DEPTH_1_MM );    
  72.     oniDepthStream.setVideoMode(modeDepth);    
  73.     // start depth stream    
  74.     result = oniDepthStream.start();   
  75.     CheckOpenNIError( result, "start depth stream" );  
  76.      
  77.     // create color stream    
  78.     VideoStream oniColorStream;    
  79.     result = oniColorStream.create( device, openni::SENSOR_COLOR );    
  80.     CheckOpenNIError( result, "create color stream" );  
  81.     // set color video mode    
  82.     VideoMode modeColor;    
  83.     modeColor.setResolution( 640, 480 );    
  84.     modeColor.setFps( 30 );    
  85.     modeColor.setPixelFormat( PIXEL_FORMAT_RGB888 );    
  86.     oniColorStream.setVideoMode( modeColor);   
  87.     // start color stream    
  88.     result = oniColorStream.start();   
  89.     CheckOpenNIError( result, "start color stream" );  
  90.       
  91.     int count = 0;  
  92.     while(true)  
  93.     {  
  94.         // read frame    
  95.         if( oniColorStream.readFrame( &oniColorImg ) == STATUS_OK )    
  96.         {    
  97.             // convert data into OpenCV type    
  98.             Mat cvRGBImg( oniColorImg.getHeight(), oniColorImg.getWidth(), CV_8UC3, (void*)oniColorImg.getData() );    
  99.             cvtColor( cvRGBImg, cvBGRImg, CV_RGB2BGR );    
  100.             imshow( "image", cvBGRImg );    
  101.         }    
  102.   
  103.         if( oniDepthStream.readFrame( &oniDepthImg ) == STATUS_OK )    
  104.         {    
  105.             Mat cvRawImg16U( oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData() );    
  106.             cvRawImg16U.convertTo( cvDepthImg, CV_8U, 255.0/(oniDepthStream.getMaxPixelValue()));      
  107.             imshow( "depth", cvDepthImg );    
  108.         }   
  109.   
  110.     char input = waitKey(1);   
  111.         // quit  
  112.         if( input == 'q' )        
  113.             break;  
  114.         // capture  depth and color data     
  115.         if( input == 'c' )  
  116.         {  
  117.             //get data  
  118.             DepthPixel *pDepth = (DepthPixel*)oniDepthImg.getData();  
  119.             //create point cloud  
  120.             cloud.width = oniDepthImg.getWidth();  
  121.             cloud.height = oniDepthImg.getHeight();  
  122.             cloud.is_dense = false;  
  123.             cloud.points.resize(cloud.width * cloud.height);  
  124.             color_cloud.width = oniDepthImg.getWidth();  
  125.             color_cloud.height = oniDepthImg.getHeight();  
  126.             color_cloud.is_dense = false;  
  127.             color_cloud.points.resize(color_cloud.width * color_cloud.height);  
  128.   
  129.             //test = cvCreateImage(cvSize(cloud.width,cloud.height),IPL_DEPTH_8U,3);  
  130.             IplImage temp11 = (IplImage)cvBGRImg;  
  131.             //test2 = &IplImage(cvBGRImg);  
  132.             test2 = &temp11;              
  133.   
  134.             for(i=0;i<oniDepthImg.getHeight();i++)  
  135.             {  
  136.                  for(j=0;j<oniDepthImg.getWidth();j++)  
  137.                  {  
  138.                      float k = i;    
  139.                      float m = j;   
  140.                      xx = pDepth[i*oniDepthImg.getWidth()+j];  
  141.                      CoordinateConverter::convertDepthToWorld (oniDepthStream,m,k,xx,&x,&y,&z);   
  142.                      cloud[i*cloud.width+j].x = x/1000;  
  143.                      cloud[i*cloud.width+j].y = y/1000;  
  144.                      cloud[i*cloud.width+j].z = z/1000;  
  145.   
  146.                      color_cloud[i*cloud.width+j].x = x/1000;  
  147.                      color_cloud[i*cloud.width+j].y = y/1000;  
  148.                      color_cloud[i*cloud.width+j].z = z/1000;  
  149.                      color_cloud[i*cloud.width+j].b = (uint8_t)test2->imageData[i*test2->widthStep+j*3+0];  
  150.                      color_cloud[i*cloud.width+j].g = (uint8_t)test2->imageData[i*test2->widthStep+j*3+1];  
  151.                      color_cloud[i*cloud.width+j].r = (uint8_t)test2->imageData[i*test2->widthStep+j*3+2];  
  152.                     /* test->imageData[i*test->widthStep+j*3+0] = test2->imageData[i*test2->widthStep+j*3+0];  
  153.                      test->imageData[i*test->widthStep+j*3+1] = test2->imageData[i*test2->widthStep+j*3+1];  
  154.                      test->imageData[i*test->widthStep+j*3+2] = test2->imageData[i*test2->widthStep+j*3+2];*/  
  155.                  }  
  156.              }  
  157.               
  158.             //cvSaveImage("test.jpg",test);  
  159.             //pcl::io::savePLYFileBinary("test_plyc.ply",cloud);  
  160.             cout<<"the "<<count<<" is saved"<<endl;  
  161.             sprintf(filename,"./data/%d.pcd",count);  
  162.             pcl::io::savePCDFileBinaryCompressed(filename,cloud);  
  163.             cerr<<"Saved "<<cloud.points.size()<<" data points to xyz pcd."<<endl;  
  164.             sprintf(filename,"./data/color_%d.pcd",count);  
  165.             pcl::io::savePCDFileBinaryCompressed(filename,color_cloud);  
  166.             cerr<<"Saved "<<color_cloud.points.size()<<" data points to xyzrgb pcd."<<endl;  
  167.             sprintf(filename,"./data/color_%d.jpg",count);  
  168.             imwrite(filename,cvBGRImg);  
  169.             sprintf(filename,"./data/depth_%d.jpg",count++);  
  170.             imwrite(filename,cvDepthImg);  
  171.             /*for(size_t i=0;i<cloud.points.size();++i)  
  172.             cerr<<"    "<<cloud.points[i].x<<" "<<cloud.points[i].y<<" "<<cloud.points[i].z<<endl;*/  
  173.         }  
  174.     }  
  175. }  

运行结果:


键盘输入c抓取数据,按q退出






评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值