1 Writing documentation for OpenCV {#tutorial_documentation}
2 ================================
6 Doxygen overview {#tutorial_documentation_overview}
9 Intro {#tutorial_documentation_intro}
12 [Doxygen] is documentation generation system with a lot of great features, such as:
13 - parse program sources to produce actual and accurate documentation
14 - check documentation for errors
15 - insert images and formulas
16 - use markdown syntax and plain HTML for precise text formatting
17 - generate documentation in many different formats
19 OpenCV library existing documentation has been converted to doxygen format.
21 Installation {#tutorial_documentation_install}
24 Please, check official [download][Doxygen download] and [installation][Doxygen installation] pages.
25 Some linux distributions can also provide doxygen packages.
27 Generate documentation {#tutorial_documentation_generate}
28 ----------------------
30 - Get the OpenCV sources (version 3.0 and later)
31 - _Optional:_ get the OpenCV_contrib sources
32 - Create build directory near the sources folder(s) and go into it
33 - Run cmake (assuming you put sources to _opencv_ folder):
35 cmake -DBUILD_DOCS=ON ../opencv
37 Or if you get contrib sources too:
39 cmake -DBUILD_DOCS=ON -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv
45 - Open <i>doc/doxygen/html/index.html</i> file in your favorite browser
46 - Test your Python code:
51 Quick start {#tutorial_documentation_quick_start}
54 @note These instructions are specific to OpenCV library documentation, other projects can use
55 different layout scheme and documenting agreements.
57 Documentation locations {#tutorial_documentation_quick_start_1}
58 -----------------------
60 Whole documentation is gathered from many different places:
62 - __source code__ entities, like classes, functions or enumerations, should be documented in
63 corresponding header files, right prior entity definition. See examples in next sections.
65 - __pages__ are good place to put big pieces of text with images and code examples not directly
66 connected with any source code entity. Pages should be located in separate files and
67 contained in several predefined places. This tutorial is example of such page.
69 - __images__ can be used to illustrate described things. Usually located at the same places as pages,
70 images can be inserted to any place of the documentation.
72 - __code examples__ show how to use the library in real applications. Each sample is
73 self-contained file which represents one simple application. Parts of these files can be
74 included into documentation and tutorials to demonstrate function calls and objects collaboration.
76 - __BibTeX references__ are used to create one common bibliography. All science books, articles and
77 proceedings served as basis for library functionality should be put in this reference list.
79 Following scheme represents common documentation places for _opencv_ repository:
82 ├── doc - doxygen config files, root page (root.markdown.in), BibTeX file (opencv.bib)
83 │ ├── tutorials - tutorials hierarchy (pages and images)
84 │ └── py_tutorials - python tutorials hierarchy (pages and images)
87 │ ├── doc - documentation pages and images for module
88 │ └── include - code documentation in header files
89 └── samples - place for all code examples
91 │ └── tutorial_code - place for tutorial code examples
95 @note Automatic code parser looks for all header files (<i>".h, .hpp"</i> except for <i>".inl.hpp;
96 .impl.hpp; _detail.hpp"</i>) in _include_ folder and its subfolders. Some module-specific
97 instructions (group definitions) and documentation should be put into
98 <i>"include/opencv2/<module-name>.hpp"</i> file.
100 @note You can put C++ template implementation and specialization to separate files
101 (<i>".impl.hpp"</i>) ignored by doxygen.
103 @note Files in _src_ subfolder are not parsed, because documentation is intended mostly for the
104 library users, not developers. But it still is possible to generate full documentation by
105 customizing processed files list in cmake script (<i>doc/CMakeLists.txt</i>) and doxygen options in
106 its configuration file (<i>doc/Doxyfile.in</i>).
108 Since version 3.0 all new modules are placed into _opencv_contrib_ repository, it has slightly
114 ├── doc - documentation pages and images, BibTeX file (<modulename>.bib)
115 ├── include - code documentation in header files
116 ├── samples - place for code examples for documentation and tutorials
117 └── tutorials - tutorial pages and images
120 Example {#tutorial_documentation_quick_start_2}
123 To add documentation for functions, classes and other entities, just insert special comment prior
124 its definition. Like this:
127 /** @brief Calculates the exponent of every array element.
129 The function exp calculates the exponent of every element of the input array:
130 \f[ \texttt{dst} [I] = e^{ src(I) } \f]
132 The maximum relative error is about 7e-6 for single-precision input and less than 1e-10 for
133 double-precision input. Currently, the function converts denormalized values to zeros on output.
134 Special values (NaN, Inf) are not handled.
136 @param src input array.
137 @param dst output array of the same size and type as src.
139 @sa log , cartToPolar , polarToCart , phase , pow , sqrt , magnitude
141 CV_EXPORTS_W void exp(InputArray src, OutputArray dst);
146 - special C-comment syntax denotes it is doxygen comment
147 @verbatim /** ... */ @endverbatim
149 - command `brief` denotes following paragraph is a brief description
150 @verbatim @brief @endverbatim
152 - empty line denotes paragraph end
154 - TeX formula between `f[` and `f]` commands
155 @verbatim \f[ ... \f] @endverbatim
157 - command `param` denotes following word is name of the parameter and following text is
158 description of the parameter; all parameters are placed in a list
159 @verbatim @param @endverbatim
161 - command `sa` starts "See also" paragraph containing references to some classes, methods, pages or URLs.
162 @verbatim @sa @endverbatim
164 Produced reference item looks like this:
165 ![Reference link](doxygen-2.png)
167 The "More..." link brings you to the function documentation:
168 ![Function documentation](doxygen-1.png)
171 Another example {#tutorial_documentation_quick_start_3}
174 Different comment syntax can be used for one-line short comments:
180 LINE_4 = 4, //!< 4-connected line
181 LINE_8 = 8, //!< 8-connected line
182 LINE_AA = 16 //!< antialiased line
188 - special C++-comment syntax denotes it is doxygen comment
189 @verbatim //! @endverbatim
191 - additional symbol `<` denotes this comment is located _after_ documented entity
192 @verbatim //!< @endverbatim
194 Produced documentation block looks like this:
195 ![Enumeration documentation](doxygen-3.png)
197 More details {#tutorial_documentation_quick_start_4}
202 Doxygen commands starts with `@` or `\` sign:
211 Doxygen comment can have different forms:
223 Lines can start with '*':
229 Can be placed after documented entity:
236 To end paragraph, insert empty line or any command starting new paragraph:
238 @brief brief description paragraph
243 @note new note paragraph
244 note paragraph continues
252 Pages, anchors, groups and other named entities should have unique name inside the whole project.
253 It is a good idea to prefix such identifiers with module name:
255 @page core_explanation_1 Usage explanation
256 @defgroup imgproc_transform Image transformations
257 @anchor mymodule_interesting_note
260 Supported Markdown {#tutorial_documentation_quick_start_md}
263 Doxygen supports Markdown formatting with some extensions. Short syntax reference is described
264 below, for details visit [Markdown support].
266 ### lists {#tutorial_documentation_md_list}
280 ### emphasis {#tutorial_documentation_md_emph}
285 use html in complex cases:
286 <em>"path/to/file"</em>
289 ### links {#tutorial_documentation_md_links}
293 [OpenCV main site](http://opencv.org)
300 ### images {#tutorial_documentation_md_image}
303 ![image caption](image path)
306 ### headers {#tutorial_documentation_md_head}
317 ### header id {#tutorial_documentation_md_headid}
319 You can assign a unique identifier to any header to reference it from other places.
321 Header {#some_unique_identifier}
324 See @ref some_unique_identifier for details
327 ### page id {#tutorial_documentation_md_page}
329 Each page should have additional Level1 header at the beginning with page title and identifier:
331 Writing documentation for OpenCV {#tutorial_documentation}
332 ================================
335 ### tables {#tutorial_documentation_md_table}
337 Example from doxygen documentation:
339 First Header | Second Header
340 ------------- | -------------
341 Content Cell | Content Cell
342 Content Cell | Content Cell
345 Commonly used commands {#tutorial_documentation_quick_start_5}
346 ----------------------
348 Most often used doxygen commands are described here with short examples. For the full list of
349 available commands and detailed description, please visit [Command reference].
351 ### Basic commands {#tutorial_documentation_commands_basic}
353 - __brief__ - paragraph with brief entity description
355 - __param__ - description of function argument.
357 Multiple adjacent statements are merged into one list. If argument with this name is not found
358 in actual function signature - doxygen warning will be produced. Function can have either _no_
359 documented parameters, either _all_ should be documented.
361 - __sa__ - "See also" paragraph, contains references to classes, functions, pages or URLs
363 - __note__ - visually highlighted "Note" paragraph. Multiple adjacent statements are merged into
366 - __return, returns__ - describes returned value of a function
368 - __overload__ - adds fixed text to the function description: <em>"This is an overloaded member
369 function, provided for convenience. It differs from the above function only in what argument(s)
372 - __anchor__ - places invisible named anchor, which can be referenced by `ref` command. It can be
375 - __ref__ - explicit reference to a named section, page or anchor.
377 If such entity can not be found - doxygen warning will be generated. This command has an
378 optional argument - link text.
380 Doxygen also generates some links automatically: if text contains word which can be found in
381 documented entities - reference will be generated. This functionality can be disabled by prefixing
382 the word with `%` symbol.
384 Explicit reference: @ref MyClass
385 Explicit named reference: @ref example_page "Example page"
386 Implicit reference: cv::abc::MyClass1 or just MyClass1
387 Disable implicit reference: %MyClass1
392 Inline formulas are bounded with `f$` command:
397 Block formulas - with `f[` and `f]` commands:
402 ### Code inclusion commands {#tutorial_documentation_commands_include}
404 To mark some text as a code in documentation, _code_ and _endcode_ commands are used.
407 float val = img.at<float>(borderInterpolate(100, img.rows, cv::BORDER_REFLECT_101),
408 borderInterpolate(-5, img.cols, cv::BORDER_WRAP));
412 Syntax will be highlighted according to the currently parsed file type (C++ for <em>.hpp</em>, C for <em>.h</em>) or
413 you can manually specify it in curly braces:
419 To include whole example file into documentation, _include_ and _includelineno_ commands are used.
420 The file is searched in common samples locations, so you can specify just its name or short part of
421 the path. The _includelineno_ version also shows line numbers but prevents copy-pasting since
422 the line numbers are included.
425 @include samples/cpp/test.cpp
428 If you want to include some parts of existing example file - use _snippet_ command.
430 First, mark the needed parts of the file with special doxygen comments:
437 Then include this snippet into documentation:
439 @snippet samples/cpp/test.cpp var_init
442 @note Currently most of such partial inclusions are made with _dontinclude_ command for
443 compatibility with the old rST documentation. But newly created samples should be included with the
444 _snippet_ command, since this method is less affected by the changes in processed file.
446 ### Toggle buttons inclusion commands {#tutorial_documentation_toggle_buttons_commands_include}
448 Toggle buttons are used to display the selected configuration (e.g. programming language, OS, IDE).
450 To use the buttons in documentation, _add_toggle_ and _end_toggle_ commands are used.
452 The command _add_toggle_ can be
453 - general: _add_toggle{Button Name}_
454 - for C++: _add_toggle_cpp_
455 - for Java: _add_toggle_java_
456 - for Python: _add_toggle_python_
460 @add_toggle{Button Name}
462 text / code / doxygen commands
467 For example using toggle buttons with text and [code](@ref tutorial_documentation_commands_include) snippets:
476 @snippet samples/cpp/tutorial_code/introduction/documentation/documentation.cpp hello_world
483 @snippet samples/java/tutorial_code/introduction/documentation/Documentation.java hello_world
489 Text for Python button
490 @snippet samples/python/tutorial_code/introduction/documentation/documentation.py hello_world
496 Result looks like this:
503 @snippet samples/cpp/tutorial_code/introduction/documentation/documentation.cpp hello_world
510 @snippet samples/java/tutorial_code/introduction/documentation/Documentation.java hello_world
516 Text for Python button
517 @snippet samples/python/tutorial_code/introduction/documentation/documentation.py hello_world
521 As you can see, the buttons are added automatically under the previous heading.
523 ### Grouping commands {#tutorial_documentation_commands_group}
525 All code entities should be put into named groups representing OpenCV modules and their internal
526 structure, thus each module should be associated with a group with the same name. Good place to
527 define groups and subgroups is the main header file for this module:
528 <em>"<module>/include/opencv2/<module>.hpp"</em>.
530 @note Doxygen groups are called "modules" and are shown on "Modules" page.
534 @defgroup mymodule My great module
537 @defgroup mymodule_basic Basic operations
539 @defgroup mymodule_experimental Experimental operations
545 To put classes and functions into specific group, just add `ingroup` command to its documentation,
546 or wrap the whole code block with `addtogroup` command:
549 /** @brief Example function
554 @addtogroup mymodule_experimental
557 ... several functions, classes or enumerations here
563 ### Publication reference {#tutorial_documentation_commands_cite}
565 Use _cite_ command to insert reference to related publications listed in @ref citelist page.
567 First, add publication BibTeX record into <i>"<opencv>/doc/opencv.bib"</i> or
568 <i>"<opencv_contrib>/modules/<module>/doc/<module>.bib"</i> file:
571 author = {Bradski, Gary R},
572 title = {Computer vision face tracking for use in a perceptual user interface},
574 publisher = {Citeseer}
578 @note Try not to add publication duplicates because it can confuse documentation readers and writers later.
580 Then make reference with _cite_ command:
585 @note To get BibTeX record for the publications one can use [Google Scholar]. Once the publication
586 have been found - follow its "Cite" link and then choose "BibTeX" option:
587 ![](scholarship_cite_dialog.png)
589 Step-by-step {#tutorial_documentation_steps}
592 Steps described in this section can be used as checklist during documentation writing. It is not
593 necessary to do things in the same order, but some steps really depend on previous. And of course
594 these steps are just basic guidelines, there is always a place for creativity.
596 Document the function {#tutorial_documentation_steps_fun}
597 ---------------------
599 1. Add empty doxygen comment preceding function definition.
600 2. Add _brief_ command with short description of function meaning at the beginning.
601 3. Add detailed description of the function.
602 4. _Optional_: insert formulas, images and blocks of example code to illustrate complex cases
603 5. _Optional_: describe each parameter using the _param_ command.
604 6. _Optional_: describe return value of the function using the _returns_ command.
605 7. _Optional_: add "See also" section with links to similar functions or classes
606 8. _Optional_: add bibliographic reference if any.
607 9. Test your code. (Python: "make check_pylint")
608 10. Generate doxygen documentation and verify results.
610 Write the tutorial {#tutorial_documentation_steps_tutorial}
613 1. Formulate the idea to be illustrated in the tutorial.
615 2. Make the example application, simple enough to be understood by a beginning developer. Be
616 laconic and write descriptive comments, don't try to avoid every possible runtime error or to make
617 universal utility. Your goal is to illustrate the idea. And it should fit one source file!
619 If you want to insert code blocks from this file into your tutorial, mark them with special doxygen comments (see [here](@ref tutorial_documentation_commands_include)).
621 If you want to write the tutorial in more than one programming language, use the toggle buttons for alternative comments and code (see [here](@ref tutorial_documentation_toggle_buttons_commands_include)).
623 3. Collect results of the application work. It can be "before/after" images or some numbers
624 representing performance or even a video.
626 Save it in appropriate format for later use in the tutorial:
627 - To save simple graph-like images use lossless ".png" format.
628 - For photo-like images - lossy ".jpg" format.
629 - Numbers will be inserted as plain text, possibly formatted as table.
630 - Video should be uploaded on YouTube.
632 4. Create new tutorial page (<em>".markdown"</em>-file) in corresponding location (see
633 [here](@ref tutorial_documentation_quick_start_1)), and place all image files near it (or in "images"
634 subdirectory). Also put your example application file and make sure it is compiled together with the
635 OpenCV library when `-DBUILD_EXAMPLES=ON` option is enabled on cmake step.
637 5. Modify your new page:
638 - Add page title and identifier, usually prefixed with <em>"tutorial_"</em> (see [here](@ref tutorial_documentation_md_page)).
639 You can add a link to the previous and next tutorial using the identifier
641 @prev_tutorial{identifier}
642 @next_tutorial{identifier}
644 @warning Do **not** write the **hashtag (#)**, example: \n Incorrect: @verbatim @prev_tutorial{#tutorial_documentation} @endverbatim Correct: @verbatim @prev_tutorial{tutorial_documentation} @endverbatim
645 - Add brief description of your idea and tutorial goals.
646 - Describe your program and/or its interesting pieces.
647 - Describe your results, insert previously added images or other results.
649 To add a youtube video, e.g. www.youtube.com/watch?v= **ViPN810E0SU**, use _youtube_{**Video ID**}:
651 @youtube{ViPN810E0SU}
654 - Add bibliographic references if any (see [here](@ref tutorial_documentation_commands_cite)).
656 6. Add newly created tutorial to the corresponding table of contents. Just find
657 <em>"table_of_content_*.markdown"</em> file with the needed table and place new record in it
658 similar to existing ones.
660 - @subpage tutorial_windows_visual_studio_image_watch
662 _Languages:_ C++, Java, Python
664 _Compatibility:_ \>= OpenCV 2.4
666 _Author:_ Wolf Kienzle
668 You will learn how to visualize OpenCV matrices and images within Visual Studio 2012.
670 As you can see it is just a list item with special _subpage_ command which marks your page as a
671 child and places it into the existing pages hierarchy. Add compatibility information,
672 authors list and short description. Also note the list item indent, empty lines between
673 paragraphs and special _italic_ markers.
675 7. Generate doxygen documentation and verify results.
677 References {#tutorial_documentation_refs}
679 - [Doxygen] - main Doxygen page
680 - [Documenting basics] - how to include documentation in code
681 - [Markdown support] - supported syntax and extensions
682 - [Formulas support] - how to include formulas
683 - [Supported formula commands] - HTML formulas use MathJax script for rendering
684 - [Command reference] - supported commands and their parameters
686 <!-- invisible references list -->
687 [Doxygen]: http://www.doxygen.nl
688 [Doxygen download]: http://doxygen.nl/download.html
689 [Doxygen installation]: http://doxygen.nl/manual/install.html
690 [Documenting basics]: http://www.doxygen.nl/manual/docblocks.html
691 [Markdown support]: http://www.doxygen.nl/manual/markdown.html
692 [Formulas support]: http://www.doxygen.nl/manual/formulas.html
693 [Supported formula commands]: http://docs.mathjax.org/en/latest/input/tex/macros/index.html
694 [Command reference]: http://www.doxygen.nl/manual/commands.html
695 [Google Scholar]: http://scholar.google.ru/