Пример #1
0
void testRadialInversion(int scale)
{
    RGB24Buffer *image = new RGB24Buffer(250 * scale, 400 * scale);

    auto operation = [](int i, int j, RGBColor *pixel)
    {
        i = i / 100;
        j = j / 200;
        if ( (i % 2) &&  (j % 2))   *pixel = RGBColor::Green();
        if (!(i % 2) &&  (j % 2))   *pixel = RGBColor::Yellow();
        if ( (i % 2) && !(j % 2))   *pixel = RGBColor::Red();
        if (!(i % 2) && !(j % 2))   *pixel = RGBColor::Blue();
    };
    touchOperationElementwize(image, operation);

#if 0
    LensDistortionModelParameters deformator;
    deformator.setPrincipalX(image->w / 2);
    deformator.setPrincipalY(image->h / 2);
    deformator.setNormalizingFocal(deformator.principalPoint().l2Metric());

    deformator.setTangentialX(0.001);
    deformator.setTangentialY(0.001);

    deformator.setAspect(1.0);
    deformator.setScale(1.0);

    deformator.mKoeff.push_back( 0.1);
    deformator.mKoeff.push_back(-0.2);
    deformator.mKoeff.push_back( 0.3);
#else
    LensDistortionModelParameters deformator;
    deformator.setMapForward(false);
    deformator.setPrincipalX(480);
    deformator.setPrincipalY(360);
    deformator.setNormalizingFocal(734.29999999999995);

    deformator.setTangentialX(0.00);
    deformator.setTangentialY(0.00);

    deformator.setShiftX(0.00);
    deformator.setShiftY(0.00);


    deformator.setAspect(1.0);
    deformator.setScale (1.0);

    deformator.mKoeff.clear();
    deformator.mKoeff.push_back( 0);
    deformator.mKoeff.push_back( -0.65545);
    deformator.mKoeff.push_back( 0);
    deformator.mKoeff.push_back( 8.2439);
//    deformator.mKoeff.push_back( 0);
//    deformator.mKoeff.push_back( 8.01);
#endif


    RadialCorrection T(deformator);
    PreciseTimer timer;

    cout << "Initial deformation... " << endl;
    cout << T.mParams << flush;;

    cout << "Starting deformation... " << flush;
    timer = PreciseTimer::currentTime();
    RGB24Buffer *deformed = image->doReverseDeformationBlTyped<RadialCorrection>(&T);
    cout << "done in: " << timer.usecsToNow() << "us" << endl;

    /* */
    int inversionGridStep = 30;

    cout << "Starting invertion... " << flush;
    RadialCorrection invert = T.invertCorrection(image->h, image->w, inversionGridStep);
    cout << "done" << endl;

    cout << "Starting backprojection... " << flush;
    timer = PreciseTimer::currentTime();
    RGB24Buffer *backproject = deformed->doReverseDeformationBlTyped<RadialCorrection>(&invert);
    cout << "done in: " << timer.usecsToNow() << "us" << endl;
    cout << "done" << endl;


    RGB24Buffer *debug = new RGB24Buffer(image->getSize());
    /* Show visual */
    double dh = (double)image->h / (inversionGridStep - 1);
    double dw = (double)image->w / (inversionGridStep - 1);
    for (int i = 0; i < inversionGridStep; i++)
    {
        for (int j = 0; j < inversionGridStep; j++)
        {
             Vector2dd point(dw * j, dh * i);
             debug->drawCrosshare1(point, RGBColor::Yellow());
             Vector2dd deformed    = T.mapToUndistorted(point); /* this could be cached */
             Vector2dd backproject = invert.mapToUndistorted(deformed);

             debug->drawCrosshare1(backproject, RGBColor::Green());
        }
    }

    BMPLoader().save("input.bmp"      , image);
    BMPLoader().save("debug.bmp"      , debug);
    BMPLoader().save("forward.bmp"    , deformed);
    BMPLoader().save("backproject.bmp", backproject);

    delete_safe(image);
    delete_safe(debug);
    delete_safe(deformed);
    delete_safe(backproject);
}
//int main (int /*argC*/, char *argV[])
TEST(FaceRecognition1, main)
{
    const double UP_FACTOR = 5;
    const double UP_STEP   = 1.2;

//    const double DOWN_FACTOR = 0.25;
//    const double DOWN_STEP = 1.0 / 1.2;


    fstream patternFile;
    patternFile.open("boost-result.txt", fstream::in);
    VJAdaBoostedClassifier *classifier = new VJAdaBoostedClassifier();
    patternFile >> (*classifier);

    for (unsigned i = 0; i < classifier->children.size(); i++)
    {
        char name[100];
        snprintf2buf(name, "pattern%d.bmp", i);

        VJSimpleClassifier* partClass = classifier->children.at(i);
        RGB24Buffer *buffer = partClass->drawPattern();
        (BMPLoader()).save(string(name), buffer);
        delete buffer;
    }
//    classifier->print();
    patternFile.close();
    G12Buffer *inputPicture = BufferFactory::getInstance()->loadG12Bitmap(argV[1]);
    if (inputPicture == NULL)
    {
        printf("Problem loading input %s\n", argV[1]);
    }
    RGB24Buffer *output = new RGB24Buffer(inputPicture);

    G12IntegralBuffer *integral = new G12IntegralBuffer(inputPicture);

    int total = 0;
    int positive = 0;

    double factor;

    for (factor = 1.0; factor < UP_FACTOR; factor *= UP_STEP)
    {
        VJAdaBoostedClassifier *scaled = classifier->scale(factor);
        scaled->initLimits();
        double x = scaled->leftMargin;
        double y = scaled->topMargin;

        double patw = scaled->rightMargin + scaled->leftMargin;
        double path = scaled->topMargin + scaled->bottomMargin;

        int hlimit = inputPicture->h - scaled->bottomMargin;
        int wlimit = inputPicture->w - scaled->rightMargin;

        for (; y < hlimit ; y += factor)
        {
            for (; x < wlimit ; x += factor)
            {
                bool hasMatch = scaled->applyToPoint(integral, y, x);
                if (hasMatch)
                {
                    //printf("Match Found! at %d %d level %d\n",j ,i, l);
                    int h = path;
                    int w = patw;
                    output->drawCrosshare1(x,     y      , RGBColor(0xFF0000));
                    output->drawCrosshare1(x + w, y      , RGBColor(0x7F0000));
                    output->drawCrosshare1(x,     y + h  , RGBColor(0x00FF00));
                    output->drawCrosshare1(x + w, y + h  , RGBColor(0x007F00));
                    positive++;
                }
                total++;
            }
        }
        delete scaled;
    }


    delete integral;

    printf("Found %d faces among %d objects %2.2lf%%\n", positive, total, (double) 100.0 * positive / total );
    (BMPLoader()).save("output.bmp", output);
//    return 0;
}