我正在建立一个有关使用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/