0

I already wrote a program that detects different objects, and i'm now working on a tracking module to track the objects of interest. Because the detection isn't as fast, I'm hoping to pass in one frame with the detected objects about every 10-30 frames, and have the tracking module use camshift + kalman filtering to track the objects in the video stream until another new image with the detected objects is received from the detection module.

I'm pretty new to boost and c++ multi-threading in general and I wrote the following code just to see if I can be passing in frames from the detection module to the tracking module. For some reason, everything just freezes after the tracking module has received two imags. Any ideas why? Is there a better way to go about this? Thanks!

Main Thread (Detection module might work in a similar manner)

int main(int argc, char *argv[]){
    cout << "main started" << endl;

    Tracker tracker("Tracker");
    tracker.start(imread("/home/cosyco/Desktop/images/lambo1.jpeg", CV_LOAD_IMAGE_COLOR));

    boost::posix_time::seconds sleepTime(5);

    cout << "main starting sleep" << endl;
    boost::this_thread::sleep(sleepTime);
    cout << "main just woke up, switching image" << endl;
    tracker.resetImage(imread("/home/cosyco/Desktop/images/lambo2.jpeg", CV_LOAD_IMAGE_COLOR));
    cout << "main sleeping second time" << endl;
    boost::this_thread::sleep(sleepTime);
    cout << "main just woke up, switching image" << endl;
    tracker.resetImage(imread("/home/cosyco/Desktop/images/lambo3.jpeg", CV_LOAD_IMAGE_COLOR));
    cout << "main sleeping third time" << endl;
    boost::this_thread::sleep(sleepTime);
    cout << "main just woke up, switching image" << endl;
    tracker.resetImage(imread("/home/cosyco/Desktop/images/lambo4.jpeg", CV_LOAD_IMAGE_COLOR));
    tracker.join();



    cout << "main done" << endl;
    return 0;
}

Tracker:

#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <opencv2/opencv.hpp>


using namespace std;
using namespace cv;

class Tracker{

private:
    string name;
    Mat trackerImage;
    boost::thread trackerThread;


public:
    Tracker (string newName){
        cout << "creating tracker : " << newName << "created" << endl;
        name = newName;
    }


    void track(Mat image){
        image.copyTo(trackerImage);
        informUpdate();
        // use this new image to generate a histogram for camshift on the next
        //few images in the video stream before a new image with the detected object is received


        for (; ; ){
            // cout << "tracking" << endl;
        }

    }

    void start(Mat image){
        cout << "in tracker's start" << endl;
        // trackerThread = boost::thread(&Tracker::track, this, trackLimit);
        trackerThread = boost::thread(&Tracker::track, this, image);
    }


    void join(){
        trackerThread.join();
    }


    void resetImage(Mat image){
        track(image);
    }


    void informUpdate(){
        cout << "\033[1;31m image updated \033[0m" << endl;
    }
};
Stralo
  • 474
  • 4
  • 16
  • Are you absolutely sure the hang up is not from your tracking section? – a-Jays Aug 11 '14 at 11:34
  • @a-Jays - I'm not actually doing much in the tracking section a the moment though. right now it's just the for loop in the track() method. It's within that method that I will be doing camshift on every frame from the video stream until I get a new frame with the detected object to generate a new histogram for further camshift-ing. – Stralo Aug 11 '14 at 12:14
  • that `for` loop doesn't seem to be exiting. In which case your reset image update will probably not happen. So you'll be calling `track()` again when one instance of it is already running? – a-Jays Aug 11 '14 at 13:01
  • @a-Jays - That makes a lot of sense. So if I'm going to be continuously receiving images from the live feed and using camshift to track an object of interest based on a histogram I regenerate say every 10 frames, do you have any idea how I could stop wherever I might be in the camshift process and recalibrate my histogram when the detection module is ready with a new image? something like a listener to get fired when the detection module is ready and cancel whatever for loop i might be in. Thanks! – Stralo Aug 11 '14 at 13:35
  • One ugly hack I can immediately think of is to set/reset a shared `bool` when an object is updated, and check the status of this variable inside the loop. There are better ways of doing this, but I'm presently not in a position to expound. I could, if it can wait. Sorry. – a-Jays Aug 14 '14 at 08:49
  • thanks a lot for your help, a-Jays. I was able to get it working with something similar to your suggestion. – Stralo Aug 14 '14 at 23:02

0 Answers0