1 #if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID)
2 #include "opencv2/contrib/detection_based_tracker.hpp"
9 #include <android/log.h>
10 #define LOG_TAG "OBJECT_DETECTOR"
11 #define LOGD0(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
12 #define LOGI0(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
13 #define LOGW0(...) ((void)__android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
14 #define LOGE0(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
19 #define LOGD0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
20 #define LOGI0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
21 #define LOGW0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
22 #define LOGE0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
26 #define LOGD(_str, ...) LOGD0(_str , ## __VA_ARGS__)
27 #define LOGI(_str, ...) LOGI0(_str , ## __VA_ARGS__)
28 #define LOGW(_str, ...) LOGW0(_str , ## __VA_ARGS__)
29 #define LOGE(_str, ...) LOGE0(_str , ## __VA_ARGS__)
31 #define LOGD(...) do{} while(0)
32 #define LOGI(...) do{} while(0)
33 #define LOGW(...) do{} while(0)
34 #define LOGE(...) do{} while(0)
41 static inline cv::Point2f centerRect(const cv::Rect& r)
43 return cv::Point2f(r.x+((float)r.width)/2, r.y+((float)r.height)/2);
46 static inline cv::Rect scale_rect(const cv::Rect& r, float scale)
48 cv::Point2f m=centerRect(r);
49 float width = r.width * scale;
50 float height = r.height * scale;
51 int x=cvRound(m.x - width/2);
52 int y=cvRound(m.y - height/2);
54 return cv::Rect(x, y, cvRound(width), cvRound(height));
57 void* workcycleObjectDetectorFunction(void* p);
59 class DetectionBasedTracker::SeparateDetectionWork
62 SeparateDetectionWork(DetectionBasedTracker& _detectionBasedTracker, const std::string& cascadeFilename);
63 virtual ~SeparateDetectionWork();
64 bool communicateWithDetectingThread(const Mat& imageGray, vector<Rect>& rectsWhereRegions);
69 inline bool isWorking()
71 return (stateThread==STATE_THREAD_WORKING_SLEEPING) || (stateThread==STATE_THREAD_WORKING_WITH_IMAGE);
75 pthread_mutex_lock(&mutex);
79 pthread_mutex_unlock(&mutex);
84 DetectionBasedTracker& detectionBasedTracker;
85 cv::CascadeClassifier cascadeInThread;
87 pthread_t second_workthread;
88 pthread_mutex_t mutex;
89 pthread_cond_t objectDetectorRun;
90 pthread_cond_t objectDetectorThreadStartStop;
92 std::vector<cv::Rect> resultDetect;
93 volatile bool isObjectDetectingReady;
94 volatile bool shouldObjectDetectingResultsBeForgot;
96 enum StateSeparatedThread {
97 STATE_THREAD_STOPPED=0,
98 STATE_THREAD_WORKING_SLEEPING,
99 STATE_THREAD_WORKING_WITH_IMAGE,
100 STATE_THREAD_WORKING,
101 STATE_THREAD_STOPPING
103 volatile StateSeparatedThread stateThread;
105 cv::Mat imageSeparateDetecting;
107 void workcycleObjectDetector();
108 friend void* workcycleObjectDetectorFunction(void* p);
110 long long timeWhenDetectingThreadStartedWork;
113 DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork(DetectionBasedTracker& _detectionBasedTracker, const std::string& cascadeFilename)
114 :detectionBasedTracker(_detectionBasedTracker),
116 isObjectDetectingReady(false),
117 shouldObjectDetectingResultsBeForgot(false),
118 stateThread(STATE_THREAD_STOPPED),
119 timeWhenDetectingThreadStartedWork(-1)
121 if(!cascadeInThread.load(cascadeFilename)) {
122 CV_Error(CV_StsBadArg, "DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork: Cannot load a cascade from the file '"+cascadeFilename+"'");
125 res=pthread_mutex_init(&mutex, NULL);//TODO: should be attributes?
127 LOGE("ERROR in DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork in pthread_mutex_init(&mutex, NULL) is %d", res);
128 throw(std::exception());
130 res=pthread_cond_init (&objectDetectorRun, NULL);
132 LOGE("ERROR in DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork in pthread_cond_init(&objectDetectorRun,, NULL) is %d", res);
133 pthread_mutex_destroy(&mutex);
134 throw(std::exception());
136 res=pthread_cond_init (&objectDetectorThreadStartStop, NULL);
138 LOGE("ERROR in DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork in pthread_cond_init(&objectDetectorThreadStartStop,, NULL) is %d", res);
139 pthread_cond_destroy(&objectDetectorRun);
140 pthread_mutex_destroy(&mutex);
141 throw(std::exception());
145 DetectionBasedTracker::SeparateDetectionWork::~SeparateDetectionWork()
147 if(stateThread!=STATE_THREAD_STOPPED) {
148 LOGE("\n\n\nATTENTION!!! dangerous algorithm error: destructor DetectionBasedTracker::DetectionBasedTracker::~SeparateDetectionWork is called before stopping the workthread");
151 pthread_cond_destroy(&objectDetectorThreadStartStop);
152 pthread_cond_destroy(&objectDetectorRun);
153 pthread_mutex_destroy(&mutex);
155 bool DetectionBasedTracker::SeparateDetectionWork::run()
157 LOGD("DetectionBasedTracker::SeparateDetectionWork::run() --- start");
158 pthread_mutex_lock(&mutex);
159 if (stateThread != STATE_THREAD_STOPPED) {
160 LOGE("DetectionBasedTracker::SeparateDetectionWork::run is called while the previous run is not stopped");
161 pthread_mutex_unlock(&mutex);
164 stateThread=STATE_THREAD_WORKING_SLEEPING;
165 pthread_create(&second_workthread, NULL, workcycleObjectDetectorFunction, (void*)this); //TODO: add attributes?
166 pthread_cond_wait(&objectDetectorThreadStartStop, &mutex);
167 pthread_mutex_unlock(&mutex);
168 LOGD("DetectionBasedTracker::SeparateDetectionWork::run --- end");
172 #define CATCH_ALL_AND_LOG(_block) \
178 catch(cv::Exception& e) { \
179 LOGE0("\n %s: ERROR: OpenCV Exception caught: \n'%s'\n\n", CV_Func, e.what()); \
180 } catch(std::exception& e) { \
181 LOGE0("\n %s: ERROR: Exception caught: \n'%s'\n\n", CV_Func, e.what()); \
183 LOGE0("\n %s: ERROR: UNKNOWN Exception caught\n\n", CV_Func); \
187 void* workcycleObjectDetectorFunction(void* p)
189 CATCH_ALL_AND_LOG({ ((DetectionBasedTracker::SeparateDetectionWork*)p)->workcycleObjectDetector(); });
191 ((DetectionBasedTracker::SeparateDetectionWork*)p)->stateThread=DetectionBasedTracker::SeparateDetectionWork::STATE_THREAD_STOPPED;
193 LOGE0("DetectionBasedTracker: workcycleObjectDetectorFunction: ERROR concerning pointer, received as the function parameter");
198 void DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector()
200 static double freq = getTickFrequency();
201 LOGD0("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- start");
202 vector<Rect> objects;
204 CV_Assert(stateThread==STATE_THREAD_WORKING_SLEEPING);
205 pthread_mutex_lock(&mutex);
207 pthread_cond_signal(&objectDetectorThreadStartStop);
209 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- before waiting");
210 CV_Assert(stateThread==STATE_THREAD_WORKING_SLEEPING);
211 pthread_cond_wait(&objectDetectorRun, &mutex);
213 stateThread=STATE_THREAD_WORKING_WITH_IMAGE;
215 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- after waiting");
217 pthread_mutex_unlock(&mutex);
219 bool isFirstStep=true;
221 isObjectDetectingReady=false;
225 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- next step");
228 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- before waiting");
229 CV_Assert(stateThread==STATE_THREAD_WORKING_SLEEPING);
231 pthread_mutex_lock(&mutex);
232 if (!isWorking()) {//it is a rare case, but may cause a crash
233 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- go out from the workcycle from inner part of lock just before waiting");
234 pthread_mutex_unlock(&mutex);
237 CV_Assert(stateThread==STATE_THREAD_WORKING_SLEEPING);
238 pthread_cond_wait(&objectDetectorRun, &mutex);
240 stateThread=STATE_THREAD_WORKING_WITH_IMAGE;
242 pthread_mutex_unlock(&mutex);
244 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- after waiting");
250 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- go out from the workcycle just after waiting");
255 if (imageSeparateDetecting.empty()) {
256 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- imageSeparateDetecting is empty, continue");
259 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- start handling imageSeparateDetecting, img.size=%dx%d, img.data=0x%p",
260 imageSeparateDetecting.size().width, imageSeparateDetecting.size().height, (void*)imageSeparateDetecting.data);
263 int64 t1_detect=getTickCount();
265 int minObjectSize=detectionBasedTracker.parameters.minObjectSize;
266 Size min_objectSize = Size(minObjectSize, minObjectSize);
268 int maxObjectSize=detectionBasedTracker.parameters.maxObjectSize;
269 Size max_objectSize(maxObjectSize, maxObjectSize);
272 cascadeInThread.detectMultiScale( imageSeparateDetecting, objects,
273 detectionBasedTracker.parameters.scaleFactor, detectionBasedTracker.parameters.minNeighbors, 0
279 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- end handling imageSeparateDetecting");
282 LOGD("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- go out from the workcycle just after detecting");
286 int64 t2_detect = getTickCount();
287 int64 dt_detect = t2_detect-t1_detect;
288 double dt_detect_ms=((double)dt_detect)/freq * 1000.0;
290 LOGI("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- objects num==%d, t_ms=%.4f", (int)objects.size(), dt_detect_ms);
292 pthread_mutex_lock(&mutex);
293 if (!shouldObjectDetectingResultsBeForgot) {
294 resultDetect=objects;
295 isObjectDetectingReady=true;
296 } else { //shouldObjectDetectingResultsBeForgot==true
297 resultDetect.clear();
298 isObjectDetectingReady=false;
299 shouldObjectDetectingResultsBeForgot=false;
302 stateThread=STATE_THREAD_WORKING_SLEEPING;
304 pthread_mutex_unlock(&mutex);
307 }// while(isWorking())
310 pthread_mutex_lock(&mutex);
312 stateThread=STATE_THREAD_STOPPED;
314 isObjectDetectingReady=false;
315 shouldObjectDetectingResultsBeForgot=false;
317 pthread_cond_signal(&objectDetectorThreadStartStop);
319 pthread_mutex_unlock(&mutex);
321 LOGI("DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector: Returning");
324 void DetectionBasedTracker::SeparateDetectionWork::stop()
326 //FIXME: TODO: should add quickStop functionality
327 pthread_mutex_lock(&mutex);
329 pthread_mutex_unlock(&mutex);
330 LOGE("SimpleHighguiDemoCore::stop is called but the SimpleHighguiDemoCore pthread is not active");
333 stateThread=STATE_THREAD_STOPPING;
334 LOGD("DetectionBasedTracker::SeparateDetectionWork::stop: before going to sleep to wait for the signal from the workthread");
335 pthread_cond_signal(&objectDetectorRun);
336 pthread_cond_wait(&objectDetectorThreadStartStop, &mutex);
337 LOGD("DetectionBasedTracker::SeparateDetectionWork::stop: after receiving the signal from the workthread, stateThread=%d", (int)stateThread);
338 pthread_mutex_unlock(&mutex);
341 void DetectionBasedTracker::SeparateDetectionWork::resetTracking()
343 LOGD("DetectionBasedTracker::SeparateDetectionWork::resetTracking");
344 pthread_mutex_lock(&mutex);
346 if (stateThread == STATE_THREAD_WORKING_WITH_IMAGE) {
347 LOGD("DetectionBasedTracker::SeparateDetectionWork::resetTracking: since workthread is detecting objects at the moment, we should make cascadeInThread stop detecting and forget the detecting results");
348 shouldObjectDetectingResultsBeForgot=true;
349 //cascadeInThread.setStopFlag();//FIXME: TODO: this feature also should be contributed to OpenCV
351 LOGD("DetectionBasedTracker::SeparateDetectionWork::resetTracking: since workthread is NOT detecting objects at the moment, we should NOT make any additional actions");
354 resultDetect.clear();
355 isObjectDetectingReady=false;
358 pthread_mutex_unlock(&mutex);
362 bool DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThread(const Mat& imageGray, vector<Rect>& rectsWhereRegions)
364 static double freq = getTickFrequency();
366 bool shouldCommunicateWithDetectingThread = (stateThread==STATE_THREAD_WORKING_SLEEPING);
367 LOGD("DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThread: shouldCommunicateWithDetectingThread=%d", (shouldCommunicateWithDetectingThread?1:0));
369 if (!shouldCommunicateWithDetectingThread) {
373 bool shouldHandleResult = false;
374 pthread_mutex_lock(&mutex);
376 if (isObjectDetectingReady) {
377 shouldHandleResult=true;
378 rectsWhereRegions = resultDetect;
379 isObjectDetectingReady=false;
381 double lastBigDetectionDuration = 1000.0 * (((double)(getTickCount() - timeWhenDetectingThreadStartedWork )) / freq);
382 LOGD("DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThread: lastBigDetectionDuration=%f ms", (double)lastBigDetectionDuration);
385 bool shouldSendNewDataToWorkThread = true;
386 if (timeWhenDetectingThreadStartedWork > 0) {
387 double time_from_previous_launch_in_ms=1000.0 * (((double)(getTickCount() - timeWhenDetectingThreadStartedWork )) / freq); //the same formula as for lastBigDetectionDuration
388 shouldSendNewDataToWorkThread = (time_from_previous_launch_in_ms >= detectionBasedTracker.parameters.minDetectionPeriod);
389 LOGD("DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThread: shouldSendNewDataToWorkThread was 1, now it is %d, since time_from_previous_launch_in_ms=%.2f, minDetectionPeriod=%d",
390 (shouldSendNewDataToWorkThread?1:0), time_from_previous_launch_in_ms, detectionBasedTracker.parameters.minDetectionPeriod);
393 if (shouldSendNewDataToWorkThread) {
395 imageSeparateDetecting.create(imageGray.size(), CV_8UC1);
397 imageGray.copyTo(imageSeparateDetecting);//may change imageSeparateDetecting ptr. But should not.
400 timeWhenDetectingThreadStartedWork = getTickCount() ;
402 pthread_cond_signal(&objectDetectorRun);
405 pthread_mutex_unlock(&mutex);
406 LOGD("DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThread: result: shouldHandleResult=%d", (shouldHandleResult?1:0));
408 return shouldHandleResult;
411 DetectionBasedTracker::Parameters::Parameters()
414 maxObjectSize=INT_MAX;
418 minDetectionPeriod=0;
421 DetectionBasedTracker::InnerParameters::InnerParameters()
423 numLastPositionsToTrack=4;
424 numStepsToWaitBeforeFirstShow=6;
425 numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown=3;
426 numStepsToShowWithoutDetecting=3;
428 coeffTrackingWindowSize=2.0;
429 coeffObjectSizeToTrack=0.85;
430 coeffObjectSpeedUsingInPrediction=0.8;
434 DetectionBasedTracker::DetectionBasedTracker(const std::string& cascadeFilename, const Parameters& params)
435 :separateDetectionWork(),
439 CV_Assert( (params.minObjectSize > 0)
440 && (params.maxObjectSize >= 0)
441 && (params.scaleFactor > 1.0)
442 && (params.maxTrackLifetime >= 0) );
444 if (!cascadeForTracking.load(cascadeFilename)) {
445 CV_Error(CV_StsBadArg, "DetectionBasedTracker::DetectionBasedTracker: Cannot load a cascade from the file '"+cascadeFilename+"'");
450 separateDetectionWork=new SeparateDetectionWork(*this, cascadeFilename);
452 weightsPositionsSmoothing.push_back(1);
453 weightsSizesSmoothing.push_back(0.5);
454 weightsSizesSmoothing.push_back(0.3);
455 weightsSizesSmoothing.push_back(0.2);
459 DetectionBasedTracker::~DetectionBasedTracker()
463 void DetectionBasedTracker::process(const Mat& imageGray)
465 CV_Assert(imageGray.type()==CV_8UC1);
467 if (!separateDetectionWork->isWorking()) {
468 separateDetectionWork->run();
471 static double freq = getTickFrequency();
472 static long long time_when_last_call_started=getTickCount();
475 double delta_time_from_prev_call=1000.0 * (((double)(getTickCount() - time_when_last_call_started)) / freq);
476 LOGD("DetectionBasedTracker::process: time from the previous call is %f ms", (double)delta_time_from_prev_call);
477 time_when_last_call_started=getTickCount();
480 Mat imageDetect=imageGray;
482 int D = parameters.minObjectSize;
486 vector<Rect> rectsWhereRegions;
487 bool shouldHandleResult=separateDetectionWork->communicateWithDetectingThread(imageGray, rectsWhereRegions);
489 if (shouldHandleResult) {
490 LOGD("DetectionBasedTracker::process: get _rectsWhereRegions were got from resultDetect");
492 LOGD("DetectionBasedTracker::process: get _rectsWhereRegions from previous positions");
493 for(size_t i = 0; i < trackedObjects.size(); i++) {
494 int n = trackedObjects[i].lastPositions.size();
497 Rect r = trackedObjects[i].lastPositions[n-1];
499 LOGE("DetectionBasedTracker::process: ERROR: ATTENTION: strange algorithm's behavior: trackedObjects[i].rect() is empty");
504 // correction by speed of rectangle
506 Point2f center = centerRect(r);
507 Point2f center_prev = centerRect(trackedObjects[i].lastPositions[n-2]);
508 Point2f shift = (center - center_prev) * innerParameters.coeffObjectSpeedUsingInPrediction;
510 r.x += cvRound(shift.x);
511 r.y += cvRound(shift.y);
515 rectsWhereRegions.push_back(r);
518 LOGI("DetectionBasedTracker::process: tracked objects num==%d", (int)trackedObjects.size());
520 vector<Rect> detectedObjectsInRegions;
522 LOGD("DetectionBasedTracker::process: rectsWhereRegions.size()=%d", (int)rectsWhereRegions.size());
523 for(size_t i=0; i < rectsWhereRegions.size(); i++) {
524 Rect r = rectsWhereRegions[i];
526 detectInRegion(imageDetect, r, detectedObjectsInRegions);
528 LOGD("DetectionBasedTracker::process: detectedObjectsInRegions.size()=%d", (int)detectedObjectsInRegions.size());
530 updateTrackedObjects(detectedObjectsInRegions);
533 void DetectionBasedTracker::getObjects(std::vector<cv::Rect>& result) const
537 for(size_t i=0; i < trackedObjects.size(); i++) {
538 Rect r=calcTrackedObjectPositionToShow(i);
543 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);
547 void DetectionBasedTracker::getObjects(std::vector<Object>& result) const
551 for(size_t i=0; i < trackedObjects.size(); i++) {
552 Rect r=calcTrackedObjectPositionToShow(i);
556 result.push_back(Object(r, trackedObjects[i].id));
557 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);
561 bool DetectionBasedTracker::run()
563 return separateDetectionWork->run();
566 void DetectionBasedTracker::stop()
568 separateDetectionWork->stop();
571 void DetectionBasedTracker::resetTracking()
573 separateDetectionWork->resetTracking();
574 trackedObjects.clear();
577 void DetectionBasedTracker::updateTrackedObjects(const vector<Rect>& detectedObjects)
581 INTERSECTED_RECTANGLE=-2
584 int N1=trackedObjects.size();
585 int N2=detectedObjects.size();
586 LOGD("DetectionBasedTracker::updateTrackedObjects: N1=%d, N2=%d", N1, N2);
588 for(int i=0; i < N1; i++) {
589 trackedObjects[i].numDetectedFrames++;
592 vector<int> correspondence(detectedObjects.size(), NEW_RECTANGLE);
593 correspondence.clear();
594 correspondence.resize(detectedObjects.size(), NEW_RECTANGLE);
596 for(int i=0; i < N1; i++) {
597 LOGD("DetectionBasedTracker::updateTrackedObjects: i=%d", i);
598 TrackedObject& curObject=trackedObjects[i];
603 int numpositions=curObject.lastPositions.size();
604 CV_Assert(numpositions > 0);
605 Rect prevRect=curObject.lastPositions[numpositions-1];
606 LOGD("DetectionBasedTracker::updateTrackedObjects: prevRect[%d]={%d, %d, %d x %d}", i, prevRect.x, prevRect.y, prevRect.width, prevRect.height);
608 for(int j=0; j < N2; j++) {
609 LOGD("DetectionBasedTracker::updateTrackedObjects: j=%d", j);
610 if (correspondence[j] >= 0) {
611 LOGD("DetectionBasedTracker::updateTrackedObjects: j=%d is rejected, because it has correspondence=%d", j, correspondence[j]);
614 if (correspondence[j] !=NEW_RECTANGLE) {
615 LOGD("DetectionBasedTracker::updateTrackedObjects: j=%d is rejected, because it is intersected with another rectangle", j);
618 LOGD("DetectionBasedTracker::updateTrackedObjects: detectedObjects[%d]={%d, %d, %d x %d}",
619 j, detectedObjects[j].x, detectedObjects[j].y, detectedObjects[j].width, detectedObjects[j].height);
621 Rect r=prevRect & detectedObjects[j];
622 if ( (r.width > 0) && (r.height > 0) ) {
623 LOGD("DetectionBasedTracker::updateTrackedObjects: There is intersection between prevRect and detectedRect, r={%d, %d, %d x %d}",
624 r.x, r.y, r.width, r.height);
625 correspondence[j]=INTERSECTED_RECTANGLE;
627 if ( r.area() > bestArea) {
628 LOGD("DetectionBasedTracker::updateTrackedObjects: The area of intersection is %d, it is better than bestArea=%d", r.area(), bestArea);
634 if (bestIndex >= 0) {
635 LOGD("DetectionBasedTracker::updateTrackedObjects: The best correspondence for i=%d is j=%d", i, bestIndex);
636 correspondence[bestIndex]=i;
638 for(int j=0; j < N2; j++) {
639 if (correspondence[j] >= 0)
642 Rect r=detectedObjects[j] & detectedObjects[bestIndex];
643 if ( (r.width > 0) && (r.height > 0) ) {
644 LOGD("DetectionBasedTracker::updateTrackedObjects: Found intersection between "
645 "rectangles j=%d and bestIndex=%d, rectangle j=%d is marked as intersected", j, bestIndex, j);
646 correspondence[j]=INTERSECTED_RECTANGLE;
650 LOGD("DetectionBasedTracker::updateTrackedObjects: There is no correspondence for i=%d ", i);
651 curObject.numFramesNotDetected++;
655 LOGD("DetectionBasedTracker::updateTrackedObjects: start second cycle");
656 for(int j=0; j < N2; j++) {
657 LOGD("DetectionBasedTracker::updateTrackedObjects: j=%d", j);
658 int i=correspondence[j];
659 if (i >= 0) {//add position
660 LOGD("DetectionBasedTracker::updateTrackedObjects: add position");
661 trackedObjects[i].lastPositions.push_back(detectedObjects[j]);
662 while ((int)trackedObjects[i].lastPositions.size() > (int) innerParameters.numLastPositionsToTrack) {
663 trackedObjects[i].lastPositions.erase(trackedObjects[i].lastPositions.begin());
665 trackedObjects[i].numFramesNotDetected=0;
666 } else if (i==NEW_RECTANGLE){ //new object
667 LOGD("DetectionBasedTracker::updateTrackedObjects: new object");
668 trackedObjects.push_back(detectedObjects[j]);
670 LOGD("DetectionBasedTracker::updateTrackedObjects: was auxiliary intersection");
674 std::vector<TrackedObject>::iterator it=trackedObjects.begin();
675 while( it != trackedObjects.end() ) {
676 if ( (it->numFramesNotDetected > parameters.maxTrackLifetime)
679 (it->numDetectedFrames <= innerParameters.numStepsToWaitBeforeFirstShow)
681 (it->numFramesNotDetected > innerParameters.numStepsToTrackWithoutDetectingIfObjectHasNotBeenShown)
685 int numpos=it->lastPositions.size();
686 CV_Assert(numpos > 0);
687 Rect r = it->lastPositions[numpos-1];
688 LOGD("DetectionBasedTracker::updateTrackedObjects: deleted object {%d, %d, %d x %d}",
689 r.x, r.y, r.width, r.height);
690 it=trackedObjects.erase(it);
697 Rect DetectionBasedTracker::calcTrackedObjectPositionToShow(int i) const
699 if ( (i < 0) || (i >= (int)trackedObjects.size()) ) {
700 LOGE("DetectionBasedTracker::calcTrackedObjectPositionToShow: ERROR: wrong i=%d", i);
703 if (trackedObjects[i].numDetectedFrames <= innerParameters.numStepsToWaitBeforeFirstShow){
704 LOGI("DetectionBasedTracker::calcTrackedObjectPositionToShow: trackedObjects[%d].numDetectedFrames=%d <= numStepsToWaitBeforeFirstShow=%d --- return empty Rect()",
705 i, trackedObjects[i].numDetectedFrames, innerParameters.numStepsToWaitBeforeFirstShow);
708 if (trackedObjects[i].numFramesNotDetected > innerParameters.numStepsToShowWithoutDetecting) {
712 const TrackedObject::PositionsVector& lastPositions=trackedObjects[i].lastPositions;
714 int N=lastPositions.size();
716 LOGE("DetectionBasedTracker::calcTrackedObjectPositionToShow: ERROR: no positions for i=%d", i);
720 int Nsize=std::min(N, (int)weightsSizesSmoothing.size());
721 int Ncenter= std::min(N, (int)weightsPositionsSmoothing.size());
727 for(int j=0; j < Nsize; j++) {
729 w+= lastPositions[k].width * weightsSizesSmoothing[j];
730 h+= lastPositions[k].height * weightsSizesSmoothing[j];
731 sum+=weightsSizesSmoothing[j];
736 w=lastPositions[N-1].width;
737 h=lastPositions[N-1].height;
742 for(int j=0; j < Ncenter; j++) {
744 Point tl(lastPositions[k].tl());
745 Point br(lastPositions[k].br());
754 center=center+ (c1 * weightsPositionsSmoothing[j]);
755 sum+=weightsPositionsSmoothing[j];
757 center *= (float)(1 / sum);
760 Point tl(lastPositions[k].tl());
761 Point br(lastPositions[k].br());
771 Point2f tl=center-(Point2f(w,h)*0.5);
772 Rect res(cvRound(tl.x), cvRound(tl.y), cvRound(w), cvRound(h));
773 LOGD("DetectionBasedTracker::calcTrackedObjectPositionToShow: Result for i=%d: {%d, %d, %d x %d}", i, res.x, res.y, res.width, res.height);
778 void DetectionBasedTracker::detectInRegion(const Mat& img, const Rect& r, vector<Rect>& detectedObjectsInRegions)
780 Rect r0(Point(), img.size());
781 Rect r1 = scale_rect(r, innerParameters.coeffTrackingWindowSize);
784 if ( (r1.width <=0) || (r1.height <= 0) ) {
785 LOGD("DetectionBasedTracker::detectInRegion: Empty intersection");
789 int d = std::min(r.width, r.height);
790 d = cvRound(d * innerParameters.coeffObjectSizeToTrack);
792 vector<Rect> tmpobjects;
794 Mat img1(img, r1);//subimage for rectangle -- without data copying
795 LOGD("DetectionBasedTracker::detectInRegion: img1.size()=%d x %d, d=%d",
796 img1.size().width, img1.size().height, d);
798 int maxObjectSize = parameters.maxObjectSize;
799 Size max_objectSize(maxObjectSize, maxObjectSize);
801 cascadeForTracking.detectMultiScale( img1, tmpobjects,
802 parameters.scaleFactor, parameters.minNeighbors, 0
803 |CV_HAAR_FIND_BIGGEST_OBJECT
810 for(size_t i=0; i < tmpobjects.size(); i++) {
811 Rect curres(tmpobjects[i].tl() + r1.tl(), tmpobjects[i].size());
812 detectedObjectsInRegions.push_back(curres);
816 bool DetectionBasedTracker::setParameters(const Parameters& params)
818 if ( (params.minObjectSize <= 0)
819 || (params.maxObjectSize < 0)
820 || (params.scaleFactor <= 1.0)
821 || (params.maxTrackLifetime < 0) )
823 LOGE("DetectionBasedTracker::setParameters: ERROR: wrong parameters value");
827 separateDetectionWork->lock();
829 separateDetectionWork->unlock();
833 const DetectionBasedTracker::Parameters& DetectionBasedTracker::getParameters()