对cv :: calcOpticalFlow的未定义引用

时间:2014-05-07 16:07:14

标签: c++ opencv ubuntu undefined-reference

修改 哎,

对于其他有类似问题的人,我想出了一些工作。如果你只是使用:

编译它
gcc `pkg-config --cflags opencv` CameraMotionTest.cpp `pkg-config --libs opencv` -o cammotion 

而不是我使用的makefile,它正确编译。我还不确定我之前使用的方法有什么问题,所以如果有人还想对此进行评论,那么。

执行此操作后,我在代码中发现了一些其他需要修复的问题,但这些问题与此问题没有任何关系,所以我不会在这里进入。

谢谢!

ORIGINAL

我正在尝试使用openCV在Ubuntu上编译一个用于摄像机运动估计的短代码,但是我遇到了" undefined reference"其中一个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 ++一般都是新手,而且我还没有能够找出问题所在。

谢谢! 安德鲁

2 个答案:

答案 0 :(得分:1)

对于其他有类似问题的人,我想出了一些工作。如果你只是使用:

编译它
gcc `pkg-config --cflags opencv` CameraMotionTest.cpp `pkg-config --libs opencv` -o cammotion 

而不是我使用的makefile,它正确编译。我还不确定我之前使用的方法有什么问题,所以如果有人还想对此进行评论,那么。

执行此操作后,我在代码中发现了一些其他需要修复的问题,但这些问题与此问题无关,所以我不会在这里讨论。

谢谢!

答案 1 :(得分:0)

您的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 ...(基于this回答)。