From: Bernat Gabor Date: Mon, 8 Aug 2011 05:58:10 +0000 (+0000) Subject: Added the "Interoperability with OpenCV 1" tutorial. X-Git-Tag: accepted/2.0/20130307.220821~2133 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ed801d3e6523e593d94ae75c5d9857239bdd86e4;p=profile%2Fivi%2Fopencv.git Added the "Interoperability with OpenCV 1" tutorial. --- diff --git a/doc/tutorials/core/interoperability_with_OpenCV_1/images/outputInteropOpenCV1.jpg b/doc/tutorials/core/interoperability_with_OpenCV_1/images/outputInteropOpenCV1.jpg new file mode 100644 index 0000000..b94b0ec Binary files /dev/null and b/doc/tutorials/core/interoperability_with_OpenCV_1/images/outputInteropOpenCV1.jpg differ diff --git a/doc/tutorials/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.rst b/doc/tutorials/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.rst new file mode 100644 index 0000000..f1669f4 --- /dev/null +++ b/doc/tutorials/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.rst @@ -0,0 +1,132 @@ +.. _InteroperabilityWithOpenCV1: + +Interoperability with OpenCV 1 +****************************** + +Goal +==== + +For the OpenCV developer team it's important to constantly improve the library. We are constantly thinking about methods that will ease your work process, while still maintain the libraries flexibility. The new C++ interface is a development of us that serves this goal. Nevertheless, backward compatibility remains important. We do not want to break your code written for earlier version of the OpenCV library. Therefore, we made sure that we add some functions that deal with this. In the following you'll learn: + +.. container:: enumeratevisibleitemswithsquare + + + What changed with the version 2 of OpenCV in the way you use the library compared to its first version + + How to add some Gaussian noise to an image + + What are lookup tables and why use them? + +General +======= + +When making the switch you first need to learn some about the new data structure for images: :ref:`matTheBasicImageContainer`, this replaces the old *CvMat* and *IplImage* ones. Switching to the new functions is easier. You just need to remember a couple of new things. + +OpenCV 2 received reorganization. No longer are all the functions crammed into a single library. We have many modules, each of them containing data structures and functions relevant to certain tasks. This way you do not need to ship a large library if you use just a subset of OpenCV. This means that you should also include only those headers you will use. For example: + +.. code-block:: cpp + + #include + #include + #include + + +All the OpenCV related stuff is put into the *cv* namespace to avoid name conflicts with other libraries data structures and functions. Therefore, either you need to prepend the *cv::* keyword before everything that comes from OpenCV or after the includes, you just add a directive to use this: + +.. code-block:: cpp + + using namespace cv; // The new C++ interface API is inside this namespace. Import it. + +Because the functions are already in a namespace there is no need for them to contain the *cv* prefix in their name. As such all the new C++ compatible functions don't have this and they follow the camel case naming rule. This means the first letter is small (unless it's a name, like Canny) and the subsequent words start with a capital letter (like *copyMakeBorder*). + +Now, remember that you need to link to your application all the modules you use, and in case you are on Windows using the *DLL* system you will need to add, again, to the path all the binaries. For more in-depth information if you're on Windows read :ref:`Windows_Visual_Studio_How_To` and for Linux an example usage is explained in :ref:`Linux_Eclipse_Usage`. + +Now for converting the *Mat* object you can use either the *IplImage* or the *CvMat* operators. While in the C interface you used to work with pointers here it's no longer the case. In the C++ interface we have mostly *Mat* objects. These objects may be freely converted to both *IplImage* and *CvMat* with simple assignment. For example: + +.. code-block:: cpp + + Mat I; + IplImage pI = I; + CvMat mI = I; + +Now if you want pointers the conversion gets just a little more complicated. The compilers can no longer automatically determinate what you want and as you need to explicitly specify your goal. This is to call the *IplImage* and *CvMat* operators and then get their pointers. For getting the pointer we use the & sign: + +.. code-block:: cpp + + Mat I; + IplImage* pI = &I.operator IplImage(); + CvMat* mI = &I.operator CvMat(); + +One of the biggest complaints of the C interface is that it leaves all the memory management to you. You need to figure out when it is safe to release your unused objects and make sure you do so before the program finishes or you could have troublesome memory leeks. To work around this issue in OpenCV there is introduced a sort of smart pointer. This will automatically release the object when it's no longer in use. To use this declare the pointers as a specialization of the *Ptr* : + +.. code-block:: cpp + + Ptr piI = &I.operator IplImage(); + +Converting from the C data structures to the *Mat* is done by passing these inside its constructor. For example: + +.. code-block:: cpp + + Mat K(piL), L; + L = Mat(pI); + +A case study +============ + +Now that you have the basics done :download:`here's <../../../../samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp>` an example that mixes the usage of the C interface with the C++ one. You will also find it in the sample directory of the OpenCV source code library at the :file:`samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp` . To further help on seeing the difference the programs supports two modes: one mixed C and C++ and one pure C++. If you define the *DEMO_MIXED_API_USE* you'll end up using the first. The program separates the color planes, does some modifications on them and in the end merge them back together. + + +.. literalinclude:: ../../../../samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp + :language: cpp + :linenos: + :tab-width: 4 + :lines: 1-9, 22-25, 27-44 + +Here you can observe that with the new structure we have no pointer problems, although it is possible to use the old functions and in the end just transform the result to a *Mat* object. + +.. literalinclude:: ../../../../samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp + :language: cpp + :linenos: + :tab-width: 4 + :lines: 46-51 + +Because, we want to mess around with the images luma component we first convert from the default RGB to the YUV color space and then split the result up into separate planes. Here the program splits: in the first example it processes each plane using one of the three major image scanning algorithms in OpenCV (C [] operator, iterator, individual element access). In a second variant we add to the image some Gaussian noise and then mix together the channels according to some formula. + +The scanning version looks like: + +.. literalinclude:: ../../../../samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp + :language: cpp + :linenos: + :tab-width: 4 + :lines: 55-75 + +Here you can observe that we may go through all the pixels of an image in three fashions: an iterator, a C pointer and an individual element access style. You can read a more in-depth description of these in the :ref:`howToScanImagesOpenCV` tutorial. Converting from the old function names is easy. Just remove the cv prefix and use the new *Mat* data structure. Here's an example of this by using the weighted addition function: + +.. literalinclude:: ../../../../samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp + :language: cpp + :linenos: + :tab-width: 4 + :lines: 79-112 + +As you may observe the *planes* variable is of type *Mat*. However, converting from *Mat* to *IplImage* is easy and made automatically with a simple assignment operator. + +.. literalinclude:: ../../../../samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp + :language: cpp + :linenos: + :tab-width: 4 + :lines: 115-127 + +The new *imshow* highgui function accepts both the *Mat* and *IplImage* data structures. Compile and run the program and if the first image below is your input you may get either the first or second as output: + +.. image:: images/outputInteropOpenCV1.jpg + :alt: The output of the sample + :align: center + + +You may observe a runtime instance of this on the `YouTube here `_ and you can :download:`download the source code from here <../../../../samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp>` or find it in the :file:`samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp` of the OpenCV source code library. + +.. raw:: html + +
+ +
+ + + diff --git a/doc/tutorials/core/table_of_content_core/images/interopOpenCV1.png b/doc/tutorials/core/table_of_content_core/images/interopOpenCV1.png new file mode 100644 index 0000000..040f50a Binary files /dev/null and b/doc/tutorials/core/table_of_content_core/images/interopOpenCV1.png differ diff --git a/doc/tutorials/core/table_of_content_core/table_of_content_core.rst b/doc/tutorials/core/table_of_content_core/table_of_content_core.rst index 92c0acd..fe5e43a 100644 --- a/doc/tutorials/core/table_of_content_core/table_of_content_core.rst +++ b/doc/tutorials/core/table_of_content_core/table_of_content_core.rst @@ -181,6 +181,27 @@ Here you will learn the about the basic building blocks of the library. A must r :height: 90pt :width: 90pt ++ + .. tabularcolumns:: m{100pt} m{300pt} + .. cssclass:: toctableopencv + + =============== ====================================================== + |InterOOpenCV1| **Title:** :ref:`InteroperabilityWithOpenCV1` + + *Compatibility:* > OpenCV 2.0 + + *Author:* |Author_BernatG| + + Did you used OpenCV before its 2.0 version? Do you wanna know what happened with your library with 2.0? Don't you know how to convert your old OpenCV programs to the new C++ interface? Look here to shed light on all this questions. + + =============== ====================================================== + + .. |InterOOpenCV1| image:: images/interopOpenCV1.png + :height: 90pt + :width: 90pt + + + .. raw:: latex \pagebreak @@ -196,4 +217,5 @@ Here you will learn the about the basic building blocks of the library. A must r ../basic_geometric_drawing/basic_geometric_drawing ../random_generator_and_text/random_generator_and_text ../discrete_fourier_transform/discrete_fourier_transform - ../file_input_output_with_xml_yml/file_input_output_with_xml_yml \ No newline at end of file + ../file_input_output_with_xml_yml/file_input_output_with_xml_yml + ../interoperability_with_OpenCV_1/interoperability_with_OpenCV_1 \ No newline at end of file diff --git a/samples/cpp/tutorial_code/core/switching_to_the_new_interface/switching_to_the_new_interface.txt b/samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp similarity index 96% rename from samples/cpp/tutorial_code/core/switching_to_the_new_interface/switching_to_the_new_interface.txt rename to samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp index 4e004e1..5412b7b 100644 --- a/samples/cpp/tutorial_code/core/switching_to_the_new_interface/switching_to_the_new_interface.txt +++ b/samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp @@ -50,7 +50,7 @@ int main( int argc, char** argv ) vector planes; // Use the STL's vector structure to store multiple Mat objects split(I_YUV, planes); // split the image into separate color planes (Y U V) -#if 0 // change it to 0 if you want to see a blurred and noisy version of this processing +#if 1 // change it to 0 if you want to see a blurred and noisy version of this processing // Mat scanning // Method 1. process Y plane using an iterator MatIterator_ it = planes[0].begin(), it_end = planes[0].end();