如何使用dlib跟踪ROI中的对象?

时间:2018-03-20 06:32:51

标签: opencv c++11 object tracking dlib

我正在尝试获取流并定义ROI并使用dlib相关跟踪器跟踪ROI中的对象,但编译时出错,未找到任何可描述此错误解决方案的代码或说明。提前谢谢。

cvcap.cpp

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
#include "dlib/dir_nav.h"

using namespace dlib;
using namespace std;

int main(int argc, char * argv[]){

    CvCapture* camera = cvCaptureFromFile(argv[1]);

    if (camera==NULL)
        printf("camera is null\n");
    else
        printf("camera is not null\n");

    cvNamedWindow("CvCapture");
    while (cvWaitKey(1000)!=atoi("q")){
        double t1 = (double)cvGetTickCount();
        IplImage *img = cvQueryFrame(camera);

        cvSetImageROI(img, cvRect(140, 150, 340, 150));

        IplImage *img1 = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
        cvCopy(img, img1, NULL);
        cvResetImageROI(img);

        cvShowImage("ROI",img1);

        correlation_tracker tracker;
        tracker.start_track(img1, centered_rect(point(120,100), 80, 150));

        image_window win;
        tracker.update(img1);

        win.set_image(img1); 
        win.clear_overlay(); 
        win.add_overlay(tracker.get_position());

        cout << "hit enter to process next frame" << endl;
        cin.get();

        double t2=(double)cvGetTickCount();
        printf("time: %gms  fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
        cvShowImage("CvCapture",img1);
    }
    cvReleaseCapture(&camera);
}

错误是:

In file included from dlib/image_processing.h:24:0,
                 from cvcap.cpp:3:
dlib/image_processing/correlation_tracker.h: In instantiation of ‘dlib::point_transform_affine dlib::correlation_tracker::make_chip(const image_type&, 
dlib::drectangle, std::vector<dlib::matrix<std::complex<double> > >&) const [with image_type = _IplImage*]’:
dlib/image_processing/correlation_tracker.h:65:47:   required from ‘void dlib::correlation_tracker::start_track(const image_type&, const dlib::drectang
le&) [with image_type = _IplImage*]’
cvcap.cpp:36:70:   required from here
dlib/image_processing/correlation_tracker.h:301:67: error: invalid use of incomplete type ‘struct dlib::image_traits<_IplImage*>’
             typedef typename image_traits<image_type>::pixel_type pixel_type;

更新

我编辑了我的代码,现在它正在编译,但是当我运行我的代码时,会出现跟踪框,但它没有跟踪对象。

for(;;) {
        if(!vcap.read(image)) {
            std::cout << "No frame" << std::endl;
            break;
        } else {
        std::cout << "Starting" << std::endl;
            array2d<rgb_pixel> img;
            assign_image(img, cv_image<rgb_pixel>(image));

            tracker.start_track(img, centered_rect(point(413,260), 98, 126));

            for (unsigned long i = 0; i < img.size(); i++) {
                tracker.update(img);
                win.set_image(img); 
                win.clear_overlay(); 
                win.add_overlay(tracker.get_position());
            }
    } 
  } 

1 个答案:

答案 0 :(得分:1)

您正在将IplImage传递给dlib例程。但是如果你看一下docs它说你必须将你的opencv图像转换为cv_images,然后再将它们传递给dlib函数。你可以看到这个错误指出IplImage没有pixel_type特征。修复就是。

IplImage *img1 = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
...
correlation_tracker tracker;
// Not sure if  the tracker wants a single channel image
// change rgb_alpha_pixel to uchar in that case.
tracker.start_track(dlib::cv_image<dlib::rgb_alpha_pixel>(img1), centered_rect(point(120,100), 80, 150));

Pixel typesCorrelation tracker

编辑:将你的东西传递给opencv 3,这正在为我编译。你真的需要使用opencv 2吗?

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
#include "dlib/dir_nav.h"
#include <dlib/opencv/cv_image.h>

using namespace cv;
using namespace dlib;
using namespace std;

int main(int argc, char * argv[]){
    VideoCapture cap(0);
    Mat frame;
    while (cap.read(frame)) {
        cvtColor(frame, frame, CV_RGB2GRAY);
        cv_image<unsigned char> img(frame);
        correlation_tracker tracker;
        tracker.start_track(img, centered_rect(point(120,100), 80, 150));

        image_window win;
        tracker.update(img);

        win.set_image(img);
        win.clear_overlay();
        win.add_overlay(tracker.get_position());

        cout << "hit enter to process next frame" << endl;
        cv::waitKey(0);
    }
}

和丑陋的opencv 2 ..

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
#include "dlib/dir_nav.h"
#include <dlib/opencv/cv_image.h>

using namespace cv;
using namespace dlib;
using namespace std;

int main(int argc, char * argv[]){
    CvCapture* camera = cvCaptureFromFile(argv[1]);

    if (camera==NULL)
        printf("camera is null\n");
    else
        printf("camera is not null\n");

    cvNamedWindow("CvCapture");
    while (cvWaitKey(1000)!=atoi("q")) {
        double t1 = (double)cvGetTickCount();
        IplImage *img = cvQueryFrame(camera);

        cvSetImageROI(img, cvRect(140, 150, 340, 150));

        IplImage *img1 = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
        cvCopy(img, img1, NULL);
        cvResetImageROI(img);

        cvShowImage("ROI",img1);

        cvCvtColor(img1, img1, CV_RGB2GRAY);
        cv_image<unsigned char> img2(img1);
        correlation_tracker tracker;
        tracker.start_track(img2, centered_rect(point(120,100), 80, 150));

        image_window win;
        tracker.update(img2);

        win.set_image(img2);
        win.clear_overlay();
        win.add_overlay(tracker.get_position());

        cout << "hit enter to process next frame" << endl;
        cin.get();

        double t2=(double)cvGetTickCount();
        printf("time: %gms  fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
        cvShowImage("CvCapture",img1);
    }
    cvReleaseCapture(&camera);
}

正如您所看到的,错误完全来自我之前告诉您的内容。