예제 #1
0
int main( int argc, char** argv )
{
	if( argc < 2 )
	{
		std::cerr << "FATAL ERROR: Wrong program usage.\n"
							<< "\tEXAMPLE: ./track <pattern_image_path>\n"
							<< "Terminating...\n";
		exit( -1 );
	}
	else
	{
		cv::VideoCapture capture(0);
	
		cv::Mat pattern_image = cv::imread(argv[1]);
		cv::Mat currentFrame, previousFrame;
		
		tracking::Tracker aTracker( pattern_image );
		tracking::PatternDetector patternDetector( pattern_image );		
		
		cv::namedWindow( "Pattern Image" );
		cv::imshow( "Pattern Image", pattern_image );
		
		while( true )
		{
			capture >> currentFrame;
			if( currentFrame.empty() )
			{
				std::cout << "ERROR: Cannot open camera device...\n";
				return -1;
			}
			
			if ( (cvWaitKey(10) & 255) == 27 ) break;
			patternDetector.processFrame( currentFrame );
			// if( previousFrame.empty() )
			// {
			// 	patternDetector.processFrame( currentFrame );
			// }
			// else
			// {
			// 	// patternDetector.flowTracking( previousFrame, currentFrame );
			// 	patternDetector.processFrame( currentFrame );
			// }
			cv::imshow( "Video", currentFrame );
			// previousFrame = currentFrame.clone();
		}
		
	}
	
	return 0;	  
}
/*bool batchTrain(Vector<UINT> &obs)
- This method 
*/
bool HiddenMarkovModel::train(const vector< vector<UINT> > &trainingData){

    //Clear any previous models
    modelTrained = false;
    observationSequence.clear();
    estimatedStates.clear();
    trainingIterationLog.clear();
    
	UINT n,currentIter, bestIndex = 0;
	double newLoglikelihood, bestLogValue = 0;
    
    if( numRandomTrainingIterations > 1 ){

        //A buffer to keep track each AB matrix
        vector< MatrixDouble > aTracker( numRandomTrainingIterations );
        vector< MatrixDouble > bTracker( numRandomTrainingIterations );
        vector< double > loglikelihoodTracker( numRandomTrainingIterations );
        
        UINT maxNumTestIter = maxNumIter > 10 ? 10 : maxNumIter;

        //Try and find the best starting point
        for(n=0; n<numRandomTrainingIterations; n++){
            //Reset the model to a new random starting values
            randomizeMatrices(numStates,numSymbols);

            if( !train_(trainingData,maxNumTestIter,currentIter,newLoglikelihood) ){
                return false;
            }
            aTracker[n] = a;
            bTracker[n] = b;
            loglikelihoodTracker[n] = newLoglikelihood;
        }

        //Get the best result and set it as the a and b starting values
        bestIndex = 0;
        bestLogValue = loglikelihoodTracker[0];
        for(n=1; n<numRandomTrainingIterations; n++){
            if(bestLogValue < loglikelihoodTracker[n]){
                bestLogValue = loglikelihoodTracker[n];
                bestIndex = n;
            }
        }

        //Set a and b
        a = aTracker[bestIndex];
        b = bTracker[bestIndex];
        
    }else{
        randomizeMatrices(numStates,numSymbols);
    }

	//Perform the actual training
    if( !train_(trainingData,maxNumIter,currentIter,newLoglikelihood) ){
        return false;
    }

	//Calculate the observationSequence buffer length
	const UINT numObs = (unsigned int)trainingData.size();
	UINT k = 0;
    UINT averageObsLength = 0;
	for(k=0; k<numObs; k++){
		const UINT T = (unsigned int)trainingData[k].size();
		averageObsLength += T;
	}
    
    averageObsLength = (UINT)floor( averageObsLength/double(numObs) );
    observationSequence.resize( averageObsLength );
    estimatedStates.resize( averageObsLength );
    
    //Finally, flag that the model was trained
    modelTrained = true;

	return true;
}