load_save_image.rst 3.57 KB
Newer Older
1 2
.. _Load_Save_Image:

3 4
Load, Modify, and Save an Image
*******************************
5 6 7

.. note::

8
   We assume that by now you know how to load an image using :readwriteimagevideo:`imread <imread>` and to display it in a window (using :user_interface:`imshow <imshow>`). Read the :ref:`Display_Image` tutorial otherwise.
9

10 11 12 13 14
Goals
======

In this tutorial you will learn how to:

15 16
.. container:: enumeratevisibleitemswithsquare

17 18 19
   * Load an image using :readwriteimagevideo:`imread <imread>`
   * Transform an image from BGR to Grayscale format by using :miscellaneous_transformations:`cvtColor <cvtcolor>`
   * Save your transformed image in a file on disk (using :readwriteimagevideo:`imwrite <imwrite>`)
20 21 22 23 24 25 26 27 28

Code
======

Here it is:

.. code-block:: cpp
   :linenos:

29
   #include <opencv2/opencv.hpp>
30 31 32 33 34 35 36

   using namespace cv;

   int main( int argc, char** argv )
   {
    char* imageName = argv[1];

37
    Mat image;
38
    image = imread( imageName, 1 );
39

40 41 42 43 44 45 46
    if( argc != 2 || !image.data )
    {
      printf( " No image data \n " );
      return -1;
    }

    Mat gray_image;
47
    cvtColor( image, gray_image, COLOR_BGR2GRAY );
48

49
    imwrite( "../../images/Gray_Image.jpg", gray_image );
50

51 52
    namedWindow( imageName, WINDOW_AUTOSIZE );
    namedWindow( "Gray image", WINDOW_AUTOSIZE );
53 54

    imshow( imageName, image );
55
    imshow( "Gray image", gray_image );
56 57 58 59 60 61 62 63 64

    waitKey(0);

    return 0;
   }

Explanation
============

65
#. We begin by loading an image using :readwriteimagevideo:`imread <imread>`, located in the path given by *imageName*. For this example, assume you are loading a RGB image.
66

Andrey Kamaev's avatar
Andrey Kamaev committed
67
#. Now we are going to convert our image from BGR to Grayscale format. OpenCV has a really nice function to do this kind of transformations:
68 69

   .. code-block:: cpp
70

71
      cvtColor( image, gray_image, CV_BGR2GRAY );
72

73
   As you can see, :miscellaneous_transformations:`cvtColor <cvtcolor>` takes as arguments:
74

75
   .. container:: enumeratevisibleitemswithsquare
76

77
      * a source image (*image*)
78
      * a destination image (*gray_image*), in which we will save the converted image.
79
      * an additional parameter that indicates what kind of transformation will be performed. In this case we use **CV_BGR2GRAY** (because of :readwriteimagevideo:`imread <imread>` has BGR default channel order in case of color images).
80

81
#. So now we have our new *gray_image* and want to save it on disk (otherwise it will get lost after the program ends). To save it, we will use a function analagous to :readwriteimagevideo:`imread <imread>`: :readwriteimagevideo:`imwrite <imwrite>`
82 83 84

   .. code-block:: cpp

85
      imwrite( "../../images/Gray_Image.jpg", gray_image );
86

87
   Which will save our *gray_image* as *Gray_Image.jpg* in the folder *images* located two levels up of my current location.
88

89
#. Finally, let's check out the images. We create two windows and use them to show the original image as well as the new one:
90 91 92 93 94 95 96 97 98

   .. code-block:: cpp

      namedWindow( imageName, CV_WINDOW_AUTOSIZE );
      namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );

      imshow( imageName, image );
      imshow( "Gray image", gray_image );

99
#. Add the *waitKey(0)* function call for the program to wait forever for an user key press.
100 101 102 103 104 105 106


Result
=======

When you run your program you should get something like this:

107
 .. image:: images/Load_Save_Image_Result_1.jpg
108 109 110
    :alt: Load Save Image Result 1
    :align: center

111
And if you check in your folder (in my case *images*), you should have a newly .jpg file named *Gray_Image.jpg*:
112

113
 .. image:: images/Load_Save_Image_Result_2.jpg
114 115 116
    :alt: Load Save Image Result 2
    :align: center

117
Congratulations, you are done with this tutorial!