Commit 71348651 authored by Dmitriy Anisimov's avatar Dmitriy Anisimov

fixed display_image tutorial sample

parent 55c799a4
...@@ -39,28 +39,28 @@ You'll almost always end up using the: ...@@ -39,28 +39,28 @@ You'll almost always end up using the:
.. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp .. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp
:language: cpp :language: cpp
:tab-width: 4 :tab-width: 4
:lines: 1-4 :lines: 1-6
We also include the *iostream* to facilitate console line output and input. To avoid data structure and function name conflicts with other libraries, OpenCV has its own namespace: *cv*. To avoid the need appending prior each of these the *cv::* keyword you can import the namespace in the whole file by using the lines: We also include the *iostream* to facilitate console line output and input. To avoid data structure and function name conflicts with other libraries, OpenCV has its own namespace: *cv*. To avoid the need appending prior each of these the *cv::* keyword you can import the namespace in the whole file by using the lines:
.. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp .. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp
:language: cpp :language: cpp
:tab-width: 4 :tab-width: 4
:lines: 6-7 :lines: 8-9
This is true for the STL library too (used for console I/O). Now, let's analyze the *main* function. We start up assuring that we acquire a valid image name argument from the command line. This is true for the STL library too (used for console I/O). Now, let's analyze the *main* function. We start up assuring that we acquire a valid image name argument from the command line. Otherwise take a picture by default: "HappyFish.jpg".
.. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp .. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp
:language: cpp :language: cpp
:tab-width: 4 :tab-width: 4
:lines: 11-15 :lines: 13-17
Then create a *Mat* object that will store the data of the loaded image. Then create a *Mat* object that will store the data of the loaded image.
.. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp .. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp
:language: cpp :language: cpp
:tab-width: 4 :tab-width: 4
:lines: 17 :lines: 19
Now we call the :imread:`imread <>` function which loads the image name specified by the first argument (*argv[1]*). The second argument specifies the format in what we want the image. This may be: Now we call the :imread:`imread <>` function which loads the image name specified by the first argument (*argv[1]*). The second argument specifies the format in what we want the image. This may be:
...@@ -73,7 +73,7 @@ Now we call the :imread:`imread <>` function which loads the image name specifie ...@@ -73,7 +73,7 @@ Now we call the :imread:`imread <>` function which loads the image name specifie
.. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp .. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp
:language: cpp :language: cpp
:tab-width: 4 :tab-width: 4
:lines: 18 :lines: 20
.. note:: .. note::
...@@ -88,21 +88,21 @@ After checking that the image data was loaded correctly, we want to display our ...@@ -88,21 +88,21 @@ After checking that the image data was loaded correctly, we want to display our
.. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp .. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp
:language: cpp :language: cpp
:lines: 26 :lines: 28
:tab-width: 4 :tab-width: 4
Finally, to update the content of the OpenCV window with a new image use the :imshow:`imshow <>` function. Specify the OpenCV window name to update and the image to use during this operation: Finally, to update the content of the OpenCV window with a new image use the :imshow:`imshow <>` function. Specify the OpenCV window name to update and the image to use during this operation:
.. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp .. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp
:language: cpp :language: cpp
:lines: 27 :lines: 29
:tab-width: 4 :tab-width: 4
Because we want our window to be displayed until the user presses a key (otherwise the program would end far too quickly), we use the :wait_key:`waitKey <>` function whose only parameter is just how long should it wait for a user input (measured in milliseconds). Zero means to wait forever. Because we want our window to be displayed until the user presses a key (otherwise the program would end far too quickly), we use the :wait_key:`waitKey <>` function whose only parameter is just how long should it wait for a user input (measured in milliseconds). Zero means to wait forever.
.. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp .. literalinclude:: ../../../../samples/cpp/tutorial_code/introduction/display_image/display_image.cpp
:language: cpp :language: cpp
:lines: 29 :lines: 31
:tab-width: 4 :tab-width: 4
Result Result
......
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp> #include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui.hpp>
#include <iostream> #include <iostream>
#include <string>
using namespace cv; using namespace cv;
using namespace std; using namespace std;
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
if( argc != 2) string imageName("../data/HappyFish.jpg"); // by default
if( argc > 1)
{ {
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl; imageName = argv[1];
return -1;
} }
Mat image; Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file image = imread(imageName.c_str(), IMREAD_COLOR); // Read the file
if( image.empty() ) // Check for invalid input if( image.empty() ) // Check for invalid input
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment