image_ex.cpp 2.73 KB
Newer Older
1
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*

    This is an example illustrating the use of the GUI API as well as some 
    aspects of image manipulation from the dlib C++ Library.


    This is a pretty simple example.  It takes a BMP file on the command line
    and opens it up, runs a simple edge detection algorithm on it, and 
    displays the results on the screen.  
*/



#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
#include "dlib/image_transforms.h"
#include <fstream>


using namespace std;
using namespace dlib;

//  ----------------------------------------------------------------------------

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.  
40
        array2d<rgb_pixel> img;
41

42
        // Now load the image file into our image.  If something is wrong then
Davis King's avatar
Davis King committed
43
44
        // 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
45
46
        // to BMP files.
        load_image(img, argv[1]);
47

48
49
50
51
52
53

        // Now lets use some image functions.  First lets blur the image a little.
        array2d<unsigned char> blurred_img;
        gaussian_blur(img, blurred_img); 

        // Now find the horizontal and vertical gradient images.
54
55
        array2d<short> horz_gradient, vert_gradient;
        array2d<unsigned char> edge_image;
56
        sobel_edge_detector(blurred_img, horz_gradient, vert_gradient);
57
58
59
60

        // now we do the non-maximum edge suppression step so that our edges are nice and thin
        suppress_non_maximum_edges(horz_gradient, vert_gradient, edge_image); 

Davis King's avatar
Davis King committed
61
62
        // Now we would like to see what our images look like.  So lets use a 
        // window to display them on the screen.  (Note that you can zoom into 
63
        // the window by holding CTRL and scrolling the mouse wheel)
Davis King's avatar
Davis King committed
64
        image_window my_window(edge_image);
65
66

        // also make a window to display the original image
Davis King's avatar
Davis King committed
67
        image_window my_window2(img);
68
69
70
71
72
73
74
75
76
77
78
79
80
81

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

//  ----------------------------------------------------------------------------