void VectorDataSet::weightedSum(FeatureVector& result, const std::vector<int> &patterns,
								const std::vector<double> &alpha)
{
  int pIndex;
  // ASA FeatureVector is an alias for vector<double>
  vector<double> weight_temp (numFeatures,0);

  for (unsigned int i = 0; i < patterns.size(); i++){
    pIndex = patterns[i];
    for (int j = 0; j != X[pIndex].size(); ++j){
      weight_temp[j] += X[pIndex][j] * alpha[i];
    }
  }
  result.clear();
  result.initialize(weight_temp);
  
}
示例#2
0
void Slic::_InitCenterFeature(int i, int j, FeatureVector &featureVec)
{
    featureVec.clear();
    uchar *buffer = new uchar[5*5*_dataSize*_bandCount];
    _poSrcDS->RasterIO(GF_Read,j-2,i-2,5,5,buffer,5,5,_dataType,_bandCount,0,0,0,0);

    double minEdge = std::numeric_limits<double>::max();
    int minN;
    int minM;
    for (int n=1;n<4;++n)
    {
        for(int m=1;m<4;++m)
        {
            double edge = 0;
            edge += (buffer[(n-1)*5+m]-buffer[(n+1)*5+m])*(buffer[(n-1)*5+m]-buffer[(n+1)*5+m]);
            edge += (buffer[n*5+m-1]-buffer[n*5+m+1])*(buffer[n*5+m-1]-buffer[n*5+m+1]);

            if (edge < minEdge)
            {
                minEdge = edge;
                minN = n;
                minM = m;
            }
        }
    }

    int centerIndex = minN*5+minM;

    uchar* p = buffer;
    for(int k=0;k<_bandCount;++k,p += (5*5*_dataSize))
    {
        featureVec.push_back( SRCVAL(p,_dataType,centerIndex)/_regularizer);
    }
    featureVec.push_back(static_cast<double>(j+minM-2)/_regionSize);       //x
    featureVec.push_back(static_cast<double>(i+minN-2)/_regionSize);       //y
    delete []buffer;
}
void FaceFeaturesBlock::update()
{
	try
	{
		const Image &image = m_imageIn.getReadableRef< Image >();

		if( m_haarDetectionDownscaleIn.hasChanged() )
			m_faceDetection->greyImgSmlScale( m_haarDetectionDownscaleIn.getReadableRef< double >() );

		if( m_cascFileFaceIn.hasChanged() )
			m_faceDetection->loadFaceCascade( m_cascFileFaceIn.getReadableRef< std::string >() );
		if( m_cascFileEyesIn.hasChanged() )
			m_faceDetection->loadEyesCascade( m_cascFileEyesIn.getReadableRef< std::string >() );
		if( m_cascFileNoseIn.hasChanged() )
			m_faceDetection->loadNoseCascade( m_cascFileNoseIn.getReadableRef< std::string >() );
		if( m_cascFileMouthIn.hasChanged() )
			m_faceDetection->loadMouthCascade( m_cascFileMouthIn.getReadableRef< std::string >() );

		if( m_haarMinNeighboursFaceIn.hasChanged() )
			m_faceDetection->minNeighboursFace( m_haarMinNeighboursFaceIn.getReadableRef< unsigned int >() );
		if( m_haarMinNeighboursEyesIn.hasChanged() )
			m_faceDetection->minNeighboursEyes( m_haarMinNeighboursEyesIn.getReadableRef< unsigned int >() );
		if( m_haarMinNeighboursNoseIn.hasChanged() )
			m_faceDetection->minNeighboursNose( m_haarMinNeighboursNoseIn.getReadableRef< unsigned int >() );
		if( m_haarMinNeighboursMouthIn.hasChanged() )
			m_faceDetection->minNeighboursMouth( m_haarMinNeighboursMouthIn.getReadableRef< unsigned int >() );

		if( m_haarMinSizeFaceIn.hasChanged() )
			m_faceDetection->minSizeFace( m_haarMinSizeFaceIn.getReadableRef< Vec2 >() );
		if( m_haarMinSizeEyesIn.hasChanged() )
			m_faceDetection->minSizeEyes( m_haarMinSizeEyesIn.getReadableRef< Vec2 >() );
		if( m_haarMinSizeNoseIn.hasChanged() )
			m_faceDetection->minSizeNose( m_haarMinSizeNoseIn.getReadableRef< Vec2 >() );
		if( m_haarMinSizeMouthIn.hasChanged() )
			m_faceDetection->minSizeMouth( m_haarMinSizeMouthIn.getReadableRef< Vec2 >() );

		if( m_equalizeHistogramIn.hasChanged() )
			m_faceDetection->equalizeHist( m_equalizeHistogramIn.getReadableRef< bool >() );

		if( m_haarScaleFactorIn.hasChanged() )
			m_faceDetection->haarScaleFactor( m_haarScaleFactorIn.getReadableRef< double >() );

		if( m_imageIn.hasUpdated() )
		{
			double dt = m_stopWatch->milliSeconds();

			m_timeAccu += dt;
			m_timeOverall += dt;

			if( m_timeAccu > 1.0 )
			{
				m_frames = 0;
				m_timeAccu = 0.0;
			}

			m_stopWatch->restart();

			m_frames++;

			FeatureVector faces;

			m_faceDetection->detectFaces( image, faces );
			m_faceTracking->track( faces,
				m_affinityWeightPosIn.getReadableRef<double>(), 
				m_affinityWeightSizeIn.getReadableRef<double>(),
				m_affinityThresholdIn.getReadableRef<double>(),
				m_coherenceWeightDirIn.getReadableRef<double>(), 
				m_coherenceWeightVelIn.getReadableRef<double>(), 
				m_coherenceWeightSizeIn.getReadableRef<double>(),
				m_coherenceThresholdIn.getReadableRef<double>(),
				m_coherenceToleranceDirIn.getReadableRef<double>() / image.getWidth(),
				m_coherenceToleranceVelIn.getReadableRef<double>() / image.getWidth(),
				m_coherenceToleranceSizeIn.getReadableRef<double>() / image.getWidth(),
				m_discardThresholdIn.getReadableRef<double>(),
				m_extrapolationDampingIn.getReadableRef<double>(), 
				m_extrapolationCoherenceRiseIn.getReadableRef<double>(),
				m_extrapolationMinSizeFace,
				m_timeOverall );

			if( m_faceTracking->trackingInfoList().size() )
			{

				for( TrackingInfoList::iterator itT = m_faceTracking->trackingInfoList().begin(); itT != m_faceTracking->trackingInfoList().end(); ++itT )
				{
					if( itT->getFaceTrajectory().getEntryCount() )
					{
						FeatureVector eyesCandidates;
						FeatureVector noseCandidates;
						FeatureVector mouthCandidates;

						m_faceDetection->detectFeatures( image, itT->getFaceTrajectory().getLastRegion(), 
							eyesCandidates, noseCandidates, mouthCandidates, 
							m_useEyesIn.getReadableRef< bool >(),
							m_useNoseIn.getReadableRef< bool >(),
							m_useMouthIn.getReadableRef< bool >() );

						itT->addFromFeatureCandidates( eyesCandidates, noseCandidates, mouthCandidates, m_timeOverall, m_affinityWeightPosIn.getReadableRef<double>(), m_affinityWeightSizeIn.getReadableRef<double>() );
					}
				}

				std::vector< FaceDesc > &faces = m_faceOut.getWriteableRef< std::vector< FaceDesc > >();
				faces.clear();

				for( TrackingInfoList::iterator itT = m_faceTracking->trackingInfoList().begin(); itT != m_faceTracking->trackingInfoList().end(); ++itT )
				{
					FaceDesc desc( itT->getUserID(), itT->getFaceTrajectory().getLastRegion() );

					if( m_useEyesIn.getReadableRef< bool >() )
					{
						if( itT->getEyeLeftTrajectory().getEntryCount() )
						{
							desc.eyeLeft( FeatureDesc( itT->getEyeLeftTrajectory().getLastRegion() ) );
						}
						if( itT->getEyeRightTrajectory().getEntryCount() )
						{
							desc.eyeRight( FeatureDesc( itT->getEyeRightTrajectory().getLastRegion() ) );
						}
					}
					if( m_useNoseIn.getReadableRef< bool >() && itT->getNoseTrajectory().getEntryCount() )
					{
						desc.nose( FeatureDesc( itT->getNoseTrajectory().getLastRegion() ) );
					}
					if( m_useMouthIn.getReadableRef< bool >() && itT->getNoseTrajectory().getEntryCount() )
					{
						desc.mouth( FeatureDesc( itT->getMouthTrajectory().getLastRegion() ) );
					}

					faces.push_back( desc );
				}
			}
			else
				m_faceOut.discard();
		}
		else
		{
			m_faceOut.discard();
		}
	}
	catch( Exception & e )
	{
		cout << e.message() << " " << e.what() << endl;
		e.rethrow();
	}
	catch( std::exception & e )
	{
		cout << e.what() << endl;
	}
}