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);
    }
}
Ejemplo n.º 2
0
void CascadeClassifier::ApplyOriginalSize(IntImage& original,const CString filename)
{
    IntImage procface;
    IntImage image,square;
    REAL sq,ex,value;
    int result;
    CRect rect;
    REAL ratio;
    vector<CRect> results;

    procface.Copy(original);
    ratio = 1.0;
    results.clear();
    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+=1)
            for(int j=0,size_y=image.width-sy; j<size_y; j+=1)
            {
                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++)
                    {
                        REAL f1 = 0;
                        REAL** p = image.data + i;
                        SimpleClassifier& s = ac[k].scs[t];

                        switch(s.type)
                        {
                        case 0:
                            f1 =   p[s.x1][j+s.y3] - p[s.x1][j+s.y1] + p[s.x3][j+s.y3] - p[s.x3][j+s.y1]
                                   + REAL(2)*(p[s.x2][j+s.y1] - p[s.x2][j+s.y3]);
                            break;
                        case 1:
                            f1 =   p[s.x3][j+s.y1] + p[s.x3][j+s.y3] - p[s.x1][j+s.y1] - p[s.x1][j+s.y3]
                                   + REAL(2)*(p[s.x1][j+s.y2] - p[s.x3][j+s.y2]);
                            break;
                        case 2:
                            f1 =   p[s.x1][j+s.y1] - p[s.x1][j+s.y3] + p[s.x4][j+s.y3] - p[s.x4][j+s.y1]
                                   + REAL(3)*(p[s.x2][j+s.y3] - p[s.x2][j+s.y1] + p[s.x3][j+s.y1]  - p[s.x3][j+s.y3]);
                            break;
                        case 3:
                            f1 =   p[s.x1][j+s.y1] - p[s.x1][j+s.y4] + p[s.x3][j+s.y4] - p[s.x3][j+s.y1]
                                   + REAL(3)*(p[s.x3][j+s.y2] - p[s.x3][j+s.y3] + p[s.x1][j+s.y3] - p[s.x1][j+s.y2]);
                            break;
                        case 4:
                            f1 =   p[s.x1][j+s.y1] + p[s.x1][j+s.y3] + p[s.x3][j+s.y1] + p[s.x3][j+s.y3]
                                   - REAL(2)*(p[s.x2][j+s.y1] + p[s.x2][j+s.y3] + p[s.x1][j+s.y2] + p[s.x3][j+s.y2])
                                   + REAL(4)*p[s.x2][j+s.y2];
                            break;
                        default:
#ifndef DEBUG
                            __assume(0);
#else
                            ;
#endif
                        }
                        if(s.parity!=0)
                            if(f1<sq*s.thresh)
                                value += ac[k].alphas[t];
                            else ;
                        else if(f1>=sq*s.thresh)
                            value += ac[k].alphas[t];
                        else ;
                    }
                    if(value<ac[k].thresh)
                    {
                        result = 0;
                        break;
                    }
                }
                if(result!=0)
                {
                    const REAL r = 1.0/ratio;
                    rect.left = (LONG)(j*r);
                    rect.top = (LONG)(i*r);
                    rect.right = (LONG)((j+sy)*r);
                    rect.bottom = (LONG)((i+sx)*r);
                    results.push_back(rect);
                }
            }
        ratio = ratio * REAL(0.8);
        procface.Resize(image,REAL(0.8));
        SwapIntImage(procface,image);
    }

    total_fp += results.size();

    PostProcess(results,2);
    PostProcess(results,0);
    DrawResults(original,results);
//	original.Save(filename+"_result.JPG");
}