1 #if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID)
2 #include "opencv2/contrib/detection_based_tracker.hpp"
7 #include <android/log.h>
8 #define LOG_TAG "OBJECT_DETECTOR"
9 #define LOGD0(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
10 #define LOGI0(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
11 #define LOGW0(...) ((void)__android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
12 #define LOGE0(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
17 #define LOGD0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
18 #define LOGI0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
19 #define LOGW0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
20 #define LOGE0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
24 #define LOGD(_str, ...) LOGD0(_str , ## __VA_ARGS__)
25 #define LOGI(_str, ...) LOGI0(_str , ## __VA_ARGS__)
26 #define LOGW(_str, ...) LOGW0(_str , ## __VA_ARGS__)
27 #define LOGE(_str, ...) LOGE0(_str , ## __VA_ARGS__)
29 #define LOGD(...) do{} while(0)
30 #define LOGI(...) do{} while(0)
31 #define LOGW(...) do{} while(0)
32 #define LOGE(...) do{} while(0)
39 static inline cv::Point2f centerRect(const cv::Rect& r)
41 return cv::Point2f(r.x+((float)r.width)/2, r.y+((float)r.height)/2);
43 static inline cv::Rect scale_rect(const cv::Rect& r, float scale)
45 cv::Point2f m=centerRect(r);
46 float width = r.width * scale;
47 float height = r.height * scale;
48 int x=cvRound(m.x - width/2);
49 int y=cvRound(m.y - height/2);
51 return cv::Rect(x, y, cvRound(width), cvRound(height));
54 void* workcycleObjectDetectorFunction(void* p);
55 class DetectionBasedTracker::SeparateDetectionWork
58 SeparateDetectionWork(DetectionBasedTracker& _detectionBasedTracker, const std::string& cascadeFilename);
59 virtual ~SeparateDetectionWork();
60 bool communicateWithDetectingThread(const Mat& imageGray, vector<Rect>& rectsWhereRegions);
64 inline bool isWorking()
66 return (stateThread==STATE_THREAD_WORKING_SLEEPING) || (stateThread==STATE_THREAD_WORKING_WITH_IMAGE);
70 pthread_mutex_lock(&mutex);
74 pthread_mutex_unlock(&mutex);
79 DetectionBasedTracker& detectionBasedTracker;
80 cv::CascadeClassifier cascadeInThread;
82 pthread_t second_workthread;
83 pthread_mutex_t mutex;
84 pthread_cond_t objectDetectorRun;
85 pthread_cond_t objectDetectorThreadStartStop;
87 std::vector<cv::Rect> resultDetect;
88 volatile bool isObjectDetectingReady;
89 volatile bool shouldObjectDetectingResultsBeForgot;
91 enum StateSeparatedThread {
92 STATE_THREAD_STOPPED=0,
93 STATE_THREAD_WORKING_SLEEPING,
94 STATE_THREAD_WORKING_WITH_IMAGE,
98 volatile StateSeparatedThread stateThread;
100 cv::Mat imageSeparateDetecting;
102 void workcycleObjectDetector();
103 friend void* workcycleObjectDetectorFunction(void* p);
105 long long timeWhenDetectingThreadStartedWork;
108 DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork(DetectionBasedTracker& _detectionBasedTracker, const std::string& cascadeFilename)
109 :detectionBasedTracker(_detectionBasedTracker),
110 cascadeInThread(cascadeFilename),
111 isObjectDetectingReady(false),
112 shouldObjectDetectingResultsBeForgot(false),
113 stateThread(STATE_THREAD_STOPPED),
114 timeWhenDetectingThreadStartedWork(-1)
117 res=pthread_mutex_init(&mutex, NULL);//TODO: should be attributes?
119 LOGE("ERROR in DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork in pthread_mutex_init(&mutex, NULL) is %d", res);
120 throw(std::exception());
122 res=pthread_cond_init (&objectDetectorRun, NULL);
124 LOGE("ERROR in DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork in pthread_cond_init(&objectDetectorRun,, NULL) is %d", res);
125 pthread_mutex_destroy(&mutex);
126 throw(std::exception());
128 res=pthread_cond_init (&objectDetectorThreadStartStop, NULL);
130 LOGE("ERROR in DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork in pthread_cond_init(&objectDetectorThreadStartStop,, NULL) is %d", res);
131 pthread_cond_destroy(&objectDetectorRun);
132 pthread_mutex_destroy(&mutex);
133 throw(std::exception());
137 DetectionBasedTracker::SeparateDetectionWork::~SeparateDetectionWork()
139 if(stateThread!=STATE_THREAD_STOPPED) {
140 LOGE("\n\n\nATTENTION!!! dangerous algorithm error: destructor DetectionBasedTracker::DetectionBasedTracker::~SeparateDetectionWork is called before stopping the workthread");
143 pthread_cond_destroy(&objectDetectorThreadStartStop);
144 pthread_cond_destroy(&objectDetectorRun);
145 pthread_mutex_destroy(&mutex);
147 bool DetectionBasedTracker::SeparateDetectionWork::run()
149 LOGD("DetectionBasedTracker::SeparateDetectionWork::run() --- start");
150 pthread_mutex_lock(&mutex);
151 if (stateThread != STATE_THREAD_STOPPED) {
152 LOGE("DetectionBasedTracker::SeparateDetectionWork::run is called while the previous run is not stopped");
153 pthread_mutex_unlock(&mutex);
156 stateThread=STATE_THREAD_WORKING_SLEEPING;
157 pthread_create(&second_workthread, NULL, workcycleObjectDetectorFunction, (void*)this); //TODO: add attributes?
158 pthread_cond_wait(&objectDetectorThreadStartStop, &mutex);
159 pthread_mutex_unlock(&mutex);
160 LOGD("DetectionBasedTracker::SeparateDetectionWork::run --- end");
165 #define CATCH_ALL_AND_LOG(_block) \
171 catch(cv::Exception& e) { \
172 LOGE0("\n %s: ERROR: OpenCV Exception caught: \n'%s'\n\n", __func__, e.what()); \
173 } catch(std::exception& e) { \
174 LOGE0("\n %s: ERROR: Exception caught: \n'%s'\n\n", __func__, e.what()); \
176 LOGE0("\n %s: ERROR: UNKNOWN Exception caught\n\n", __func__); \
180 #define CATCH_ALL_AND_LOG(_block) \
186 catch(cv::Exception& e) { \
187 LOGE0("\n ERROR: OpenCV Exception caught: \n'%s'\n\n", e.what()); \
188 } catch(std::exception& e) { \
189 LOGE0("\n ERROR: Exception caught: \n'%s'\n\n", e.what()); \
191 LOGE0("\n ERROR: UNKNOWN Exception caught\n\n"); \
196 void* workcycleObjectDetectorFunction(void* p)
198 CATCH_ALL_AND_LOG({ ((DetectionBasedTracker::SeparateDetectionWork*)p)->workcycleObjectDetector(); });
200 ((DetectionBasedTracker::SeparateDetectionWork*)p)->stateThread=DetectionBasedTracker::SeparateDetectionWork::STATE_THREAD_STOPPED;
202 LOGE0("DetectionBasedTracker: workcycleObjectDetectorFunction: ERROR concerning pointer, received as the function parameter");
207 void DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector()
209 static double freq = getTickFrequency();
210 LOGD0("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- start");
211 vector<Rect> objects;
213 CV_Assert(stateThread==STATE_THREAD_WORKING_SLEEPING);
214 pthread_mutex_lock(&mutex);
216 pthread_cond_signal(&objectDetectorThreadStartStop);
218 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- before waiting");
219 CV_Assert(stateThread==STATE_THREAD_WORKING_SLEEPING);
220 pthread_cond_wait(&objectDetectorRun, &mutex);
222 stateThread=STATE_THREAD_WORKING_WITH_IMAGE;
224 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- after waiting");
226 pthread_mutex_unlock(&mutex);
228 bool isFirstStep=true;
230 isObjectDetectingReady=false;
234 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- next step");
237 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- before waiting");
238 CV_Assert(stateThread==STATE_THREAD_WORKING_SLEEPING);
240 pthread_mutex_lock(&mutex);
241 CV_Assert(stateThread==STATE_THREAD_WORKING_SLEEPING);
242 pthread_cond_wait(&objectDetectorRun, &mutex);
244 stateThread=STATE_THREAD_WORKING_WITH_IMAGE;
246 pthread_mutex_unlock(&mutex);
248 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- after waiting");
254 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- go out from the workcycle just after waiting");
259 if (imageSeparateDetecting.empty()) {
260 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- imageSeparateDetecting is empty, continue");
263 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- start handling imageSeparateDetecting, img.size=%dx%d, img.data=0x%p",
264 imageSeparateDetecting.size().width, imageSeparateDetecting.size().height, (void*)imageSeparateDetecting.data);
267 int64 t1_detect=getTickCount();
269 int minObjectSize=detectionBasedTracker.parameters.minObjectSize;
270 Size min_objectSize=Size(minObjectSize, minObjectSize);
272 int maxObjectSize=detectionBasedTracker.parameters.maxObjectSize;
273 Size max_objectSize(maxObjectSize, maxObjectSize);
276 cascadeInThread.detectMultiScale( imageSeparateDetecting, objects,
277 detectionBasedTracker.parameters.scaleFactor, detectionBasedTracker.parameters.minNeighbors, 0
283 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- end handling imageSeparateDetecting");
286 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- go out from the workcycle just after detecting");
290 int64 t2_detect=getTickCount();
291 int64 dt_detect=t2_detect-t1_detect;
292 double dt_detect_ms=((double)dt_detect)/freq * 1000.0;
294 LOGI("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- objects num==%d, t_ms=%.4f", (int)objects.size(), dt_detect_ms);
296 pthread_mutex_lock(&mutex);
297 if (!shouldObjectDetectingResultsBeForgot) {
298 resultDetect=objects;
299 isObjectDetectingReady=true;
300 } else { //shouldObjectDetectingResultsBeForgot==true
301 resultDetect.clear();
302 isObjectDetectingReady=false;
303 shouldObjectDetectingResultsBeForgot=false;
306 stateThread=STATE_THREAD_WORKING_SLEEPING;
308 pthread_mutex_unlock(&mutex);
311 }// while(isWorking())
314 pthread_mutex_lock(&mutex);
316 stateThread=STATE_THREAD_STOPPED;
318 isObjectDetectingReady=false;
319 shouldObjectDetectingResultsBeForgot=false;
321 pthread_cond_signal(&objectDetectorThreadStartStop);
323 pthread_mutex_unlock(&mutex);
325 LOGI("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector: Returning");
328 void DetectionBasedTracker::SeparateDetectionWork::stop()
330 //FIXME: TODO: should add quickStop functionality
331 pthread_mutex_lock(&mutex);
333 pthread_mutex_unlock(&mutex);
334 LOGE("SimpleHighguiDemoCore::stop is called but the SimpleHighguiDemoCore pthread is not active");
337 stateThread=STATE_THREAD_STOPPING;
338 LOGD("DetectionBasedTracker::SeparateDetectionWork::stop: before going to sleep to wait for the signal from the workthread");
339 pthread_cond_signal(&objectDetectorRun);
340 pthread_cond_wait(&objectDetectorThreadStartStop, &mutex);
341 LOGD("DetectionBasedTracker::SeparateDetectionWork::stop: after receiving the signal from the workthread, stateThread=%d", (int)stateThread);
342 pthread_mutex_unlock(&mutex);
345 void DetectionBasedTracker::SeparateDetectionWork::resetTracking()
347 LOGD("DetectionBasedTracker::SeparateDetectionWork::resetTracking");
348 pthread_mutex_lock(&mutex);
350 if (stateThread == STATE_THREAD_WORKING_WITH_IMAGE) {
351 LOGD("DetectionBasedTracker::SeparateDetectionWork::resetTracking: since workthread is detecting objects at the moment, we should make cascadeInThread stop detecting and forget the detecting results");
352 shouldObjectDetectingResultsBeForgot=true;
353 //cascadeInThread.setStopFlag();//FIXME: TODO: this feature also should be contributed to OpenCV
355 LOGD("DetectionBasedTracker::SeparateDetectionWork::resetTracking: since workthread is NOT detecting objects at the moment, we should NOT make any additional actions");
358 resultDetect.clear();
359 isObjectDetectingReady=false;
362 pthread_mutex_unlock(&mutex);
366 bool DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThread(const Mat& imageGray, vector<Rect>& rectsWhereRegions)
368 static double freq = getTickFrequency();
370 bool shouldCommunicateWithDetectingThread=(stateThread==STATE_THREAD_WORKING_SLEEPING);
371 LOGD("DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThread: shouldCommunicateWithDetectingThread=%d", (shouldCommunicateWithDetectingThread?1:0));
373 if (!shouldCommunicateWithDetectingThread) {
377 bool shouldHandleResult=false;
378 pthread_mutex_lock(&mutex);
380 if (isObjectDetectingReady) {
381 shouldHandleResult=true;
382 rectsWhereRegions=resultDetect;
383 isObjectDetectingReady=false;
385 double lastBigDetectionDuration=1000.0 * (((double)(getTickCount() - timeWhenDetectingThreadStartedWork )) / freq);
386 LOGD("DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThread: lastBigDetectionDuration=%f ms", (double)lastBigDetectionDuration);
389 bool shouldSendNewDataToWorkThread=true;
390 if (timeWhenDetectingThreadStartedWork > 0) {
391 double time_from_previous_launch_in_ms=1000.0 * (((double)(getTickCount() - timeWhenDetectingThreadStartedWork )) / freq); //the same formula as for lastBigDetectionDuration
392 shouldSendNewDataToWorkThread = (time_from_previous_launch_in_ms >= detectionBasedTracker.parameters.minDetectionPeriod);
393 LOGD("DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThread: shouldSendNewDataToWorkThread was 1, now it is %d, since time_from_previous_launch_in_ms=%.2f, minDetectionPeriod=%d",
394 (shouldSendNewDataToWorkThread?1:0), time_from_previous_launch_in_ms, detectionBasedTracker.parameters.minDetectionPeriod);
397 if (shouldSendNewDataToWorkThread) {
399 imageSeparateDetecting.create(imageGray.size(), CV_8UC1);
401 imageGray.copyTo(imageSeparateDetecting);//may change imageSeparateDetecting ptr. But should not.
404 timeWhenDetectingThreadStartedWork = getTickCount() ;
406 pthread_cond_signal(&objectDetectorRun);
409 pthread_mutex_unlock(&mutex);
410 LOGD("DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThread: result: shouldHandleResult=%d", (shouldHandleResult?1:0));
412 return shouldHandleResult;
415 DetectionBasedTracker::Parameters::Parameters()
418 maxObjectSize=INT_MAX;
422 minDetectionPeriod=0;
427 DetectionBasedTracker::InnerParameters::InnerParameters()
429 numLastPositionsToTrack=4;
430 numStepsToWaitBeforeFirstShow=6;
431 numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown=3;
432 numStepsToShowWithoutDetecting=3;
434 coeffTrackingWindowSize=2.0;
435 coeffObjectSizeToTrack=0.85;
436 coeffObjectSpeedUsingInPrediction=0.8;
439 DetectionBasedTracker::DetectionBasedTracker(const std::string& cascadeFilename, const Parameters& params)
440 :separateDetectionWork(),
443 cascadeForTracking(cascadeFilename)
445 CV_Assert( (params.minObjectSize > 0)
446 && (params.maxObjectSize >= 0)
447 && (params.scaleFactor > 1.0)
448 && (params.maxTrackLifetime >= 0) );
452 separateDetectionWork=new SeparateDetectionWork(*this, cascadeFilename);
454 weightsPositionsSmoothing.push_back(1);
455 weightsSizesSmoothing.push_back(0.5);
456 weightsSizesSmoothing.push_back(0.3);
457 weightsSizesSmoothing.push_back(0.2);
460 DetectionBasedTracker::~DetectionBasedTracker()
466 void DetectionBasedTracker::process(const Mat& imageGray)
469 CV_Assert(imageGray.type()==CV_8UC1);
471 if (!separateDetectionWork->isWorking()) {
472 separateDetectionWork->run();
475 static double freq = getTickFrequency();
476 static long long time_when_last_call_started=getTickCount();
479 double delta_time_from_prev_call=1000.0 * (((double)(getTickCount() - time_when_last_call_started)) / freq);
480 LOGD("DetectionBasedTracker::process: time from the previous call is %f ms", (double)delta_time_from_prev_call);
481 time_when_last_call_started=getTickCount();
484 Mat imageDetect=imageGray;
486 int D=parameters.minObjectSize;
490 vector<Rect> rectsWhereRegions;
491 bool shouldHandleResult=separateDetectionWork->communicateWithDetectingThread(imageGray, rectsWhereRegions);
495 if (shouldHandleResult) {
496 LOGD("DetectionBasedTracker::process: get _rectsWhereRegions were got from resultDetect");
498 LOGD("DetectionBasedTracker::process: get _rectsWhereRegions from previous positions");
499 for(size_t i=0; i < trackedObjects.size(); i++) {
500 int n=trackedObjects[i].lastPositions.size();
503 Rect r=trackedObjects[i].lastPositions[n-1];
505 LOGE("DetectionBasedTracker::process: ERROR: ATTENTION: strange algorithm's behavior: trackedObjects[i].rect() is empty");
510 //correction by speed of rectangle
512 Point2f center=centerRect(r);
513 Point2f center_prev=centerRect(trackedObjects[i].lastPositions[n-2]);
514 Point2f shift=(center - center_prev) * innerParameters.coeffObjectSpeedUsingInPrediction;
516 r.x+=cvRound(shift.x);
517 r.y+=cvRound(shift.y);
521 rectsWhereRegions.push_back(r);
524 LOGI("DetectionBasedTracker::process: tracked objects num==%d", (int)trackedObjects.size());
526 vector<Rect> detectedObjectsInRegions;
528 LOGD("DetectionBasedTracker::process: rectsWhereRegions.size()=%d", (int)rectsWhereRegions.size());
529 for(size_t i=0; i < rectsWhereRegions.size(); i++) {
530 Rect r=rectsWhereRegions[i];
532 detectInRegion(imageDetect, r, detectedObjectsInRegions);
534 LOGD("DetectionBasedTracker::process: detectedObjectsInRegions.size()=%d", (int)detectedObjectsInRegions.size());
536 updateTrackedObjects(detectedObjectsInRegions);
539 void DetectionBasedTracker::getObjects(std::vector<cv::Rect>& result) const
543 for(size_t i=0; i < trackedObjects.size(); i++) {
544 Rect r=calcTrackedObjectPositionToShow(i);
549 LOGD("DetectionBasedTracker::process: found a object with SIZE %d x %d, rect={%d, %d, %d x %d}", r.width, r.height, r.x, r.y, r.width, r.height);
552 void DetectionBasedTracker::getObjects(std::vector<Object>& result) const
556 for(size_t i=0; i < trackedObjects.size(); i++) {
557 Rect r=calcTrackedObjectPositionToShow(i);
561 result.push_back(Object(r, trackedObjects[i].id));
562 LOGD("DetectionBasedTracker::process: found a object with SIZE %d x %d, rect={%d, %d, %d x %d}", r.width, r.height, r.x, r.y, r.width, r.height);
568 bool DetectionBasedTracker::run()
570 return separateDetectionWork->run();
573 void DetectionBasedTracker::stop()
575 separateDetectionWork->stop();
578 void DetectionBasedTracker::resetTracking()
580 separateDetectionWork->resetTracking();
581 trackedObjects.clear();
584 void DetectionBasedTracker::updateTrackedObjects(const vector<Rect>& detectedObjects)
588 INTERSECTED_RECTANGLE=-2
591 int N1=trackedObjects.size();
592 int N2=detectedObjects.size();
593 LOGD("DetectionBasedTracker::updateTrackedObjects: N1=%d, N2=%d", N1, N2);
595 for(int i=0; i < N1; i++) {
596 trackedObjects[i].numDetectedFrames++;
599 vector<int> correspondence(detectedObjects.size(), NEW_RECTANGLE);
600 correspondence.clear();
601 correspondence.resize(detectedObjects.size(), NEW_RECTANGLE);
603 for(int i=0; i < N1; i++) {
604 LOGD("DetectionBasedTracker::updateTrackedObjects: i=%d", i);
605 TrackedObject& curObject=trackedObjects[i];
610 int numpositions=curObject.lastPositions.size();
611 CV_Assert(numpositions > 0);
612 Rect prevRect=curObject.lastPositions[numpositions-1];
613 LOGD("DetectionBasedTracker::updateTrackedObjects: prevRect[%d]={%d, %d, %d x %d}", i, prevRect.x, prevRect.y, prevRect.width, prevRect.height);
615 for(int j=0; j < N2; j++) {
616 LOGD("DetectionBasedTracker::updateTrackedObjects: j=%d", j);
617 if (correspondence[j] >= 0) {
618 LOGD("DetectionBasedTracker::updateTrackedObjects: j=%d is rejected, because it has correspondence=%d", j, correspondence[j]);
621 if (correspondence[j] !=NEW_RECTANGLE) {
622 LOGD("DetectionBasedTracker::updateTrackedObjects: j=%d is rejected, because it is intersected with another rectangle", j);
625 LOGD("DetectionBasedTracker::updateTrackedObjects: detectedObjects[%d]={%d, %d, %d x %d}",
626 j, detectedObjects[j].x, detectedObjects[j].y, detectedObjects[j].width, detectedObjects[j].height);
628 Rect r=prevRect & detectedObjects[j];
629 if ( (r.width > 0) && (r.height > 0) ) {
630 LOGD("DetectionBasedTracker::updateTrackedObjects: There is intersection between prevRect and detectedRect, r={%d, %d, %d x %d}",
631 r.x, r.y, r.width, r.height);
632 correspondence[j]=INTERSECTED_RECTANGLE;
634 if ( r.area() > bestArea) {
635 LOGD("DetectionBasedTracker::updateTrackedObjects: The area of intersection is %d, it is better than bestArea=%d", r.area(), bestArea);
641 if (bestIndex >= 0) {
642 LOGD("DetectionBasedTracker::updateTrackedObjects: The best correspondence for i=%d is j=%d", i, bestIndex);
643 correspondence[bestIndex]=i;
645 for(int j=0; j < N2; j++) {
646 if (correspondence[j] >= 0)
649 Rect r=detectedObjects[j] & detectedObjects[bestIndex];
650 if ( (r.width > 0) && (r.height > 0) ) {
651 LOGD("DetectionBasedTracker::updateTrackedObjects: Found intersection between "
652 "rectangles j=%d and bestIndex=%d, rectangle j=%d is marked as intersected", j, bestIndex, j);
653 correspondence[j]=INTERSECTED_RECTANGLE;
657 LOGD("DetectionBasedTracker::updateTrackedObjects: There is no correspondence for i=%d ", i);
658 curObject.numFramesNotDetected++;
662 LOGD("DetectionBasedTracker::updateTrackedObjects: start second cycle");
663 for(int j=0; j < N2; j++) {
664 LOGD("DetectionBasedTracker::updateTrackedObjects: j=%d", j);
665 int i=correspondence[j];
666 if (i >= 0) {//add position
667 LOGD("DetectionBasedTracker::updateTrackedObjects: add position");
668 trackedObjects[i].lastPositions.push_back(detectedObjects[j]);
669 while ((int)trackedObjects[i].lastPositions.size() > (int) innerParameters.numLastPositionsToTrack) {
670 trackedObjects[i].lastPositions.erase(trackedObjects[i].lastPositions.begin());
672 trackedObjects[i].numFramesNotDetected=0;
673 } else if (i==NEW_RECTANGLE){ //new object
674 LOGD("DetectionBasedTracker::updateTrackedObjects: new object");
675 trackedObjects.push_back(detectedObjects[j]);
677 LOGD("DetectionBasedTracker::updateTrackedObjects: was auxiliary intersection");
681 std::vector<TrackedObject>::iterator it=trackedObjects.begin();
682 while( it != trackedObjects.end() ) {
683 if ( (it->numFramesNotDetected > parameters.maxTrackLifetime)
686 (it->numDetectedFrames <= innerParameters.numStepsToWaitBeforeFirstShow)
688 (it->numFramesNotDetected > innerParameters.numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown)
692 int numpos=it->lastPositions.size();
693 CV_Assert(numpos > 0);
694 Rect r = it->lastPositions[numpos-1];
695 LOGD("DetectionBasedTracker::updateTrackedObjects: deleted object {%d, %d, %d x %d}",
696 r.x, r.y, r.width, r.height);
697 it=trackedObjects.erase(it);
703 Rect DetectionBasedTracker::calcTrackedObjectPositionToShow(int i) const
705 if ( (i < 0) || (i >= (int)trackedObjects.size()) ) {
706 LOGE("DetectionBasedTracker::calcTrackedObjectPositionToShow: ERROR: wrong i=%d", i);
709 if (trackedObjects[i].numDetectedFrames <= innerParameters.numStepsToWaitBeforeFirstShow){
710 LOGI("DetectionBasedTracker::calcTrackedObjectPositionToShow: trackedObjects[%d].numDetectedFrames=%d <= numStepsToWaitBeforeFirstShow=%d --- return empty Rect()",
711 i, trackedObjects[i].numDetectedFrames, innerParameters.numStepsToWaitBeforeFirstShow);
714 if (trackedObjects[i].numFramesNotDetected > innerParameters.numStepsToShowWithoutDetecting) {
718 const TrackedObject::PositionsVector& lastPositions=trackedObjects[i].lastPositions;
720 int N=lastPositions.size();
722 LOGE("DetectionBasedTracker::calcTrackedObjectPositionToShow: ERROR: no positions for i=%d", i);
726 int Nsize=std::min(N, (int)weightsSizesSmoothing.size());
727 int Ncenter= std::min(N, (int)weightsPositionsSmoothing.size());
733 for(int j=0; j < Nsize; j++) {
735 w+= lastPositions[k].width * weightsSizesSmoothing[j];
736 h+= lastPositions[k].height * weightsSizesSmoothing[j];
737 sum+=weightsSizesSmoothing[j];
742 w=lastPositions[N-1].width;
743 h=lastPositions[N-1].height;
748 for(int j=0; j < Ncenter; j++) {
750 Point tl(lastPositions[k].tl());
751 Point br(lastPositions[k].br());
760 center=center+ (c1 * weightsPositionsSmoothing[j]);
761 sum+=weightsPositionsSmoothing[j];
763 center *= (float)(1 / sum);
766 Point tl(lastPositions[k].tl());
767 Point br(lastPositions[k].br());
777 Point2f tl=center-(Point2f(w,h)*0.5);
778 Rect res(cvRound(tl.x), cvRound(tl.y), cvRound(w), cvRound(h));
779 LOGD("DetectionBasedTracker::calcTrackedObjectPositionToShow: Result for i=%d: {%d, %d, %d x %d}", i, res.x, res.y, res.width, res.height);
784 void DetectionBasedTracker::detectInRegion(const Mat& img, const Rect& r, vector<Rect>& detectedObjectsInRegions)
786 Rect r0(Point(), img.size());
787 Rect r1=scale_rect(r, innerParameters.coeffTrackingWindowSize);
789 if ( (r1.width <=0) || (r1.height <= 0) ) {
790 LOGD("DetectionBasedTracker::detectInRegion: Empty intersection");
794 int d=std::min(r.width, r.height);
795 d=cvRound(d * innerParameters.coeffObjectSizeToTrack);
797 vector<Rect> tmpobjects;
799 Mat img1(img, r1);//subimage for rectangle -- without data copying
800 LOGD("DetectionBasedTracker::detectInRegion: img1.size()=%d x %d, d=%d",
801 img1.size().width, img1.size().height, d);
803 int maxObjectSize=parameters.maxObjectSize;
804 Size max_objectSize(maxObjectSize, maxObjectSize);
806 cascadeForTracking.detectMultiScale( img1, tmpobjects,
807 parameters.scaleFactor, parameters.minNeighbors, 0
808 |CV_HAAR_FIND_BIGGEST_OBJECT
815 for(size_t i=0; i < tmpobjects.size(); i++) {
816 Rect curres(tmpobjects[i].tl() + r1.tl(), tmpobjects[i].size());
817 detectedObjectsInRegions.push_back(curres);
821 bool DetectionBasedTracker::setParameters(const Parameters& params)
823 if ( (params.minObjectSize <= 0)
824 || (params.maxObjectSize < 0)
825 || (params.scaleFactor <= 1.0)
826 || (params.maxTrackLifetime < 0) )
828 LOGE("DetectionBasedTracker::setParameters: ERROR: wrong parameters value");
832 separateDetectionWork->lock();
834 separateDetectionWork->unlock();
838 const DetectionBasedTracker::Parameters& DetectionBasedTracker::getParameters()