c++ - 使用Tracker-API从网络摄像头跟踪对象,但未定义error:cap

标签 c++ opencv

我正在建立一个有关使用Tracker-API(3.3.1 OpenCV库)从网络摄像机跟踪对象的项目,但是在打开摄像机代码时仍然出现错误。如果我只运行开头的摄像机代码,就没有错误。 This is my source code

在我的源代码中,他们正在使用视频文件,但是在我的系统中,我想从网络摄像头实时跟踪对象。我的代码有问题吗?

#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <opencv2\core.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\tracking\tracker.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/utility.hpp>
#include <iostream>
#include <string>
#include <math.h>

using namespace cv;
using namespace std;

// Convert to string
#define SSTR( x ) static_cast< std::ostringstream & >( \
    (std::ostringstream() << std::dec << x)).str()

int main(int argc, char **argv)
{
    // List of tracker types in OpenCV 3.4.1
    string trackerTypes[5] = { "BOOSTING", "MIL", "KCF", "TLD", "MEDIANFLOW" };
    // vector <string> trackerTypes(types, std::end(types));

    // Create a tracker
    string trackerType = trackerTypes[0];

    Ptr<Tracker> tracker;
    if (trackerType == "BOOSTING")
        tracker = TrackerBoosting::create();
    if (trackerType == "MIL")
        tracker = TrackerMIL::create();
    if (trackerType == "KCF")
        tracker = TrackerKCF::create();
    if (trackerType == "TLD")
        tracker = TrackerTLD::create();
    if (trackerType == "MEDIANFLOW")


        // Read video
        VideoCapture cap(0); // open the default camera
    if (!cap.isOpened())  // check if we succeeded
    {
        cout << "Could not read video file" << endl;
        return 1;

    }

    Mat frame;
    bool ok = cap.read(frame);

    Rect2d bbox(287, 23, 86, 320);
    rectangle(frame, bbox, Scalar(255, 0, 0), 2, 1);

    imshow("Tracking", frame);
    tracker->init(frame, bbox);

    while (cap.read(frame)){
        double timer = (double)getTickCount();
        bool ok = tracker->update(frame, bbox);
        float fps = getTickFrequency() / ((double)getTickCount() - timer);
        if (ok){
            rectangle(frame, bbox, Scalar(255, 0, 0), 2, 1);
        }
        else{
            putText(frame, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_PLAIN, 0.75, Scalar(0, 0, 255), 2);
        }
        imshow("Tracking", frame);
        int k = waitKey(1);
        if (k == 27){
            break;
        }
    }
}

最佳答案

仅在跟踪器类型为中位数流量的情况下,才构造cap对象:

    if (trackerType == "MEDIANFLOW")


    // Read video
    VideoCapture cap(0); // open the default camera

也许你错过了
tracker = TrackerMedianFlow::create();

行在那里(在最后一个if之后)?

关于c++ - 使用Tracker-API从网络摄像头跟踪对象,但未定义error:cap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61421968/

相关文章:

c++ - 对 WinMain@16 C++、SDL-2 的 undefined reference

python - opencv::cuda GpuMat CV_8UC1到CV_32FC1转换黑色图像

opencv - 关于将 detect_markers.cpp 与 opencv aruco 一起使用的问题?

java - 如何更改图像位深度?

ios - 集成 OpenCV 框架的架构 armv7 的 undefined symbol

具有复杂内核的 OpenCV 卷积/filter2d

c++ - '创建指向非类类型成员的指针 'T*'' 编译器错误

c++ - 在Linux上为Windows构建GCC插件

c++ - 关于c/c++ Segmentation fault的问题

c++ - 除以 2 和 Sigsegv 错误时位移位的奇怪行为