Beispiel #1
0
/*!
 * \brief buildMassCenters Use this function to retrieve the mass centers of _contours.
 * \param _contours Input contours.
 * \param _massCenters Output mass centers.
 */
void ContourManager::buildMassCenters(const ContourVector & _contours, PointVector & _massCenters)
{
    ContourVector::size_type contourSize = _contours.size();
    _massCenters.resize(contourSize);

    for (ContourVector::size_type i = 0; i < contourSize; ++i)
    {
        massCenter(_contours[i],_massCenters[i]);
    }//for(ContourVector::size_type i = 0; i < contourSize; ++i)
}//buildMassCenters
Beispiel #2
0
/*!
 * \brief buildMassCenters Use this function to retrieve the bounding rects of _contours.
 * \param _contours Input contours.
 * \param _massCenters Output bounding rects. Its size must be at least equal to _contours size. No control is made.
 */
void ContourManager::buildBoundings(const ContourVector & _contours, RectVector & _boundings)
{
    ContourVector::size_type contourSize = _contours.size();
    _boundings.resize(contourSize);

#if SD_APPROX_CURVES
    //Approximate contour curves
    std::vector<ContourCurve> contourCurves(contourSize);
#endif//SD_APPROX_CURVES

    //Retrieving bounding rects
    for(ContourVector::size_type i = 0; i < contourSize; ++i)
    {
#if SD_APPROX_CURVES
        cv::approxPolyDP(cv::Mat(_contours[i]), contourCurves[i], 3, true);
        _boundings[i] = cv::boundingRect(cv::Mat(contourCurves[i]));
#else
        _boundings[i] = cv::boundingRect(cv::Mat(_contours[i]));
#endif//SD_APPROX_CURVES
    }//for(ContourVector::size_type i = 0; i < contourSize; ++i)
}//buildBoundings
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 ) );
}