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

Posted

技术标签:

【中文标题】如何使用 dlib 跟踪 ROI 内的对象?【英文标题】:how to track objects within the ROI using dlib? 【发布时间】:2018-08-28 21:08:09 【问题描述】:

我正在尝试获取流并定义 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】:

您将 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 types 和 Correlation 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);

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

【讨论】:

感谢您的回答,但再次显示相同的错误 @JaySingh 你对所有 dlib 函数都这样做了吗? @JaySingh 这解决了您的问题吗? 是的,谢谢它对我有很大帮助,但我根据需要编辑了我的代码,现在它正在工作,但在我更新问题时跟踪对象,所以如果你有任何线索而不是评论。 @JaySingh 您现在遇到的问题与这个问题无关。请使用您正在使用的输入和完整示例创建一个新的。

以上是关于如何使用 dlib 跟踪 ROI 内的对象?的主要内容,如果未能解决你的问题,请参考以下文章

Opencv 对象跟踪和计数在视频帧中通过 ROI 的对象

尽管初始大小固定,但跟踪对象的 BoundingBox/ROI 的大小不断增加

如何使用openCV检测脸颊?

从 opencv 中的 kcf 跟踪中清除 ROI 历史记录

dlib,python:如何使整个头部(不仅是脸)变成正方形(不是矩形)?

如何使openCV背景减法KNN算法持续更长时间,跟踪一个没有移动的前景对象