Example #1
0
File: main.cpp Project: FaceAR/dlib
void rotate_dataset(const command_line_parser& parser)
{
    image_dataset_metadata::dataset metadata;
    const string datasource = parser[0];
    load_image_dataset_metadata(metadata,datasource);

    double angle = get_option(parser, "rotate", 0);

    // Set the current directory to be the one that contains the
    // metadata file. We do this because the file might contain
    // file paths which are relative to this folder.
    set_current_dir(get_parent_directory(file(datasource)));

    const string file_prefix = "rotated_"+ cast_to_string(angle) + "_";
    const string metadata_filename = get_parent_directory(file(datasource)).full_name() +
        directory::get_separator() + file_prefix + file(datasource).name();


    array2d<rgb_pixel> img, temp;
    for (unsigned long i = 0; i < metadata.images.size(); ++i)
    {
        file f(metadata.images[i].filename);
        const string filename = get_parent_directory(f).full_name() + directory::get_separator() + file_prefix + to_png_name(f.name());

        load_image(img, metadata.images[i].filename);
        const point_transform_affine tran = rotate_image(img, temp, angle*pi/180);
        save_png(temp, filename);

        for (unsigned long j = 0; j < metadata.images[i].boxes.size(); ++j)
        {
            const rectangle rect = metadata.images[i].boxes[j].rect;
            rectangle newrect;
            newrect += tran(rect.tl_corner());
            newrect += tran(rect.tr_corner());
            newrect += tran(rect.bl_corner());
            newrect += tran(rect.br_corner());
            // now make newrect have the same area as the starting rect.
            double ratio = std::sqrt(rect.area()/(double)newrect.area());
            newrect = centered_rect(newrect, newrect.width()*ratio, newrect.height()*ratio);
            metadata.images[i].boxes[j].rect = newrect;

            // rotate all the object parts
            std::map<std::string,point>::iterator k;
            for (k = metadata.images[i].boxes[j].parts.begin(); k != metadata.images[i].boxes[j].parts.end(); ++k)
            {
                k->second = tran(k->second); 
            }
        }

        metadata.images[i].filename = filename;
    }

    save_image_dataset_metadata(metadata, metadata_filename);
}
Example #2
0
int main(int argc, char** argv)
{
    try
    {
        // make sure the user entered an argument to this program
        if (argc != 2)
        {
            cout << "error, you have to enter a BMP file as an argument to this program" << endl;
            return 1;
        }

        // Here we declare an image object that can store rgb_pixels.  Note that in 
        // dlib there is no explicit image object, just a 2D array and
        // various pixel types.  
        array2d<rgb_pixel> img;

        // Now load the image file into our image.  If something is wrong then
        // load_image() will throw an exception.  Also, if you linked with libpng
        // and libjpeg then load_image() can load PNG and JPEG files in addition
        // to BMP files. 
        load_image(img, argv[1]);

        // get the 100 strongest SURF points from the image
        std::vector<surf_point> sp = get_surf_points(img, 100);

        // create a window to display the input image and the SURF boxes.  (Note that
        // you can zoom into the window by holding CTRL and scrolling the mouse wheel)
        image_window my_window(img);

        // Now lets draw some rectangles on top of the image so we can see where
        // SURF found its points.
        for (unsigned long i = 0; i < sp.size(); ++i)
        {
            // Pull out the info from the SURF point relevant to figuring out
            // where its rotated box should be.
            const unsigned long box_size = static_cast<unsigned long>(sp[i].p.scale*20);
            const double ang = sp[i].angle;
            const point center(sp[i].p.center);
            const rectangle rect = centered_rect(center, box_size, box_size); 

            // Rotate the 4 corners of the rectangle 
            const point p1 = rotate_point(center, rect.tl_corner(), ang);
            const point p2 = rotate_point(center, rect.tr_corner(), ang);
            const point p3 = rotate_point(center, rect.bl_corner(), ang);
            const point p4 = rotate_point(center, rect.br_corner(), ang);

            // Draw the sides of the box as red lines
            my_window.add_overlay(p1, p2, rgb_pixel(255,0,0));
            my_window.add_overlay(p1, p3, rgb_pixel(255,0,0));
            my_window.add_overlay(p4, p2, rgb_pixel(255,0,0));
            my_window.add_overlay(p4, p3, rgb_pixel(255,0,0));

            // Draw a line from the center to the top side so we can see how the box is oriented.
            // Also make this line green.
            my_window.add_overlay(center, (p1+p2)/2, rgb_pixel(0,255,0));
        }

        // wait until the user closes the window before we let the program 
        // terminate.
        my_window.wait_until_closed();
    }
    catch (exception& e)
    {
        cout << "exception thrown: " << e.what() << endl;
    }
}