【问题标题】:OpenCV Harris Corner Detection crashesOpenCV Harris 角点检测崩溃
【发布时间】:2015-02-03 21:39:39
【问题描述】:

我正在尝试使用 OpenCV 的 Harris Corner 检测算法来查找图像中的角点。我想使用Lucas-Kanade Pyramidal Optical flow 跨连续帧跟踪它。 我有这个 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 运行时库在 OpenCVTest.exe 中检测到致命错误。 (OpenCVTest 是我的项目名称)

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

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

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    正如我们在source code 中看到的,如果先前的点数组有某种错误,则会引发此错误。由于checkVector 的文档有点粗略,所以很难说到底是什么让它变得糟糕。您仍然可以查看the code 来找出答案。 但我的猜测是您的 left_corners 变量的类型错误(不是 CV_32F)或形状错误。

    【讨论】:

    • 好的。我知道错误是如何被抛出的。非常感激。但仍然不知道如何让它发挥作用。
    • 由于requireContiguous参数设置为True,可能是不接受传递Point2f的向量(出于某种原因)。
    • 如果“不接受传递Point2f的向量”,那么接受什么?
    • 我不知道这是否真的是问题所在。由于数据类型是InputArray,我希望您的数据能够正常工作。也许是时候启动调试器了。
    猜你喜欢
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2013-08-17
    • 2018-10-22
    • 2012-10-24
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多