From 435328569162104db9c14f718f4ba170d1206470 Mon Sep 17 00:00:00 2001 From: abidrahmank Date: Sun, 10 Aug 2014 11:53:46 +0530 Subject: [PATCH] updated bindings section --- .../.py_bindings_basics.rst.swp | Bin 20480 -> 0 bytes .../py_bindings_basics/py_bindings_basics.rst | 14 +++++++------- 2 files changed, 7 insertions(+), 7 deletions(-) delete mode 100644 source/py_tutorials/py_bindings/py_bindings_basics/.py_bindings_basics.rst.swp diff --git a/source/py_tutorials/py_bindings/py_bindings_basics/.py_bindings_basics.rst.swp b/source/py_tutorials/py_bindings/py_bindings_basics/.py_bindings_basics.rst.swp deleted file mode 100644 index 96597f450e4848c194a2b2f4100debc670df067b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeHOOKc=Z8Sb#aZUWgw2`40^N&*|(nVqq{Q5GDc$Z@=}VZFB2-d&!Wsp+nnY22^v zhsOg%NN_;n!X@GnDH4QBB9ORnLm)sx;s8Q{1aaa32Ovl}aYf?$|ElWgnH_r#;vtkC z?eCfHuKKI$tN&fS-NhT5TjEmlQ3s!QInHm^U)g=_XLmWTUUD4S^}SRMLK*GT(<47J zJ^s1v*h-726)Mk{tqSYb`bT$GukLPcUu|tCDq6d-d|_vl58~*;UXjPCFN3U=#YO6> zRx)bm;X(6#*VOkZbG>1{ zADVjpR&)J!^PNmRH<$HSjX;e+jX;e+jX;e+jX;e+jX;e+jX;e+jX;gS{{R8$IS&3g zXDH#v`M=Ho|9Z}GUI+ag^li|Kpcg=21AP_r70{PK9|z5W9tQpWEZ!dg{TlTAI~?a5 zpx_?I*$3T!#&PZgT}Odn5A-Yw3ZDag3*&whbOUr9v+i z$YY@na}{|aQbQ5OUJ!_V?LKkP3x@Mn3x(-D4Tmfk>P_h4mZOXj$pvH|-I17_?%c;%Xd;@eN0JWVGcC02c|}SRTa{9vN<(%!-NFyNmQf$P!9%b7v}iBm zfDi##olY_W-jRFIE;XXP&>T=B67Bh>c&f;W9dssys6t}07;tNS(+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l@JiE>jYIvXscykmLn)Z19Q#?;Uc&P zEWQqakp@!!PJ-l0m<@6?Mvljvx~P*4DPc@irK-RQE|O5xKM{tY#()DCngG|@Gakpo zB%r7grAwq$hhSX}TNX~owxdmp#Wcp5FbowXF(MwLXDpEQg{6#RtJ%6A_-T%Eunmq- zfmsXu(d(F4xtRk5NZU?2O6BDcW1DSfL5}?e(!@>xZ zISx2{}6$#Rj~KdQWbLZsG**Shog2rX83aRbMWTv+fwj zU}`*hlyk<>FN6&>^&!~>GDJv!Cw7JFt!%<7h0KhOmtgi#1ma?bEr!_B0DBfun#;=R z1L7eZteF!;Z8NwWh+{+?j__v4h^jjJ09Ui*f*Ku6qOLblX3@={ShTmP3K4EoiNy|# zUACIZ62R=ISabgW+p~^y4|09V|B*`b=Lfic4zvy006hbuHtV4JQzK9#P$N(yP$N(y zP$N(yP$N(yP$N(yP$TgFi$M7v7bV(MwhZN`QTO1(2!3zdCiu26NkvC}v~O#M6ScqT zqsgeCo#QmuwNqg!W;Z10M{Ti>ORt|uv(1q&C^jIN} W74ldij}`J*xKD?l1)qNMSpNW%h@z|j diff --git a/source/py_tutorials/py_bindings/py_bindings_basics/py_bindings_basics.rst b/source/py_tutorials/py_bindings/py_bindings_basics/py_bindings_basics.rst index 7334123..7e48930 100644 --- a/source/py_tutorials/py_bindings/py_bindings_basics/py_bindings_basics.rst +++ b/source/py_tutorials/py_bindings/py_bindings_basics/py_bindings_basics.rst @@ -14,17 +14,17 @@ Learn: How OpenCV-Python bindings are generated? ========================================= -In OpenCV, all algorithms are implemented in C++. But these algorithms can be used from different languages like Python, Java etc. This is made possible by the bindings generator files. These files form a bridge between C++ and Python which enable users to call C++ functions from Python. To get a complete picture of what is happening background, a good knowledge of Python/C API is required. A simple example on extending C++ functions to Python can be found in official Python documentation[1]. So extending every function in OpenCV to Python by writing its wrapper function is a time-consuming task. So OpenCV does it more intelligent way. OpenCV generates these wrapper functions automatically from the C++ headers using some Python scripts which are located in ``modules/python/src2``. We will look into what they do. +In OpenCV, all algorithms are implemented in C++. But these algorithms can be used from different languages like Python, Java etc. This is made possible by the bindings generators. These generators create a bridge between C++ and Python which enables users to call C++ functions from Python. To get a complete picture of what is happening in background, a good knowledge of Python/C API is required. A simple example on extending C++ functions to Python can be found in official Python documentation[1]. So extending all functions in OpenCV to Python by writing their wrapper functions manually is a time-consuming task. So OpenCV does it in a more intelligent way. OpenCV generates these wrapper functions automatically from the C++ headers using some Python scripts which are located in ``modules/python/src2``. We will look into what they do. -First, ``modules/python/CMakeFiles.txt`` is a cmake script which checks the modules to be extended to Python. It will automatically check all the modules to be extended and grab their header files. These header files contain list of all classes, functions, constants etc. for that particular modules. +First, ``modules/python/CMakeFiles.txt`` is a CMake script which checks the modules to be extended to Python. It will automatically check all the modules to be extended and grab their header files. These header files contain list of all classes, functions, constants etc. for that particular modules. -Second, these header files are passed to a Python script, ``modules/python/src2/gen2.py``. He is the Python bindings generator script. It calls another Python script ``modules/python/src2/hdr_parser.py``. This is the header parser file. This guy splits the complete header file into small Python lists. So these lists contain all details about a particular function, class etc. For example, a function will be parsed to get a list containing function name, return type, input arguments, argument types etc. Final list contains details of all the functions, structs, classes etc. in that header file. +Second, these header files are passed to a Python script, ``modules/python/src2/gen2.py``. This is the Python bindings generator script. It calls another Python script ``modules/python/src2/hdr_parser.py``. This is the header parser script. This header parser splits the complete header file into small Python lists. So these lists contain all details about a particular function, class etc. For example, a function will be parsed to get a list containing function name, return type, input arguments, argument types etc. Final list contains details of all the functions, structs, classes etc. in that header file. -But header parser doesn't parse all the functions/classes in the header file. Instead, there are certain macros added to the beginning of these declarations which enables the header parser to identify functions to be parsed. These macros are added by the developer who programs the particular function. So the developer decides which functions should be extended to Python and which are not. Details of the macros will be given in next session. +But header parser doesn't parse all the functions/classes in the header file. The developer has to specify which functions should be exported to Python. For that, there are certain macros added to the beginning of these declarations which enables the header parser to identify functions to be parsed. These macros are added by the developer who programs the particular function. In short, the developer decides which functions should be extended to Python and which are not. Details of those macros will be given in next session. So header parser returns a final big list of parsed functions. Our generator script (gen2.py) will create wrapper functions for all the functions/classes/enums/structs parsed by header parser (You can find these header files during compilation in the ``build/modules/python/`` folder as ``pyopencv_generated_*.h`` files). But there may be some basic OpenCV datatypes like Mat, Vec4i, Size. They need to be extended manually. For example, a Mat type should be extended to Numpy array, Size should be extended to a tuple of two integers etc. Similarly, there may be some complex structs/classes/functions etc. which need to be extended manually. All such manual wrapper functions are placed in ``modules/python/src2/pycv2.hpp``. -So now only thing left is the compilation of these wrapper files which gives us **cv2** module. So when you call a function, say ``res = equalizeHist(img1,img2)`` in Python, ``cv::Mat`` structures corresponding to ``img1``, ``img2`` and ``res`` will be formed and calls the ``equalizeHist()`` function in C++. Final result, ``res`` will be converted back into a Numpy array. So in short, almost all operations are done in C++ which gives us almost same speed as that of C++. +So now only thing left is the compilation of these wrapper files which gives us **cv2** module. So when you call a function, say ``res = equalizeHist(img1,img2)`` in Python, you pass two numpy arrays and you expect another numpy array as the output. So these numpy arrays are converted to ``cv::Mat`` and then calls the ``equalizeHist()`` function in C++. Final result, ``res`` will be converted back into a Numpy array. So in short, almost all operations are done in C++ which gives us almost same speed as that of C++. So this is the basic version of how OpenCV-Python bindings are generated. @@ -40,7 +40,7 @@ Functions are extended using ``CV_EXPORTS_W`` macro. An example is shown below. CV_EXPORTS_W void equalizeHist( InputArray src, OutputArray dst ); -Header parser can understand the input and output arguments from keywords like ``InputArray, OutputArray`` etc. But sometimes, we may need to hard-code inputs and outputs. For that, macros like ``CV_OUT, CV_IN_OUT`` etc. are used. +Header parser can understand the input and output arguments from keywords like ``InputArray, OutputArray`` etc. But sometimes, we may need to hardcode inputs and outputs. For that, macros like ``CV_OUT, CV_IN_OUT`` etc. are used. .. code-block:: cpp @@ -109,4 +109,4 @@ Some other small classes/structs can be exported using ``CV_EXPORTS_W_MAP`` wher CV_PROP_RW double nu20, nu11, nu02, nu30, nu21, nu12, nu03; }; -So these are the major extension macros available in OpenCV. +So these are the major extension macros available in OpenCV. Typically, a developer has to put proper macros in their appropriate positions. Rest is done by generator scripts. Sometimes, there may be an exceptional cases where generator scripts cannot create the wrappers. Such functions need to be handled manually. But most of the time, a code written according to OpenCV coding guidelines will be automatically wrapped by generator scripts.