我正在C ++ Visual Studio 2015中使用OpenCV 3.4.1跟踪类。在OpenCv中可用的8个跟踪器中,我主要使用KCF,CSRT和MEDIANFLOW,这些跟踪算法的fps分别为30、4、4。我想知道是否可以更改这些帧的fps,以便更快地处理视频。我在这里做错了什么?还是有人知道如何将CSRT / MEDIANFLOW的fps提高到30(与KCF相同)?
在我的代码中,KCF比CSRT和MEDIANFLOW快,但是跟踪性能/准确性不如CSRT。但是KCF的处理速度确实很高,因为它的fps高。
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include <iostream>
#include <string>
#include <iomanip>
#include <map>
using namespace std;
using namespace cv;
vector<string> trackerTypes = { "BOOSTING", "MIL", "KCF", "TLD", "MEDIANFLOW", "GOTURN", "MOSSE", "CSRT" };
// create tracker by name
Ptr<Tracker> createTrackerByName(string trackerType)
{
Ptr<Tracker> tracker;
if (trackerType == trackerTypes[0])
tracker = TrackerBoosting::create();
else if (trackerType == trackerTypes[1])
tracker = TrackerMIL::create();
else if (trackerType == trackerTypes[2])
tracker = TrackerKCF::create();
else if (trackerType == trackerTypes[3])
tracker = TrackerTLD::create();
else if (trackerType == trackerTypes[4])
tracker = TrackerMedianFlow::create();
else if (trackerType == trackerTypes[5])
tracker = TrackerGOTURN::create();
else if (trackerType == trackerTypes[6])
tracker = TrackerMOSSE::create();
else if (trackerType == trackerTypes[7])
tracker = TrackerCSRT::create();
else {
cout << "Incorrect tracker name" << endl;
cout << "Available trackers are: " << endl;
for (vector<string>::iterator it = trackerTypes.begin(); it != trackerTypes.end(); ++it)
std::cout << " " << *it << endl;
}
return tracker;
}
int main(int argc, char * argv[])
{
const int N = 2;
double xcordinate = 0;
double ycordinate = 0;
double Xvalue = 0;
double Yvalue = 0;
double firstFrameY[10] = {};
double remainFrameY[10] = {};
double firstFrameX[10] = {};
double remainFrameX[10] = {};
string trackerType = "CSRT";
cout << "The Selected tracker is " << trackerType << endl;
string videoPath = "SS-100_random11sec_Trim.mp4";
vector<Rect> bboxes;
Mat frame;
cv::VideoCapture cap(videoPath);
cap >> frame;
bool showCrosshair = true;
bool fromCenter = false;
cout << "Enter the value of X-cordinate\n";
cin >> xcordinate;
cout << "Enter the value of Y-cordinate\n";
cin >> ycordinate;
cv::selectROIs("MultiTracker", frame, bboxes, showCrosshair, fromCenter);
if (bboxes.size() < 1)
return 0;
Ptr<MultiTracker> multiTracker = cv::MultiTracker::create();
for (int i = 0; i < bboxes.size(); i++)
multiTracker->add(createTrackerByName(trackerType), frame, Rect2d(bboxes[i]));
cout << "Started tracking, press ESC to quit." << endl;
int m = 0;
while (cap.isOpened())
{
cap >> frame;
if (frame.empty()) break;
multiTracker->update(frame);
for (unsigned i = 0; i < multiTracker->getObjects().size(); i++)
{
rectangle(frame, multiTracker->getObjects()[i], colors[i], 2, 1);
bboxes[i] = multiTracker->getObjects()[i];
//cout << "The Co-ordinates: " << bboxes[i] << endl;
outputFile << std::left << bboxes[i].x <<',' << std::right << bboxes[i].y << '\n';
}
if (m == 0) //for saving the reference values
{
for (int i = 0; i< N; i++)
{
firstFrameY[i] = bboxes[i].y;
remainFrameY[i] = bboxes[i].y;
firstFrameX[i] = bboxes[i].x;
remainFrameX[i] = bboxes[i].x;
}
m++;
}
else
{
//get the y values into an array and pass it below
for (int i = 0; i < N; i++)
{
remainFrameY[i] = bboxes[i].y;
remainFrameX[i] = bboxes[i].x;
}
}
Xvalue = getTheEquationX(firstFrameX, remainFrameX, xcordinate, TrackedX);
Yvalue = getTheEquationY(firstFrameY, remainFrameY, ycordinate, TrackedY);
cropObject(Xvalue, Yvalue, frame, count_4);
resize(frame, frame, Size(1280, 720), 0, 0, INTER_CUBIC);
imshow("MultiTracker", frame);
if (waitKey(1) == 27) break;
}