title: undefined reference to cv::calcOpticalFlow
body: <p><strong><em>EDIT</em></strong>
  Hey,</p>
  
  <p>For anyone else having a similar issue, I figured something of a work around out.  If you just compile this using :</p>
  
  <pre><code>gcc `pkg-config --cflags opencv` CameraMotionTest.cpp `pkg-config --libs opencv` -o cammotion 
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Thanks!</p>
  
  <p><strong><em>ORIGINAL</em></strong></p>
  
  <p>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:</p>
  
  <pre><code>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&amp;, cv::_InputArray const&amp;, cv::_InputArray 
  const&amp;, cv::_OutputArray const&amp;, cv::_OutputArray const&amp;, cv::_OutputArray const&amp;, cv::Size_&lt;int&gt;, int, cv::TermCriteria, int, double)'
  collect2: ld returned 1 exit status
  make: *** [CameraMotion] Error 1
  </code></pre>
  
  <p>I am using this makefile to try and compile and run the program:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Finally, the code I am trying to compile is:</p>
  
  <pre><code>#include &lt;opencv2/objdetect/objdetect.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  #include &lt;opencv2/video/tracking.hpp&gt;
  #include &lt;opencv/cv.h&gt;
  #include &lt;opencv/cxcore.h&gt;
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  using namespace cv;
  
  
  int main(int argc, const char** argv){
      //storing the image in a temporary variable
      vector&lt;Mat&gt; img;
      int noi=5;
      for( int index=0; index&lt;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&lt;Point2f&gt; 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&lt;uchar&gt; status;
          vector&lt;float&gt; track_err;
          int maxlevel=3;
          Mat im2=img[1];
          TermCriteria termcrit(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS,20,.03);
          vector&lt;Point2f&gt; pointskept1,pointskept2;
          vector&lt;int&gt;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&lt;&lt;K&lt;&lt;endl;
  
          ofstream myfile;
      //collecting new images, determining corners, and calculating optical flow
      for (int i=1; i&lt;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&lt;status.size();i++){
              if(status[t] &amp;&amp; track_err[i]&lt;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&lt;&lt;"Rotation mat: ";
          for(int l=0;l&lt;R.rows;l++){
              for(int m=0; m&lt;R.cols; m++){
                  myfile&lt;&lt;R.at&lt;float&gt;(i,m)&lt;&lt;", ";
              }
          }
          myfile&lt;&lt;"Translation vector: ";
          for(int l=0; l&lt;tran.rows;l++){
              myfile&lt;&lt;tran.at&lt;float&gt;(l,1)&lt;&lt;", ";
          }
          myfile&lt;&lt;"\n";
          myfile.close();
  
      }
  
  
      return 0;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Thanks!
  Andrew</p>