@ -52,11 +52,10 @@ static inline cv::Rect scale_rect(const cv::Rect& r, float scale)
} ;
} ;
void * workcycleObjectDetectorFunction ( void * p ) ;
void * workcycleObjectDetectorFunction ( void * p ) ;
class DetectionBasedTracker : : SeparateDetectionWork
class DetectionBasedTracker : : SeparateDetectionWork
{
{
public :
public :
SeparateDetectionWork ( DetectionBasedTracker & _detectionBasedTracker , cv : : Ptr < DetectionBasedTracker : : IDetector > _detector ) ;
SeparateDetectionWork ( DetectionBasedTracker & _detectionBasedTracker , const std : : string & cascadeFilename ) ;
virtual ~ SeparateDetectionWork ( ) ;
virtual ~ SeparateDetectionWork ( ) ;
bool communicateWithDetectingThread ( const Mat & imageGray , vector < Rect > & rectsWhereRegions ) ;
bool communicateWithDetectingThread ( const Mat & imageGray , vector < Rect > & rectsWhereRegions ) ;
bool run ( ) ;
bool run ( ) ;
@ -78,7 +77,7 @@ class DetectionBasedTracker::SeparateDetectionWork
protected :
protected :
DetectionBasedTracker & detectionBasedTracker ;
DetectionBasedTracker & detectionBasedTracker ;
cv : : Ptr < DetectionBasedTracker : : IDetector > cascadeInThread ;
cv : : CascadeClassifier cascadeInThread ;
pthread_t second_workthread ;
pthread_t second_workthread ;
pthread_mutex_t mutex ;
pthread_mutex_t mutex ;
@ -106,7 +105,7 @@ class DetectionBasedTracker::SeparateDetectionWork
long long timeWhenDetectingThreadStartedWork ;
long long timeWhenDetectingThreadStartedWork ;
} ;
} ;
DetectionBasedTracker : : SeparateDetectionWork : : SeparateDetectionWork ( DetectionBasedTracker & _detectionBasedTracker , cv : : Ptr < DetectionBasedTracker : : IDetector > _detector )
DetectionBasedTracker : : SeparateDetectionWork : : SeparateDetectionWork ( DetectionBasedTracker & _detectionBasedTracker , const std : : string & cascadeFilename )
: detectionBasedTracker ( _detectionBasedTracker ) ,
: detectionBasedTracker ( _detectionBasedTracker ) ,
cascadeInThread ( ) ,
cascadeInThread ( ) ,
isObjectDetectingReady ( false ) ,
isObjectDetectingReady ( false ) ,
@ -114,10 +113,9 @@ DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork(DetectionBas
stateThread ( STATE_THREAD_STOPPED ) ,
stateThread ( STATE_THREAD_STOPPED ) ,
timeWhenDetectingThreadStartedWork ( - 1 )
timeWhenDetectingThreadStartedWork ( - 1 )
{
{
CV_Assert ( ! _detector . empty ( ) ) ;
if ( ! cascadeInThread . load ( cascadeFilename ) ) {
CV_Error ( CV_StsBadArg , " DetectionBasedTracker::SeparateDetectionWork::SeparateDetectionWork: Cannot load a cascade from the file ' " + cascadeFilename + " ' " ) ;
cascadeInThread = _detector ;
}
int res = 0 ;
int res = 0 ;
res = pthread_mutex_init ( & mutex , NULL ) ; //TODO: should be attributes?
res = pthread_mutex_init ( & mutex , NULL ) ; //TODO: should be attributes?
if ( res ) {
if ( res ) {
@ -276,17 +274,20 @@ void DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector()
int64 t1_detect = getTickCount ( ) ;
int64 t1_detect = getTickCount ( ) ;
cascadeInThread - > detect ( imageSeparateDetecting , objects ) ;
int minObjectSize = detectionBasedTracker . parameters . minObjectSize ;
Size min_objectSize = Size ( minObjectSize , minObjectSize ) ;
/*cascadeInThread.detectMultiScale( imageSeparateDetecting, objects,
int maxObjectSize = detectionBasedTracker . parameters . maxObjectSize ;
Size max_objectSize ( maxObjectSize , maxObjectSize ) ;
cascadeInThread . detectMultiScale ( imageSeparateDetecting , objects ,
detectionBasedTracker . parameters . scaleFactor , detectionBasedTracker . parameters . minNeighbors , 0
detectionBasedTracker . parameters . scaleFactor , detectionBasedTracker . parameters . minNeighbors , 0
| CV_HAAR_SCALE_IMAGE
| CV_HAAR_SCALE_IMAGE
,
,
min_objectSize ,
min_objectSize ,
max_objectSize
max_objectSize
) ;
) ;
*/
LOGD ( " DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- end handling imageSeparateDetecting " ) ;
LOGD ( " DetectionBasedTracker::SeparateDetectionWork::workcycleObjectDetector() --- end handling imageSeparateDetecting " ) ;
if ( ! isWorking ( ) ) {
if ( ! isWorking ( ) ) {
@ -421,10 +422,16 @@ bool DetectionBasedTracker::SeparateDetectionWork::communicateWithDetectingThrea
DetectionBasedTracker : : Parameters : : Parameters ( )
DetectionBasedTracker : : Parameters : : Parameters ( )
{
{
minObjectSize = 96 ;
maxObjectSize = INT_MAX ;
scaleFactor = 1.1 ;
maxTrackLifetime = 5 ;
maxTrackLifetime = 5 ;
minNeighbors = 2 ;
minDetectionPeriod = 0 ;
minDetectionPeriod = 0 ;
}
}
DetectionBasedTracker : : InnerParameters : : InnerParameters ( )
DetectionBasedTracker : : InnerParameters : : InnerParameters ( )
{
{
numLastPositionsToTrack = 4 ;
numLastPositionsToTrack = 4 ;
@ -437,32 +444,39 @@ DetectionBasedTracker::InnerParameters::InnerParameters()
coeffObjectSpeedUsingInPrediction = 0.8 ;
coeffObjectSpeedUsingInPrediction = 0.8 ;
}
}
DetectionBasedTracker : : DetectionBasedTracker ( const std : : string & cascadeFilename , const Parameters & params )
DetectionBasedTracker : : DetectionBasedTracker ( cv : : Ptr < IDetector > MainDetector , cv : : Ptr < IDetector > TrackingDetector , const Parameters & params )
: separateDetectionWork ( ) ,
: separateDetectionWork ( ) ,
innerParameters ( ) ,
innerParameters ( ) ,
cascadeForTracking ( TrackingDetector ) ,
parameters ( params ) ,
numTrackedSteps ( 0 )
numTrackedSteps ( 0 )
{
{
CV_Assert ( ( params . maxTrackLifetime > = 0 )
CV_Assert ( ( params . minObjectSize > 0 )
& & ( ! MainDetector . empty ( ) )
& & ( params . maxObjectSize > = 0 )
& & ( ! TrackingDetector . empty ( ) ) ) ;
& & ( params . scaleFactor > 1.0 )
& & ( params . maxTrackLifetime > = 0 ) ) ;
if ( ! cascadeForTracking . load ( cascadeFilename ) ) {
CV_Error ( CV_StsBadArg , " DetectionBasedTracker::DetectionBasedTracker: Cannot load a cascade from the file ' " + cascadeFilename + " ' " ) ;
}
parameters = params ;
separateDetectionWork = new SeparateDetectionWork ( * this , MainDetector ) ;
separateDetectionWork = new SeparateDetectionWork ( * this , cascadeFilename ) ;
weightsPositionsSmoothing . push_back ( 1 ) ;
weightsPositionsSmoothing . push_back ( 1 ) ;
weightsSizesSmoothing . push_back ( 0.5 ) ;
weightsSizesSmoothing . push_back ( 0.5 ) ;
weightsSizesSmoothing . push_back ( 0.3 ) ;
weightsSizesSmoothing . push_back ( 0.3 ) ;
weightsSizesSmoothing . push_back ( 0.2 ) ;
weightsSizesSmoothing . push_back ( 0.2 ) ;
}
}
DetectionBasedTracker : : ~ DetectionBasedTracker ( )
DetectionBasedTracker : : ~ DetectionBasedTracker ( )
{
{
}
}
void DetectionBasedTracker : : process ( const Mat & imageGray )
void DetectionBasedTracker : : process ( const Mat & imageGray )
{
{
CV_Assert ( imageGray . type ( ) = = CV_8UC1 ) ;
CV_Assert ( imageGray . type ( ) = = CV_8UC1 ) ;
if ( ! separateDetectionWork - > isWorking ( ) ) {
if ( ! separateDetectionWork - > isWorking ( ) ) {
@ -480,9 +494,15 @@ void DetectionBasedTracker::process(const Mat& imageGray)
Mat imageDetect = imageGray ;
Mat imageDetect = imageGray ;
int D = parameters . minObjectSize ;
if ( D < 1 )
D = 1 ;
vector < Rect > rectsWhereRegions ;
vector < Rect > rectsWhereRegions ;
bool shouldHandleResult = separateDetectionWork - > communicateWithDetectingThread ( imageGray , rectsWhereRegions ) ;
bool shouldHandleResult = separateDetectionWork - > communicateWithDetectingThread ( imageGray , rectsWhereRegions ) ;
if ( shouldHandleResult ) {
if ( shouldHandleResult ) {
LOGD ( " DetectionBasedTracker::process: get _rectsWhereRegions were got from resultDetect " ) ;
LOGD ( " DetectionBasedTracker::process: get _rectsWhereRegions were got from resultDetect " ) ;
} else {
} else {
@ -497,6 +517,7 @@ void DetectionBasedTracker::process(const Mat& imageGray)
continue ;
continue ;
}
}
//correction by speed of rectangle
//correction by speed of rectangle
if ( n > 1 ) {
if ( n > 1 ) {
Point2f center = centerRect ( r ) ;
Point2f center = centerRect ( r ) ;
@ -539,7 +560,6 @@ void DetectionBasedTracker::getObjects(std::vector<cv::Rect>& result) const
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 ) ;
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 ) ;
}
}
}
}
void DetectionBasedTracker : : getObjects ( std : : vector < Object > & result ) const
void DetectionBasedTracker : : getObjects ( std : : vector < Object > & result ) const
{
{
result . clear ( ) ;
result . clear ( ) ;
@ -554,6 +574,8 @@ void DetectionBasedTracker::getObjects(std::vector<Object>& result) const
}
}
}
}
bool DetectionBasedTracker : : run ( )
bool DetectionBasedTracker : : run ( )
{
{
return separateDetectionWork - > run ( ) ;
return separateDetectionWork - > run ( ) ;
@ -689,7 +711,6 @@ void DetectionBasedTracker::updateTrackedObjects(const vector<Rect>& detectedObj
}
}
}
}
}
}
Rect DetectionBasedTracker : : calcTrackedObjectPositionToShow ( int i ) const
Rect DetectionBasedTracker : : calcTrackedObjectPositionToShow ( int i ) const
{
{
if ( ( i < 0 ) | | ( i > = ( int ) trackedObjects . size ( ) ) ) {
if ( ( i < 0 ) | | ( i > = ( int ) trackedObjects . size ( ) ) ) {
@ -722,8 +743,8 @@ Rect DetectionBasedTracker::calcTrackedObjectPositionToShow(int i) const
double sum = 0 ;
double sum = 0 ;
for ( int j = 0 ; j < Nsize ; j + + ) {
for ( int j = 0 ; j < Nsize ; j + + ) {
int k = N - j - 1 ;
int k = N - j - 1 ;
w + = lastPositions [ k ] . width * weightsSizesSmoothing [ j ] ;
w + = lastPositions [ k ] . width * weightsSizesSmoothing [ j ] ;
h + = lastPositions [ k ] . height * weightsSizesSmoothing [ j ] ;
h + = lastPositions [ k ] . height * weightsSizesSmoothing [ j ] ;
sum + = weightsSizesSmoothing [ j ] ;
sum + = weightsSizesSmoothing [ j ] ;
}
}
w / = sum ;
w / = sum ;
@ -741,7 +762,7 @@ Rect DetectionBasedTracker::calcTrackedObjectPositionToShow(int i) const
Point br ( lastPositions [ k ] . br ( ) ) ;
Point br ( lastPositions [ k ] . br ( ) ) ;
Point2f c1 ;
Point2f c1 ;
c1 = tl ;
c1 = tl ;
c1 = c1 * 0.5f ;
c1 = c1 * 0.5f ;
Point2f c2 ;
Point2f c2 ;
c2 = br ;
c2 = br ;
c2 = c2 * 0.5f ;
c2 = c2 * 0.5f ;
@ -781,7 +802,8 @@ void DetectionBasedTracker::detectInRegion(const Mat& img, const Rect& r, vector
return ;
return ;
}
}
int d = cvRound ( std : : min ( r . width , r . height ) * innerParameters . coeffObjectSizeToTrack ) ;
int d = std : : min ( r . width , r . height ) ;
d = cvRound ( d * innerParameters . coeffObjectSizeToTrack ) ;
vector < Rect > tmpobjects ;
vector < Rect > tmpobjects ;
@ -789,17 +811,17 @@ void DetectionBasedTracker::detectInRegion(const Mat& img, const Rect& r, vector
LOGD ( " DetectionBasedTracker::detectInRegion: img1.size()=%d x %d, d=%d " ,
LOGD ( " DetectionBasedTracker::detectInRegion: img1.size()=%d x %d, d=%d " ,
img1 . size ( ) . width , img1 . size ( ) . height , d ) ;
img1 . size ( ) . width , img1 . size ( ) . height , d ) ;
cascadeForTracking - > setMinObjectSize ( Size ( d , d ) ) ;
int maxObjectSize = parameters . maxObjectSize ;
cascadeForTracking - > detect ( img1 , tmpobjects ) ;
Size max_objectSize ( maxObjectSize , maxObjectSize ) ;
/*
detectMultiScale ( img1 , tmpobjects ,
cascadeForTracking . detectMultiScale ( img1 , tmpobjects ,
parameters . scaleFactor , parameters . minNeighbors , 0
parameters . scaleFactor , parameters . minNeighbors , 0
| CV_HAAR_FIND_BIGGEST_OBJECT
| CV_HAAR_FIND_BIGGEST_OBJECT
| CV_HAAR_SCALE_IMAGE
| CV_HAAR_SCALE_IMAGE
,
,
Size ( d , d ) ,
Size ( d , d ) ,
max_objectSize
max_objectSize
) ; */
) ;
for ( size_t i = 0 ; i < tmpobjects . size ( ) ; i + + ) {
for ( size_t i = 0 ; i < tmpobjects . size ( ) ; i + + ) {
Rect curres ( tmpobjects [ i ] . tl ( ) + r1 . tl ( ) , tmpobjects [ i ] . size ( ) ) ;
Rect curres ( tmpobjects [ i ] . tl ( ) + r1 . tl ( ) , tmpobjects [ i ] . size ( ) ) ;
@ -809,7 +831,10 @@ void DetectionBasedTracker::detectInRegion(const Mat& img, const Rect& r, vector
bool DetectionBasedTracker : : setParameters ( const Parameters & params )
bool DetectionBasedTracker : : setParameters ( const Parameters & params )
{
{
if ( params . maxTrackLifetime < 0 )
if ( ( params . minObjectSize < = 0 )
| | ( params . maxObjectSize < 0 )
| | ( params . scaleFactor < = 1.0 )
| | ( params . maxTrackLifetime < 0 ) )
{
{
LOGE ( " DetectionBasedTracker::setParameters: ERROR: wrong parameters value " ) ;
LOGE ( " DetectionBasedTracker::setParameters: ERROR: wrong parameters value " ) ;
return false ;
return false ;