qq_26508447 2015-11-19 02:31 采纳率: 0%
浏览 2035

VS2013运行opencv3自带的lkdemo例程报错。。。求助大神

运行opencv3自带的lkdemo例程,程序可以运行,但鼠标点选角点后,程序便会报错。下图是详细情况。源码如下:
#include "stdafx.h"
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"

#include
#include

using namespace cv;
using namespace std;

static void help()
{
// print a welcome message, and the OpenCV version
cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
"Using OpenCV version " << CV_VERSION << endl;
cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
cout << "\nHot keys: \n"
"\tESC - quit the program\n"
"\tr - auto-initialize tracking\n"
"\tc - delete all the points\n"
"\tn - switch the \"night\" mode on/off\n"
"To add/remove a feature point click it\n" << endl;
}

Point2f point;
bool addRemovePt = false;

static void onMouse(int event, int x, int y, int /*flags*/, void* /*param*/)
{
if (event == EVENT_LBUTTONDOWN)
{
point = Point2f((float)x, (float)y);
addRemovePt = true;
}
}

int main()
{
help();

VideoCapture cap;
cap.open("phone.mp4");
TermCriteria termcrit(TermCriteria::COUNT | TermCriteria::EPS, 20, 0.03);
Size subPixWinSize(10, 10), winSize(31, 31);

const int MAX_COUNT = 500;
bool needToInit = false;
bool nightMode = false;

if (!cap.isOpened())
{
    cout << "Could not initialize capturing...\n";
    return 0;
}

namedWindow("LK Demo", 1);
setMouseCallback("LK Demo", onMouse, 0);

Mat gray, prevGray, image, frame;
vector<Point2f> points[2];

for (;;)
{
    cap >> frame;
    if (frame.empty())
        break;

    frame.copyTo(image);
    cvtColor(image, gray, COLOR_BGR2GRAY);

    if (nightMode)
        image = Scalar::all(0);

    if (needToInit)
    {
        // automatic initialization
        goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
        cornerSubPix(gray, points[1], subPixWinSize, Size(-1, -1), termcrit);
        addRemovePt = false;
    }
    else if (!points[0].empty())
    {
        vector<uchar> status;
        vector<float> err;
        if (prevGray.empty())
            gray.copyTo(prevGray);
        calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
            3, termcrit, 0, 0.001);
        size_t i, k;
        for (i = k = 0; i < points[1].size(); i++)
        {
            if (addRemovePt)
            {
                if (norm(point - points[1][i]) <= 5)
                {
                    addRemovePt = false;
                    continue;
                }
            }

            if (!status[i])
                continue;

            points[1][k++] = points[1][i];
            circle(image, points[1][i], 3, Scalar(0, 255, 0), -1, 8);
        }
        points[1].resize(k);
    }

    if (addRemovePt && points[1].size() < (size_t)MAX_COUNT)
    {
        vector<Point2f> tmp;
        tmp.push_back(point);
        cornerSubPix(gray, tmp, winSize, Size(-1, -1), termcrit);
        points[1].push_back(tmp[0]);
        addRemovePt = false;
    }

    needToInit = false;
    imshow("LK Demo", image);

    char c = (char)waitKey(10);
    if (c == 27)
        break;
    switch (c)
    {
    case 'r':
        needToInit = true;
        break;
    case 'c':
        points[0].clear();
        points[1].clear();
        break;
    case 'n':
        nightMode = !nightMode;
        break;
    }

    std::swap(points[1], points[0]);
    cv::swap(prevGray, gray);
}

return 0;

}

程序死在了calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
3, termcrit, 0, 0.001);一句

  • 写回答

3条回答 默认 最新

  • qq_26508447 2015-11-19 02:33
    关注

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀