本来用VS用的好好的,但是转到了Ubuntu后发现直接抓瞎= =
本着菜鸟的心态老老实实地用了Eclipse(编译C++要用Helios,Mars各种错 ),还发现网上资料奇少,仅有的资料还错误很多,特此编写此文章,希望可以帮助后来的同学不要浪费不必要的时间在搭建软件环境上。
注:我系统是Ubuntu 14.04,64位的。另外,OpenCV,OpenGL,OpenNI,Nite的安装我就不赘述了,网上资料有很多。
本文基本参考Ubuntu下安装配置OpenNI,OpenCV,并由此文章修改而来。
一、安装Eclipse
1.下载
http://www.eclipse.org/downloads/
因为我是用C++,所以下载的IDE 4 C/C++ Developers
2.解压出eclipse文件夹并移动到想要的文件夹下
3.创建桌面链接
# gedit /usr/share/applications/eclipse.desktop
写入以下内容
[Desktop Entry]
Name=Eclipse
Comment=Eclipse SDK
Encoding=UTF-8
Exec=(文件夹所在位置)eclipse/eclipse
Icon=(文件夹所在位置)eclipse/icon.xpm
Terminal=false
Type=Application
Categories=Application;Development;
二、OpenCV(我的版本是3.1.0)
1.使用eclipse建立新工程,如testOpenCV
2.在左侧的project exploer中右击testOpenCV,点properties,在对话框中选择C/C++ Build->settings->GCC C++ Compiler(如果用C写就选GCC C Compiler)->Directories,在右侧Include paths(-l)里点右边绿色加号,添加路径/usr/local/include/opencv
3.然后再选择GCC C++ Linker->Libraries,在Libraries (-l)中添加opencv_core,opencv_highgui,如果需要其他库也依次添加。库的路径是/usr/local/lib,因此要添加 Library search path (-L):/usr/local/lib。
测试代码:
#include <cv.h>
#include <opencv2/opencv.hpp>//原文中都是早期版本OpenCV用的函数名和库文件,并不能通过编译
using namespace cv;
int main( )
{
Mat image;
image = imread( "lena.jpg", 1);
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
(Lena.jpg自己随便找一张图片就行,放到cpp所在的文件夹下)
三、OpenNI
1.新建一个空的或helloworld工程,如kinectOpenNI;
2.在左侧的project explorer中右击kinectOpenNI,点properties,在对话框左侧中选择C/C++ Build->Settings->GCC C++ Compiler(如果用C写就选GCC C Compiler)->Includes;
在右侧Include paths(-l)里点右边绿色加号,添加两个路径:
/usr/include/ni
/usr/include/nite
3.然后再选择GCC C++ Linker->Libraries,在Libraries (-l)中添加
OpenNI
glut
GL(原文中没有提到的库,但是必须要用到)
XnVNite(注意XnVNite可能有版本号,要到你的/usr/lib目录下看一看,有个文件叫libXnVNite_XXXX.so之类的,我的是libXnVNite_1_5_2.so,所以我填的是XnVNite_1_5_2,反正就是随机应变吧,填不对的话它会报错说找不到库)
由于这几个库都是在系统/usr/lib/目录下的,因此不用添加Library search path (-L)。
测试代码:
/*******************************
* *
*OpenNI 1.x Alpha *
*Copyright (C) 2011 PrimeSense Ltd. *
* *
*This file is part of OpenNI. *
* *
*OpenNI is free software: you can redistribute it and/or modify *
*it under the terms of the GNU Lesser General Public License as published *
*by the Free Software Foundation, either version 3 of the License, or *
*(at your option) any later version.*
* *
*OpenNI is distributed in the hope that it will be useful,*
*but WITHOUT ANY WARRANTY; without even the implied warranty of *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
*GNU Lesser General Public License for more details.*
* *
*You should have received a copy of the GNU Lesser General Public License *
*along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
***********************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOS.h>
#include <GL/glut.h>
#include <math.h>
#include <XnCppWrapper.h>
using namespace xn;
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
#define SAMPLE_XML_PATH "/home/dna/kinect/OpenNI-Bin-Dev-Linux-x64-v1.5.7.10/Samples/Config/SamplesConfig.xml"
#define GL_WIN_SIZE_X 1280
#define GL_WIN_SIZE_Y 1024
#define DISPLAY_MODE_OVERLAY 1
#define DISPLAY_MODE_DEPTH 2
#define DISPLAY_MODE_IMAGE 3
#define DEFAULT_DISPLAY_MODE DISPLAY_MODE_DEPTH
#define MAX_DEPTH 10000
//----------------------------