metsburg
metsburg

Reputation: 2021

OpenCV Harris Corner Detection crashes

I'm trying to use Harris Corner detection algorithm of OpenCV to find corners in an image. I want to track it across consecutive frames using Lucas-Kanade Pyramidal Optical flow. I have this C++ code, which doesn't seem to work for some reason:

#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

void main()

{
    Mat img1, img2;
    Mat disp1, disp2;
int thresh = 200;

vector<Point2f> left_corners;
vector<Point2f> right_corners;
vector<unsigned char> status;
vector<float> error;

Size s;
s.height = 400;
s.width = 400;

img1 = imread("D:\\img_l.jpg",0);
img2 = imread("D:\\img_r.jpg",0);

resize(img2, img2, s, 0, 0, INTER_CUBIC);   
resize(img1, img1, s, 0, 0, INTER_CUBIC);


disp1 = Mat::zeros( img1.size(), CV_32FC1 );
disp2 = Mat::zeros( img2.size(), CV_32FC1 );

int blockSize = 2;
int apertureSize = 3;
double k = 0.04;

 cornerHarris( img1, disp1, blockSize, apertureSize, k, BORDER_DEFAULT );
  normalize( disp1, disp1, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );

  for( int j = 0; j < disp1.size().height ; j++ )
     { 
         for( int i = 0; i < disp1.size().width; i++ )
          {
            if( (int) disp1.at<float>(j,i) > thresh )
              { 
                left_corners.push_back(Point2f( j, i )); 
              }
          }
     }

  right_corners.resize(left_corners.size());

  calcOpticalFlowPyrLK(img1,img2,left_corners,right_corners,status,error, Size(11,11),5);     
  printf("Vector size : %d",left_corners.size());


  waitKey(0);
}

When I run it, I get the following error message:

Microsoft Visual Studio C Runtime Library has detected a fatal error in OpenCVTest.exe. (OpenCVTest being the name of my project)

OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in unknown function, file ..\..\OpenCV-2.3.0-win-src\OpenCV-2.3.0\modules\video\src\lkpyramid.cpp, line 71

I have been trying to debug this from yesterday, but in vain. Please help.

Upvotes: 1

Views: 1147

Answers (1)

Hannes Ovr&#233;n
Hannes Ovr&#233;n

Reputation: 21831

As we can see in the source code, this error is thrown if the previous points array is in someway faulty. Exactly what makes it bad is hard to say since the documentation for checkVector is a bit sketchy. You can still look at the code to find out. But my guess is that your left_corners variable have either the wrong type (not CV_32F) or the wrong shape.

Upvotes: 3

Related Questions