我在 Ubuntu 平台上使用 OpenCv 和 OpenKinect 来访问 Kinect 传感器。主要错误是:undefined reference to `freenect_sync_get_rgb_cv' 帮我调试一下这个错误。源代码如下:
#include <iostream>
#include <cv.h>
#include <opencv/highgui.h>
// OpenKinect Header files
#include <libfreenect.h>
#include <libfreenect_sync.h>
#include <libfreenect/libfreenect_cv.h>
// --- C++ ---
#include <stdio.h>
#include <fstream>
#include <vector>
#include <math.h>
#include <iostream>
#include <vector>
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;
char key;
// IplImage *freenect_sync_get_depth_cv(int index);
// IplImage *freenect_sync_get_rgb_cv(int index);
int main(int argc, char** argv)
{
IplImage* image = NULL;
/* create a window */
cvNamedWindow("Camera_Output", 1);
while(1) {
image = freenect_sync_get_rgb_cv(0);
// Mat image(freenect_sync_get_rgb_cv(0));
//CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
// cvCvtColor(image, image, CV_RGB2BGR); // cvLoadImage( freenect_sync_get_rgb_cv(0) )
// VideoCapture::grab
//cvCreateImage(cvSize(640, 480), 8, 4);
cvShowImage("Camera_Output", image);
if (!image) {
printf("Error: Kinect not connected?\n");
return -1;
}
key = cvWaitKey(100); //Capture Keyboard stroke
if (char(key) == 27){
break;
}
}
/* free memory */
cvDestroyWindow( "video" );
return 0;
}
错误看起来像:
[100%] Building CXX object CMakeFiles/KinectRGB.dir/KinectRGB.cpp.o
Linking CXX executable KinectRGB
CMakeFiles/KinectRGB.dir/KinectRGB.cpp.o: In function `main':
KinectRGB.cpp:(.text+0x2c): undefined reference to `freenect_sync_get_rgb_cv'
collect2: error: ld returned 1 exit status
make[2]: *** [KinectRGB] Error 1
make[1]: *** [CMakeFiles/KinectRGB.dir/all] Error 2
make: *** [all] Error 2
sincos@sincos-300E4C-300E5C-300E7C:~/Desktop/OpenCV_test/KinectRead/build$
构建代码的 CMakeLists.txt 文件如下所示:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
project(KinectRGB)
set(DEPENDENCIES OpenCV GLUT OpenGL)
message("\n")
foreach( DEP ${DEPENDENCIES} )
find_package( ${DEP} )
string( TOUPPER ${DEP} UDEP ) # Capitalize
if( ${DEP}_FOUND OR ${UDEP}_FOUND )
message("\n${DEP}_Found = TRUE\n")
endif()
find_package(Threads REQUIRED)
find_package(libfreenect REQUIRED)
include_directories("/usr/include/libusb-1.0/")
endforeach()
include_directories(
${FREENECT_INCLUDE_DIR}
${GLUT_INCLUDE_DIR}
${OPENGL_INCLUDE_DIR}
${OpenCV_INCLUDE_DIRS}
)
add_executable(KinectRGB KinectRGB.cpp)
target_link_libraries(KinectRGB
${FREENECT_LIBRARIES}
${GLUT_LIBRARY}
${OPENGL_LIBRARIES}
${OpenCV_LIBS}
${CMAKE_THREAD_LIBS_INIT}
)