编辑 嘿,
对于其他有类似问题的人,我想了一些解决办法。如果你只是使用编译
gcc `pkg-config --cflags opencv` CameraMotionTest.cpp `pkg-config --libs opencv` -o cammotion
它编译正确,而不是我使用的makefile。我不太确定我之前使用的方法出了什么问题,所以如果有人还想对此发表评论,那就继续吧。
完成这项工作后,我在代码中发现了一些其他需要修复的问题,但这些问题与这个问题无关,所以我不会在这里讨论它们。
谢谢!
原件
我正试图使用openCV在Ubuntu上编译一个用于相机运动估计的短代码,但遇到了一个openCV函数(而且只有一个(的"未定义引用"错误。当我尝试编译时,我得到的错误如下:
g++ CameraMotionTest.cpp -lopencv_video -lopencv_calib3d -lopencv_imgproc -lopencv_objdetect -lopencv_features2d -lopencv_core -lopencv_highgui -lopencv_videostab -lopencv_contrib -lopencv_flann -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_gpu -lopencv_ocl -o CameraMotion
/tmp/ccdHB3Pr.o: In function `main':
CameraMotionTest.cpp:(.text+0x77f): undefined reference to `cv::calcOpticalFlowPyrLK(cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray
const&, cv::_OutputArray const&, cv::_OutputArray const&, cv::_OutputArray const&, cv::Size_<int>, int, cv::TermCriteria, int, double)'
collect2: ld returned 1 exit status
make: *** [CameraMotion] Error 1
我正在使用这个makefile来尝试编译和运行程序:
all: run
run: CameraMotion
./CameraMotion *.jpg
CameraMotion: CameraMotionTest.cpp
g++ CameraMotionTest.cpp -lopencv_video -lopencv_calib3d -lopencv_imgproc -lopencv_objdetect -lopencv_features2d -lopencv_core -lopencv_highgui -lopencv_videostab -lopencv_contrib -lopencv_flann -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_gpu -lopencv_ocl -o CameraMotion
最后,我试图编译的代码是:
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;
using namespace cv;
int main(int argc, const char** argv){
//storing the image in a temporary variable
vector<Mat> img;
int noi=5;
for( int index=0; index<noi;index++){
img.push_back(imread(argv[index+1]));
}
Mat im1=img[0];
//converting image to grayscale
cvtColor(im1,im1,CV_RGB2GRAY);
//initializing variable
vector<Point2f> corners1, corners2;
//setting parameters for corner detection
int maxCorner=200;
double quality=0.01;
double minDist=20;
int blockSize=3;
double k=0.04;
Mat mask;
vector<uchar> status;
vector<float> track_err;
int maxlevel=3;
Mat im2=img[1];
TermCriteria termcrit(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS,20,.03);
vector<Point2f> pointskept1,pointskept2;
vector<int>pointskeptindex;
Mat F,E,R,tran;
Matx33d W(0,-1,0,
1,0,0,
0,0,1);
Matx33d Winv(0,1,0,
-1,0,0,
0,0,1);
OutputArray statF=noArray();
float fx=951.302687761842550;
float fy=951.135570101293520;
float cx=484.046807724895250;
float cy=356.325026020307800;
float alpha=0;
float kmatdata[3][3]={{fx,fy*tan(alpha),cx},{0,fy,cy},{0,0,1}};
Mat K(3,3,CV_32FC1,kmatdata);
cout<<K<<endl;
ofstream myfile;
//collecting new images, determining corners, and calculating optical flow
for (int i=1; i<noi-1; i++) {
//capturing next image
//converting new image to grayscale
cvtColor(im2,im2,CV_RGB2GRAY);
//determining corner features
goodFeaturesToTrack(im1,corners1, maxCorner, quality, minDist, mask, blockSize, false,k);
goodFeaturesToTrack(im2,corners2, maxCorner, quality, minDist, mask, blockSize, false,k);
//calculating optical flow
calcOpticalFlowPyrLK(im1,im2,corners1,corners2,status,track_err,Size(10,10),maxlevel,termcrit,0.0001);
//filtering points
for(int t=0; t<status.size();i++){
if(status[t] && track_err[i]<12.0){
pointskeptindex.push_back(i);
pointskept1.push_back(corners1[i]);
pointskept2.push_back(corners2[i]);
} else {
status[i]=0;
}
}
F=findFundamentalMat(pointskept1,pointskept2,FM_RANSAC,1,0.99,statF);
E=K.t()*F*K;
SVD svd(E);
R=svd.u*Mat(W)*svd.vt;
tran=svd.u.col(2);
//renaming new image to image 1
im2.copyTo(im1);
im2=img[i+1];
myfile.open("output.txt", ios_base::app);
myfile<<"Rotation mat: ";
for(int l=0;l<R.rows;l++){
for(int m=0; m<R.cols; m++){
myfile<<R.at<float>(i,m)<<", ";
}
}
myfile<<"Translation vector: ";
for(int l=0; l<tran.rows;l++){
myfile<<tran.at<float>(l,1)<<", ";
}
myfile<<"n";
myfile.close();
}
return 0;
}
其他人遇到过这样的问题吗?我假设某个地方只是有一个链接错误,但坦率地说,我对opencv和c++还很陌生,我还没能弄清楚哪里出了问题。
谢谢!Andrew
对于其他有类似问题的人,我想了一些解决办法。如果你只是使用编译
gcc `pkg-config --cflags opencv` CameraMotionTest.cpp `pkg-config --libs opencv` -o cammotion
它编译正确,而不是我使用的makefile。我不太确定我之前使用的方法出了什么问题,所以如果有人还想对此发表评论,那就继续吧。
完成这项工作后,我在代码中发现了一些其他需要修复的问题,但这些问题与这个问题无关,所以我不会在这里讨论它们。
谢谢!
您的OpenCV安装似乎有一些问题。为了编译你的代码,在OpenCV 2.4.9上,使用就足够了
g++ t1.cpp -lopencv_video -lopencv_core -lopencv_objdetect -lopencv_imgproc -lopencv_highgui -lopencv_calib3d -o CameraMotion
您也可以尝试使用nm -g <library> | grep -i <function_name>
来检查您的libopencv_video.so是否包含calcOpticalFlowPyrLK。。。(基于这个答案(。