Opencv跟踪算法多线程共同使用,最终使用中值得到跟踪框

这里写自定义目录标题

  • Opencv跟踪算法综合

Opencv跟踪算法综合

本文主要实现opencv 自带跟踪算法多线程共同计算而产生最终的跟踪结果
(基于opencv342测试 opencv3 vs各版本下载地址: 链接: https://pan.baidu.com/s/1f5oAFqs-u15vkD5LNTcxtw 提取码: 2qj9)
多目标多线程更新的也发布了 地址:https://blog.csdn.net/weixin_37918890/article/details/107091504

结果示例:
在这里插入图片描述
代码:


#include 
#include 
#include 
#include 
using namespace cv;
using namespace std;

bool ok_BOOSTING =false;
bool ok_KCF = false;
bool ok_TLD = false;
bool ok_MEDIANFLOW = false;
bool ok_CSRT = false;

void BOOSTING_tracker(Ptr* tracker,Mat* frame, Rect2d *bbox) {
   
    ok_BOOSTING = (*tracker)->update(*frame, *bbox);
}


void KCF_tracker(Ptr* tracker, Mat* frame, Rect2d* bbox) {

    ok_KCF = (*tracker)->update(*frame, *bbox);
}
void TLD_tracker(Ptr* tracker, Mat* frame, Rect2d* bbox) {

    ok_TLD = (*tracker)->update(*frame, *bbox);
}
void MEDIANFLOW_tracker(Ptr* tracker, Mat* frame, Rect2d* bbox) {
    ok_MEDIANFLOW = (*tracker)->update(*frame, *bbox);

}
void CSRT_tracker(Ptr* tracker, Mat* frame, Rect2d* bbox) {

    ok_CSRT = (*tracker)->update(*frame, *bbox);
}


int main(int argc, char** argv)
{
    // List of tracker types in OpenCV 3.2

    //初始化六个跟踪器
    Ptr trackerBOOSTING;
    trackerBOOSTING = TrackerBoosting::create();
    Ptr trackerMIL;
    trackerMIL = TrackerMIL::create();
    Ptr trackerKCF;
    trackerKCF = TrackerKCF::create();
    Ptr trackerTLD;
    trackerTLD = TrackerTLD::create();
    Ptr trackerMEDIANFLOW;
    trackerMEDIANFLOW = TrackerMedianFlow::create();
    Ptr trackerCSRT;
    trackerCSRT = TrackerCSRT::create();

    //读取视频
    VideoCapture video("D:/testinput/fish6.mp4");
    if (!video.isOpened())
    {
        cout << "Could not read video file" << endl;
        return 1;

    }
    Mat frame;
    bool ok = video.read(frame);
    
    Rect2d bbox(287, 23, 86, 320);
    bbox = selectROI(frame, false);
    rectangle(frame, bbox, Scalar(255, 0, 0), 2, 1);
    Rect2d bboxBOOSTING;
    bboxBOOSTING = bbox;
    Rect2d bboxMIL;
    bboxMIL = bbox;
    Rect2d bboxKCF;
    bboxKCF = bbox;
    Rect2d bboxTLD;
    bboxTLD = bbox;
    Rect2d bboxMEDIANFLOW;
    bboxMEDIANFLOW = bbox;
    Rect2d bboxCSRT;
    bboxCSRT = bbox;


    //初始化六个跟踪器
    trackerBOOSTING->init(frame, bboxBOOSTING);
    trackerMIL->init(frame, bboxMIL);
    trackerKCF->init(frame, bboxKCF);
    trackerTLD->init(frame, bboxTLD);
    trackerMEDIANFLOW->init(frame, bboxMEDIANFLOW);
    trackerCSRT->init(frame, bboxCSRT);
    vector TrackerThread_;

    while (video.read(frame))
    {
        double timer = (double)getTickCount();
        TrackerThread_.push_back(thread(BOOSTING_tracker, &trackerBOOSTING, &frame, &bboxBOOSTING));
        TrackerThread_.push_back(thread(KCF_tracker, &trackerKCF, &frame, &bboxKCF));
        TrackerThread_.push_back(thread(TLD_tracker, &trackerTLD, &frame, &bboxTLD));
        TrackerThread_.push_back(thread(MEDIANFLOW_tracker, &trackerMEDIANFLOW, &frame, &bboxMEDIANFLOW));
        TrackerThread_.push_back(thread(CSRT_tracker, &trackerCSRT, &frame, &bboxCSRT));
        bool ok_MIL = trackerMIL->update(frame, bboxMIL);
        for (auto& ite : TrackerThread_) {
            ite.join();
        }
        TrackerThread_.clear();
        
        
        float fps = getTickFrequency() / ((double)getTickCount() - timer);
        vector BBoxs;
        if (ok_BOOSTING)
        {
            BBoxs.push_back(bboxBOOSTING);
        }
        if (ok_KCF)
        {
            BBoxs.push_back(bboxKCF);
        }
        if (ok_TLD)
        {
            BBoxs.push_back(bboxTLD);
        }
        if (ok_MEDIANFLOW)
        {
            BBoxs.push_back(bboxMEDIANFLOW);
        }
        if (ok_CSRT)
        {
            BBoxs.push_back(bboxCSRT);
        }
        if (ok_MIL)
        {
            BBoxs.push_back(bboxMIL);
        }
        vector xs; 
        vector ys;
        vector ws;
        vector hs;

        int size_used = BBoxs.size();
        int xtotal = 0,ytotal=0,wtotal=0,htotal=0;
        for (int index = 0; index < size_used; index++) {
            xs.push_back(BBoxs[index].x);
            ys.push_back(BBoxs[index].y);
            ws.push_back(BBoxs[index].width);
            hs.push_back(BBoxs[index].height);
            xtotal = xtotal + BBoxs[index].x;
            ytotal = ytotal + BBoxs[index].y;
            wtotal = wtotal + BBoxs[index].width;
            htotal = htotal + BBoxs[index].height;
        }
        xtotal = xtotal / size_used;
        ytotal = ytotal / size_used;
        wtotal = wtotal / size_used;
        htotal = htotal / size_used;
        sort(xs.begin(), xs.end());
        sort(ys.begin(), ys.end());
        sort(ws.begin(), ws.end());
        sort(hs.begin(), hs.end());
        int new_i;
        new_i = size_used / 2;
        Rect2d newbox(xs[new_i],ys[new_i], ws[new_i], hs[new_i]);
        //Rect2d newbox(xtotal, ytotal, wtotal, htotal);

        Mat frame_src = frame.clone();
        cout << "size_used:" << size_used << endl;
        if (size_used>0)
        {
            // Tracking success : Draw the tracked object
            rectangle(frame, newbox, Scalar(255, 0, 0), 2, 1);
        }
        else
        {
            // Tracking failure detected.
            putText(frame, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);
        }

        // Display tracker type on frame
        putText(frame, "TEST_fish Tracker", Point(100, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);

        // Display FPS on frame
        putText(frame, "FPS : " + to_string((int(fps))), Point(100, 50), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);

        // Display frame.
        imshow("TEST_fish Tracking", frame);

        Mat frameBOOSTING = frame_src.clone();
        if (ok_BOOSTING) {
            rectangle(frameBOOSTING, bboxBOOSTING, Scalar(255, 0, 0), 2, 1);

        }
        else {
            putText(frameBOOSTING, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);

        }
        putText(frameBOOSTING, "BOOSTING Tracker", Point(100, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);
        
        imshow("BOOSTING Tracking", frameBOOSTING);

        Mat frameKCF = frame_src.clone();
        if (ok_KCF) {
            rectangle(frameKCF, bboxKCF, Scalar(255, 0, 0), 2, 1);

        }
        else {
            putText(frameKCF, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);
           // trackerKCF.release();
            //trackerKCF=TrackerKCF::create();
           // trackerKCF->init(frame, newbox);
        }
        putText(frameKCF, "KCF Tracker", Point(100, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);

        imshow("KCF Tracking", frameKCF);


        Mat frameTLD = frame_src.clone();
        if (ok_TLD) {
            rectangle(frameTLD, bboxTLD, Scalar(255, 0, 0), 2, 1);

        }
        else {
            putText(frameTLD, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);

        }
        putText(frameTLD, "TLD Tracker", Point(100, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);

        imshow("TLD Tracking", frameTLD);

        Mat frameMEDIANFLOW = frame_src.clone();
        if (ok_MEDIANFLOW) {
            rectangle(frameMEDIANFLOW, bboxMEDIANFLOW, Scalar(255, 0, 0), 2, 1);

        }
        else {
            putText(frameMEDIANFLOW, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);

        }
        putText(frameMEDIANFLOW, "MEDIANFLOW Tracker", Point(100, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);

        imshow("MEDIANFLOW Tracking", frameMEDIANFLOW);

        Mat frameCSRT = frame_src.clone();
        if (ok_CSRT) {
            rectangle(frameCSRT, bboxCSRT, Scalar(255, 0, 0), 2, 1);

        }
        else {
            putText(frameCSRT, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);

        }
        putText(frameCSRT, "CSRT Tracker", Point(100, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);

        imshow("CSRT Tracking", frameCSRT);


        Mat frameMIL = frame_src.clone();
        if (ok_MIL) {
            rectangle(frameMIL, bboxMIL, Scalar(255, 0, 0), 2, 1);

        }
        else {
            putText(frameMIL, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);

        }
        putText(frameMIL, "MIL Tracker", Point(100, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);

        imshow("MIL Tracking", frameMIL);

        bboxBOOSTING = newbox;
        bboxKCF = newbox;
        bboxTLD = newbox;
        bboxMEDIANFLOW = newbox;
        bboxCSRT = newbox;
        bboxMIL = newbox;

        // Exit if ESC pressed.
        int k = waitKey(1);
        if (k == 27)
        {
            break;
        }
        
        //bool ok = tracker->update(frame, bbox);


    }


}



你可能感兴趣的:(OpenCv传统图像处理)