Esempio n. 1
0
void ContourManager::children(const ContourVector & _contours, const Hierarchy & _hierarchy, int _parentIndex, ContourVector & _children)
{
    _children.clear();

    int currentChild = _hierarchy[_parentIndex][HIERARCHY_INDEX_FIRST_CHILD];//First child

    while (currentChild >= 0)
    {
        _children.push_back(_contours.at(currentChild));

        currentChild = _hierarchy[currentChild][HIERARCHY_INDEX_NEXT];
    }//while (currentChild >= 0)
}//children
void MotionTrackingTestApp::onDepth( openni::VideoFrameRef frame, const OpenNI::DeviceOptions& deviceOptions){
    mInput = toOcv( OpenNI::toChannel16u( frame ) );
    
    cv::Mat withoutBlack;
    withoutBlack = removeBlack( mInput, mNearLimit, mFarLimit );
    
    cv::Mat eightBit;
    cv::Mat thresh;
    
    // convert to RGB color space, with some compensation
    withoutBlack.convertTo( eightBit, CV_8UC3, 0.1/1.0  );
    cv::bitwise_not(eightBit, eightBit);
    
    mContours.clear();
    mApproxContours.clear();
    cv::threshold( eightBit, thresh, mThresh, mMaxVal, CV_8U );
    cv::findContours( thresh, mContours, mHierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
    
    vector<cv::Point> approx;
    // approx number of points per contour
    for( int i=0; i<mContours.size(); i++ ) {
        cv::approxPolyDP(mContours[i], approx, 3, true );
        mApproxContours.push_back( approx );
    }
    
    // get data that we can later compare
    mShapes.clear();
    mShapes = getEvaluationSet( mApproxContours, 75, 100000 );
    
    // find the nearest match for each shape
    for( int i = 0; i<mTrackedShapes.size(); i++ ){
        Shape* nearestShape = findNearestMatch( mTrackedShapes[i], mShapes, 5000 );
        
        if( nearestShape != NULL){
            // update our tracked contour
            // last frame seen
            nearestShape->matchFound = true;
            mTrackedShapes[i].centroid = nearestShape->centroid;
            mTrackedShapes[i].lastFrameSeen = ci::app::getElapsedFrames();
            mTrackedShapes[i].hull.clear();
            mTrackedShapes[i].hull = nearestShape->hull;
        }
    }
    
    // if shape->matchFound is false, add it as a new shape
    for( int i = 0; i<mShapes.size(); i++ ){
        if( mShapes[i].matchFound == false ){
            mShapes[i].ID = shapeUID;
            mShapes[i].lastFrameSeen = ci::app::getElapsedFrames();
            mTrackedShapes.push_back( mShapes[i]);
            shapeUID++;
//            std::cout << "adding a new tracked shape with ID: " << mShapes[i].ID << std::endl;
        }
    }
    
    // if we didnt find a match for x frames, delete the tracked shape
    for( vector<Shape>::iterator it=mTrackedShapes.begin(); it!=mTrackedShapes.end(); ){
//        std::cout << "tracked shapes size: " << mTrackedShapes.size() << std::endl;
        if( ci::app::getElapsedFrames() - it->lastFrameSeen > 20 ){
//            std::cout << "deleting shape with ID: " << it->ID << std::endl;
            it = mTrackedShapes.erase(it);
        } else {
            ++it;
        }
    }
    
    cv::Mat gray8Bit;
    withoutBlack.convertTo( gray8Bit, CV_8UC3, 0.1/1.0  );
    
    mSurfaceDepth = Surface8u( fromOcv( mInput  ) );
    mSurfaceBlur = Surface8u( fromOcv( withoutBlack ) );
    mSurfaceSubtract = Surface8u( fromOcv( eightBit ) );
}