OpenCV Harris Corner Detection crashes

OpenCV Harris Corner Detection crashes

本文关键字:crashes Detection Corner Harris OpenCV      更新时间:2023-10-16

我正在尝试使用OpenCV的Harris Corner检测算法来查找图像中的角落。我想使用 Lucas-Kanade 金字塔光流跨连续帧跟踪它。我有这个C++代码,由于某种原因似乎不起作用:

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

当我运行它时,我收到以下错误消息:

Microsoft Visual Studio C Runtime Library 在 OpenCVTest.exe 中检测到致命错误。(OpenCVTest是我的项目名称)

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

我从昨天开始一直在尝试调试它,但徒劳无功。请帮忙。

正如我们在源代码中看到的,如果前面的点数组以某种方式出错,则会引发此错误。很难说究竟是什么让它变得糟糕,因为checkVector的文档有点粗略。您仍然可以查看代码以找出答案。但我的猜测是,您的left_corners变量具有错误的类型(不是CV_32F)或错误的形状。