File: load_save_image.rst

package info (click to toggle)
opencv 2.4.9.1%2Bdfsg-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 126,800 kB
  • ctags: 62,729
  • sloc: xml: 509,055; cpp: 490,794; lisp: 23,208; python: 21,174; java: 19,317; ansic: 1,038; sh: 128; makefile: 72
file content (118 lines) | stat: -rw-r--r-- 3,665 bytes parent folder | download | duplicates (3)
1
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
.. _Load_Save_Image:

Load, Modify, and Save an Image
*******************************

.. note::

   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.

Goals
======

In this tutorial you will learn how to:

.. container:: enumeratevisibleitemswithsquare

   * 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>`)

Code
======

Here it is:

.. code-block:: cpp
   :linenos:

   #include <cv.h>
   #include <highgui.h>

   using namespace cv;

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

    Mat image;
    image = imread( imageName, 1 );

    if( argc != 2 || !image.data )
    {
      printf( " No image data \n " );
      return -1;
    }

    Mat gray_image;
    cvtColor( image, gray_image, CV_BGR2GRAY );

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

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

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

    waitKey(0);

    return 0;
   }

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

#. 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.

#. 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:

   .. code-block:: cpp

      cvtColor( image, gray_image, CV_BGR2GRAY );

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

   .. container:: enumeratevisibleitemswithsquare

      * a source image (*image*)
      * a destination image (*gray_image*), in which we will save the converted image.
      * 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).

#. 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>`

   .. code-block:: cpp

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

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

#. 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:

   .. code-block:: cpp

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

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

#. Add the *waitKey(0)* function call for the program to wait forever for an user key press.


Result
=======

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

 .. image:: images/Load_Save_Image_Result_1.jpg
    :alt: Load Save Image Result 1
    :align: center

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

 .. image:: images/Load_Save_Image_Result_2.jpg
    :alt: Load Save Image Result 2
    :align: center

Congratulations, you are done with this tutorial!