It took a long time for the boss to complete OpenNI and OPenCV configuration in Linux. There was little talk about the OpenNI configuration method in Linux on the Internet, looking for methods to use OpenNI for development is like a haystack ...... I don't even mention any words in the manual. It's just a matter of turning over the good information. I would like to share this with you.
I. OPenNI
1. Software Download:
(1) OpenNI: aspx "> http://www.openni.org/Downloads/OpenNIModules.aspx
Choose "OpenNI Binaries"-> "Unstable"-> "... for ubuntu..." and click "Download ".
Decompress the downloaded package and go to the decompressed path of cd: $./install. sh (do not remember to add sudo. Try it)
(2) SensorKinect:
Command: $ git clone https://github.com/avin2/SensorKinect.git
If git is not installed, sudo apt-get install ~
The process is slow. After the process is completed, a folder SensorKinect will appear in the current path, cd will be added to SensorKinect/Platform/Linux/CreateRedist, and then $. /RedistMaker. The Redist folder appears in the upper-level directory Linux. In this case, the $./install. sh directory is displayed on the Internet, but the instal. sh file is only available in a directory at a level. But it seems that the root permission is required to execute this operation. I don't know how to do it. $ sudo just, I simply copied it out, decompressed it, and went in (... /SensorKinect/Platform/Linux/CreateRedist/Sensor-Bin-Linux-x86-v5.0.5.1/), in the inside $./install. sh, it turned out to be OK. By the way, you may enter $. /install. sh will prompt that there are no commands or the like, you can install. on the sh file, right-click "properties" and select "allow program execution File.
In this case, you can test in the OpenNI-Bin-XXXX/Samples/Bin/x86-Release Directory: $. /NiViewer, may prompt that there is no library and the like, try to dress up: $ sudo apt-get install libusb-1.0-0-dev freeglut3-dev, should be able to run after, you can see the long-awaited screen. If the system prompts "Failed to set USB interface" at this time! Or Open failed: The network connection has been closed !, Run the following command in the command line:
$ Sudo rmmod gspca_kinect
This is because Ubuntu may have its own gspca_kinect driver, which is in conflict with each other. It seems that you need to execute this statement every time you restart your computer.
For more information, see https://github.com/avin2/sensorkinect.
(2) NITE: http://www.openni.org/Downloads/OpenNIModules.aspx
Select "OpenNI Compliant Middleware Binaries"-> "Unstable"-> "... Ubuntu..." and download it.
Decompress the package and enter the directory $./install. sh.
2. Development Environment Configuration
I chose eclipse-cdt for development. The only poor information on the internet is that codeblocks is used for development, which is almost the same. Let's take eclipse as an example.
Create an empty or helloworld project, such as kinectOpenNI. In the project exploer on the left, right-click kinectOpenNI, and click properties, in the dialog box, select C/C ++ Build-> settings-> gcc c ++ Compiler (select gcc c Compiler if written in C)-> Directories, in Include paths (-l) on the Right, click the green plus sign on the right, and add two paths:/usr/include/ni and/usr/include/nite, then select gcc c ++ Linker-> Libraries and add OpenNI, glut, and XnVNite to Libraries (-l). Note that XnVNite may have version numbers, you need to go to your/usr/lib directory and check that there is a file named libXnVNite_XXXX.so. My file is libxnvnite_rj5_0.so, So I entered XnVNite _ Listen 5_0. It is just a random response. If it is incorrect, it will report an error saying that the database cannot be found. Because these libraries are in the system/usr/lib/directory, you do not need to add the Library search path (-L ).
So far, your project can be compiled. Try this example: (Note the path of the xml file. The example is found directly from the installation package. Of course, remember to connect your Kinect)
View Code
/*************************************** *************************************
**
* 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 *
* MERCHANTABILITY or fitness for a special PURPOSE. See *
* GNU Lesser General Public License for more details .*
**
* You shoshould have written ed a copy of the GNU Lesser General Public License *
* Along with OpenNI. If not, see **
**************************************** ************************************/
//---------------------------------------------------------------------------
// Des
//---------------------------------------------------------------------------
# Include <XnOS. h>
# If (XN_PLATFORM = XN_PLATFORM_MACOSX)
# Include <GLUT/glut. h>
# Else
# Include <GL/glut. h>
# Endif
# Include <math. h>
# Include <XnCppWrapper. h>
Using namespace xn;
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
# Define SAMPLE_XML_PATH "/home/iotuyrfviloh/Softwares/OpenNI-Bin-Dev-Linux-x86-v1.4.0.2/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
//---------------------------------------------------------------------------
// Globals
//---------------------------------------------------------------------------
Float g_pDepthHist [MAX_DEPTH];
XnRGB24Pixel * g_pTexMap = NULL;
Unsigned int g_nTexMapX = 0;
Unsigned int g_nTexMapY = 0;
Unsigned int g_nViewState = DEFAULT_DISPLAY_MODE;
Context g_context;
ScriptNode g_scriptNode;
DepthGenerator g_depth;
ImageGenerator g_image;
DepthMetaData g_depthMD;
ImageMetaData g_imageMD;
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
Void glutIdle (void)
{
// Display the frame
Glupostredisplay ();
}
Void gludisplay (void)
{
XnStatus rc = XN_STATUS_ OK;
// Read a new frame
Rc = g_context.WaitAnyUpdateAll ();
If (rc! = XN_STATUS_ OK)
{
Printf ("Read failed: % s \ n", xnGetStatusString (rc ));
Return;
}
G_depth.GetMetaData (g_depthMD );
G_image.GetMetaData (g_imageMD );
Const XnDepthPixel * pDepth = g_depthMD.Data ();
Const XnUInt8 * pImage = g_imageMD.Data ();
Unsigned int nImageScale = GL_WIN_SIZE_X/g_depthMD.FullXRes ();
// Copied from SimpleViewer
// Clear the OpenGL buffers
GlClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Setup the OpenGL viewpoint
GlMatrixMode (GL_PROJECTION );
GlPushMatrix ();
GlLoadIdentity ();
GlOrtho (0, GL_WIN_SIZE_X, GL_WIN_SIZE_Y, 0,-1.0, 1.0 );
// Calculate the accumulative histogram (the yellow display ...)
XnOSMemSet (g_pDepthHist, 0, MAX_DEPTH * sizeof (float ));
Unsigned int nNumberOfPoints = 0;
For (XnUInt y = 0; y <g_depthMD.YRes (); ++ y)
{
For (XnUInt x = 0; x <g_depthMD.XRes (); ++ x, ++ pDepth)
{
If (* pDepth! = 0)
{
G_pDepthHist [* pDepth] ++;
NNumberOfPoints ++;
}
}
}
For (int nIndex = 1; nIndex <MAX_DEPTH; nIndex ++)
{
G_pDepthHist [nIndex] + = g_pDepthHist [nIndex-1];
}
If (nNumberOfPoints)
{
For (int nIndex = 1; nIndex <MAX_DEPTH; nIndex ++)
{
G_pDepthHist [nIndex] = (unsigned int) (256 * (1.0f-(g_pDepthHist [nIndex]/nNumberOfPoints )));
}
}
XnOSMemSet (g_pTexMap, 0, g_nTexMapX * g_nTexMapY * sizeof (XnRGB24Pixel ));
// Check if we need to draw image frame to texture
If (g_nViewState = DISPLAY_MODE_OVERLAY |
G_nViewState = DISPLAY_MODE_IMAGE)
{
Const XnRGB24Pixel * pImageRow = g_imageMD.RGB24Data ();
XnRGB24Pixel * pTexRow = g_pTexMap + g_imageMD.YOffset () * g_nTexMapX;
For (XnUInt y = 0; y <g_imageMD.YRes (); ++ y)
{
Const XnRGB24Pixel * pImage = pImageRow;
XnRGB24Pixel * pTex = pTexRow + g_imageMD.XOffset ();
For (XnUInt x = 0; x <g_imageMD.XRes (); ++ x, ++ pImage, ++ pTex)
{
* PTex = * pImage;
}
PImageRow + = g_imageMD.XRes ();
PTexRow + = g_nTexMapX;
}
}
// Check if we need to draw depth frame to texture
If (g_nViewState = DISPLAY_MODE_OVERLAY |
G_nViewState = DISPLAY_MODE_DEPTH)
{
Const XnDepthPixel * pDepthRow = g_depthMD.Data ();
XnRGB24Pixel * pTexRow = g_pTexMap + g_depthMD.YOffset () * g_nTexMapX;
For (XnUInt y = 0; y <g_depthMD.YRes (); ++ y)
{
Const XnDepthPixel * pDepth = pDepthRow;
XnRGB24Pixel * pTex = pTexRow + g_depthMD.XOffset ();
For (XnUInt x = 0; x <g_depthMD.XRes (); ++ x, ++ pDepth, ++ pTex)
{
If (* pDepth! = 0)
{
Int nHistValue = g_pDepthHist [* pDepth];
PTex-> nRed = nHistValue;
PTex-> nGreen = nHistValue;
PTex-> nBlue = 0;
}
}
PDepthRow + = g_depthMD.XRes ();
PTexRow + = g_nTexMapX;
}
}
// Create the OpenGL texture map
GlTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE );
GlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
GlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
GlTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, g_nTexMapX, g_nTexMapY, 0, GL_RGB, GL_UNSIGNED_BYTE, g_pTexMap );
// Display the OpenGL texture map
GlColor4f (1, 1, 1 );
GlBegin (GL_QUADS );
Int nXRes = g_depthMD.FullXRes ();
Int nYRes = g_depthMD.FullYRes ();
// Upper left
GlTexCoord2f (0, 0 );
GlVertex2f (0, 0 );
// Upper right
GlTexCoord2f (float) nXRes/(float) g_nTexMapX, 0 );
GlVertex2f (GL_WIN_SIZE_X, 0 );
// Bottom right
GlTexCoord2f (float) nXRes/(float) g_nTexMapX, (float) nYRes/(float) g_nTexMapY );
GlVertex2f (GL_WIN_SIZE_X, GL_WIN_SIZE_Y );
// Bottom left
GlTexCoord2f (0, (float) nYRes/(float) g_nTexMapY );
GlVertex2f (0, GL_WIN_SIZE_Y );
GlEnd ();
// Swap the OpenGL display buffers
Gluswapbuffers ();
}
Void glukeyboard (unsigned char key, int x, int y)
{
Switch (key)
{
Case 27:
Exit (1 );
Case '1 ':
G_nViewState = DISPLAY_MODE_OVERLAY;
G_depth.GetAlternativeViewPointCap (). SetViewPoint (g_image );
Break;
Case '2 ':
G_nViewState = DISPLAY_MODE_DEPTH;
G_depth.GetAlternativeViewPointCap (). ResetViewPoint ();
Break;
Case '3 ':
G_nViewState = DISPLAY_MODE_IMAGE;
G_depth.GetAlternativeViewPointCap (). ResetViewPoint ();
Break;
Case 'M ':
G_context.SetGlobalMirror (! G_context.GetGlobalMirror ());
Break;
}
}
Int main (int argc, char * argv [])
{
XnStatus rc;
EnumerationErrors errors;
Rc = g_context.InitFromXmlFile (SAMPLE_XML_PATH, g_scriptNode, & errors );
If (rc = XN_STATUS_NO_NODE_PRESENT)
{
XnChar strError [1024];
Errors. ToString (strError 1024 );
Printf ("% s \ n", strError );
Return (rc );
}
Else if (rc! = XN_STATUS_ OK)
{
Printf ("Open failed: % s \ n", xnGetStatusString (rc ));
Return (rc );
}
Rc = g_context.FindExistingNode (XN_NODE_TYPE_DEPTH, g_depth );
If (rc! = XN_STATUS_ OK)
{
Printf ("No depth node exists! Check your XML .");
Return 1;
}
Rc = g_context.FindExistingNode (XN_NODE_TYPE_IMAGE, g_image );
If (rc! = XN_STATUS_ OK)
{
Printf ("No image node exists! Check your XML .");
Return 1;
}
G_depth.GetMetaData (g_depthMD );
G_image.GetMetaData (g_imageMD );
// Hybrid mode isn' t supported in this sample
If (g_imageMD.FullXRes ()! = G_depthMD.FullXRes () | g_imageMD.FullYRes ()! = G_depthMD.FullYRes ())
{
Printf ("The device depth and image resolution must be equal! \ N ");
Return 1;
}
// RGB is the only image format supported.
If (g_imageMD.PixelFormat ()! = XN_PIXEL_FORMAT_RGB24)
{
Printf ("The device image format must be RGB24 \ n ");
Return 1;
}
// Texture map init
G_nTexMapX = (unsigned short) (g_depthMD.FullXRes ()-1)/512) + 1) * 512;
G_nTexMapY = (unsigned short) (g_depthMD.FullYRes ()-1)/512) + 1) * 512;
G_pTexMap = (XnRGB24Pixel *) malloc (g_nTexMapX * g_nTexMapY * sizeof (XnRGB24Pixel ));
// OpenGL init
Gluinit (& argc, argv );
Fig );
Gluinitwindowsize (GL_WIN_SIZE_X, GL_WIN_SIZE_Y );
Valley createwindow ("OpenNI Simple Viewer ");
Glufullscreen ();
Glusetcursor (glu_cursor_none );
Glukeyboardfunc (glukeyboard );
Gludisplayfunc (gludisplay );
GlutIdleFunc (glutIdle );
GlDisable (GL_DEPTH_TEST );
Glable (GL_TEXTURE_2D );
// Per frame code is in gludisplay
Glumainloop ();
Return 0;
}
Ii. OpenCV
Although OpenGL can be used, OpenCV is still very good.
1. Software Download
(1) install these:
Apt-get install build-essential
Apt-get install cmake-gui
Apt-get install pkg-config
Apt-get install libpng12-0 libpng12-dev libpng ++-dev libpng3
Apt-get install libpnglite-dev libpngwriter0-dev libpngwriter0c2
Apt-get install zlib1g-dbg zlib1g zlib1g-dev
Apt-get install libjasper-dev libjasper-runtime libjasper1
Apt-get install pngtools libtiff4-dev lib1_4 lib1_xx0c2 libtiff-tools
Apt-get install lib1_8 libjpeg8-dev libjpeg8-dbg libjpeg-prog
Apt-get install ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev
Apt-get install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev
Apt-get install libxine1-ffmpeg libxine-dev libxine1-bin
Apt-get install libunicap2 libunicap2-dev
Apt-get install libdc1394-22-dev libdc1394-22 libdc1394-utils
Apt-get install swig
Apt-get install libv4l-0 libv4l-dev
Apt-get install python-numpy
Apt-get install libpython2.6 python-dev python2.6-dev
You may not be able to use it, but it seems that build-essential, cmake, pkg-config are all necessary, and others are not sure. Please install them for the sake of insurance ~
(2) download the latest version of OpenCV: http://sourceforge.net/projects/opencvlibrary/files/
Decompress, generate a directory like a OpenCV-2.3.1, and create a directory next to it, such as a OpenCV-2.3.1-build. In this case, you can open the cmake-gui and find it in the "programming" item on the main menu. Fill in the complete paths for the OpenCV-2.3.1 and OpenCV-2.3.1-build, respectively, in the Source and Build columns above. Click Config below. If the window content turns red, click again until you know it is not red, then click Generate. Command line cd into the OpenCV-2.3.1-build, $ make and then $ sudo make install, then OpenCV should be installed under/usr/local.
2. Create a project
Use eclipse to create a new project. For example, in the project exploer on the left, right-click testOpenCV, and click properties, in the dialog box, select C/C ++ Build-> settings-> gcc c ++ Compiler (select gcc c Compiler if written in C)-> Directories, in the Include paths (-l) on the Right, click the green plus sign on the right, add the path/usr/local/include/opencv, and then select gcc c ++ Linker-> Libraries, add opencv_core and opencv_highgui to Libraries (-l). If you need other Libraries, add them one by one. The Library path is/usr/local/lib. Therefore, you must add the Library search path (-L):/usr/local/lib.
Add environment variables to the system. I tried several methods, and I don't know which one will take effect:
Input in the command line in sequence (that is, create a new file, add a line/usr/local/lib, or use gedit ):
Sudo vi/etc/ld. so. conf. d/opencv. conf
G
O
/Usr/local/lib
<Esc>
: Wq!
Similarly, open the file/etc/bash. bashrc and add the following two lines at the end:
PKG_CONFIG_PATH = $ PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
Export PKG_CONFIG_PATH
Then input the following in the command line:
Sudo ldconfig-v
Export LD_LIBRARY_PATH =/usr/local/lib: $ LD_LIBRARY_PATH
Restart, and then compile and run the program. If the configuration is not good, an error may be reported that the libopencv_core.so file is not available. If it still doesn't work, let's look at other posts. Different people may have different situations. I feel deeply.
Okay, let's see if it can run:
Test procedure:
# Include <iostream>
# Include "cv. h"
# Include <cxcore. h>
# Include Using namespace std;
Int main (){
IplImage * img = cvLoadImage ("a.jpg ");
CvNamedWindow ("Image:", 1 );
CvShowImage ("Image:", img );
CvWaitKey ();
CvDestroyWindow ("Image :");
CvReleaseImage (& img );
Return 0;
}
The image path is consistent with the cpp file in the project, or an absolute path.
If everything goes well, congratulations.
With OpenNI and OpenCV, feel free to develop your interesting Kinect application ~
Author: blog address: http://sunshy.cnblogs.com/