Ejemplo n.º 1
0
void CascadeClassifier::ApplyOriginalSizeForInputBoosting(const CString filename,int& pointer) const
{
    IntImage procface;
    IntImage image,square;
    REAL sq,ex,value;
    int result;
    CRect rect;
    REAL ratio;

    procface.Load(filename);
    if(procface.height <=0 || procface.width<=0) return;
    ratio = 1.0;
    REAL paddedsize = REAL(1)/REAL((sx+1)*(sy+1));
    while((procface.height>sx+1) && (procface.width>sy+1))
    {
        procface.CalcSquareAndIntegral(square,image);
        for(int i=0,size_x=image.height-sx; i<size_x; i+=bootstrap_increment[bootstrap_level])
            for(int j=0,size_y=image.width-sy; j<size_y; j+=bootstrap_increment[bootstrap_level])
            {
                ex = image.data[i+sx][j+sy]+image.data[i][j]-image.data[i+sx][j]-image.data[i][j+sy];
                if(ex<mean_min || ex>mean_max) continue;
                sq = square.data[i+sx][j+sy]+square.data[i][j]-square.data[i+sx][j]-square.data[i][j+sy];
                if(sq<sq_min) continue;
                ex *= paddedsize;
                ex = ex * ex;
                sq *= paddedsize;
                sq = sq - ex;
                ASSERT(sq>=0);
                if(sq>0) sq = sqrt(sq);
                else sq = 1.0;
                if(sq<var_min) continue;
                result = 1;
                for(int k=0; k<count; k++)
                {
                    value = 0.0;
                    for(int t=0,size=ac[k].count; t<size; t++)
                        value += (ac[k].alphas[t]*ac[k].scs[t].Apply(ac[k].scs[t].GetOneFeatureTranslation(image.data+i,j)/sq));
                    if(value<ac[k].thresh)
                    {
                        result = 0;
                        break;
                    }
                }
                if(result==1)
                {
                    for(int k=1; k<=sx; k++)
                        for(int t=1; t<=sy; t++)
                            trainset[pointer].data[k][t]=image.data[i+k][j+t]-image.data[i+k][j]-image.data[i][j+t]+image.data[i][j];
                    pointer++;
                    if(pointer==totalcount) return;
                }
            }
        ratio = ratio * bootstrap_resizeratio[bootstrap_level];
        procface.Resize(image,bootstrap_resizeratio[bootstrap_level]);
        SwapIntImage(procface,image);
    }
}
int main(int argc,char* argv[])
{
	std::cout << "usage:" << std::endl;
	std::cout << argv[0] << " <video_file>" << std::endl << std::endl;
	std::cout << "keys:" << std::endl;
	std::cout << "space : toggle using simple post-process (NMS, non-maximal suppression)" << std::endl;
	std::cout << "0     : waits to process next frame until a key pressed" << std::endl;
	std::cout << "1     : doesn't wait to process next frame" << std::endl;
	std::cout << "2     : resize frames 1/2" << std::endl;
	std::cout << "3     : don't resize frames" << std::endl;
	std::cout << "4     : resize frames 1/4" << std::endl;
	if (argc < 2)
		return 0;

	cv::Mat src;
    cv::VideoCapture capture( argv[1] );

    LoadCascade(scanner);
    std::cout<<"Detectors loaded."<<std::endl;
    int key = 0;
	int wait_time = 1;
	float fx = 1;
    bool rect_organization = true;
	IntImage<double> original;

    while( key != 27 )
    {
        capture >> src;
        if( src.empty() ) break;  
		
		if (fx < 1)
		{
			cv::resize(src, src, cv::Size(), fx, fx);
		}
		
        original.Load( src );
        std::vector<CRect> results;
        scanner.FastScan(original, results, 2);

        if(rect_organization)
        {
            PostProcess(results,2);
            PostProcess(results,0);
            RemoveCoveredRectangles(results);
        }

        for(size_t i = 0; i < results.size(); i++)
        {
            cv::rectangle(src, cvPoint(results[i].left,results[i].top),cvPoint(results[i].right,results[i].bottom),cv::Scalar(0,255,0),2 );
        }

        cv::imshow("result",src);
        key = cv::waitKey( wait_time );

		if (key == 32)
			rect_organization = !rect_organization;

		if (key == 48)
			wait_time = 0;

		if (key == 49)
			wait_time = 1;

		if (key == 50)
			fx = 0.5;

		if (key == 51)
			fx = 1;

		if (key == 52)
			fx = 0.25;
	}
    cv::waitKey();
    return 0;
}