Frage

EDIT Hey,

For anyone else having a similar issue, I figured something of a work around out. If you just compile this using :

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

instead of the makefile that I used, it compiles correctly. I'm not exactly sure what was wrong with the method I was using before still so if someone still wants to comment on that go ahead.

After doing this i found some other issues in the code that needed fixed as well but those didn't have anything to do with this question so I won't go into them here.

Thanks!

ORIGINAL

I am trying to compile a short code for camera motion estimation on Ubuntu using openCV but am running into and "undefined reference" error for one of the openCV functions (and only one). The error I get when I try to compile is as follows:

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

I am using this makefile to try and compile and run the program:

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

Finally, the code I am trying to compile is:

#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;
}

Has anyone else run into a problem like this? I am assuming that there is just a linking error somewhere but I am quite frankly pretty new to opencv and c++ in general and i haven't been able to figure out what is wrong yet.

Thanks! Andrew

War es hilfreich?

Lösung

For anyone else having a similar issue, I figured something of a work around out. If you just compile this using :

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

instead of the makefile that I used, it compiles correctly. I'm not exactly sure what was wrong with the method I was using before still so if someone still wants to comment on that go ahead.

After doing this i found some other issues in the code that needed fixed as well but those didn't have anything to do with this question so I won't go into them here.

Thanks!

Andere Tipps

It seems that You have some problem with Your OpenCV instalation. To compile Your code, on OpenCV 2.4.9, it was enough to use

g++ t1.cpp -lopencv_video -lopencv_core -lopencv_objdetect -lopencv_imgproc  -lopencv_highgui -lopencv_calib3d -o CameraMotion

You can also try using nm -g <library> | grep -i <function_name> to check if Your libopencv_video.so contains calcOpticalFlowPyrLK... (based on this answer).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top