058f392afe4fd177c9514eb786804c5b50fd62d2
[platform/upstream/opencv.git] / doc / tutorials / introduction / documenting_opencv / documentation_tutorial.markdown
1 Writing documentation for OpenCV {#tutorial_documentation}
2 ================================
3
4 @tableofcontents
5
6 Doxygen overview {#tutorial_documentation_overview}
7 ================
8
9 Intro {#tutorial_documentation_intro}
10 -----
11
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
18
19 OpenCV library existing documentation has been converted to doxygen format.
20
21 Installation {#tutorial_documentation_install}
22 ------------
23
24 Please, check official [download][Doxygen download] and [installation][Doxygen installation] pages.
25 Some linux distributions can also provide doxygen packages.
26
27 Generate documentation {#tutorial_documentation_generate}
28 ----------------------
29
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):
34     @code{.sh}
35     cmake -DBUILD_DOCS=ON ../opencv
36     @endcode
37     Or if you get contrib sources too:
38     @code{.sh}
39     cmake -DBUILD_DOCS=ON -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv
40     @endcode
41 -   Run make:
42     @code{.sh}
43     make doxygen
44     @endcode
45 -   Open <i>doc/doxygen/html/index.html</i> file in your favorite browser
46 -   Test your Python code:
47     @code{.sh}
48     make check_pylint
49     @endcode
50
51 Quick start {#tutorial_documentation_quick_start}
52 ===========
53
54 @note These instructions are specific to OpenCV library documentation, other projects can use
55 different layout scheme and documenting agreements.
56
57 Documentation locations {#tutorial_documentation_quick_start_1}
58 -----------------------
59
60 Whole documentation is gathered from many different places:
61
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.
64
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.
68
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.
71
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.
75
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.
78
79 Following scheme represents common documentation places for _opencv_ repository:
80 ~~~
81 <opencv>
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)
85 ├── modules
86 │   └── <modulename>
87 │       ├── doc         - documentation pages and images for module
88 │       └── include     - code documentation in header files
89 └── samples         - place for all code examples
90     ├── cpp
91     │   └── tutorial_code   - place for tutorial code examples
92     └── ...
93 ~~~
94
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.
99
100 @note You can put C++ template implementation and specialization to separate files
101 (<i>".impl.hpp"</i>) ignored by doxygen.
102
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>).
107
108 Since version 3.0 all new modules are placed into _opencv_contrib_ repository, it has slightly
109 different layout:
110 ~~~
111 <opencv_contrib>
112 └── modules
113     └── <modulename>
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
118 ~~~
119
120 Example {#tutorial_documentation_quick_start_2}
121 -------
122
123 To add documentation for functions, classes and other entities, just insert special comment prior
124 its definition. Like this:
125
126 @verbatim
127 /** @brief Calculates the exponent of every array element.
128
129 The function exp calculates the exponent of every element of the input array:
130 \f[ \texttt{dst} [I] = e^{ src(I) } \f]
131
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.
135
136 @param src input array.
137 @param dst output array of the same size and type as src.
138
139 @sa log , cartToPolar , polarToCart , phase , pow , sqrt , magnitude
140 */
141 CV_EXPORTS_W void exp(InputArray src, OutputArray dst);
142 @endverbatim
143
144 Here you can see:
145
146 -   special C-comment syntax denotes it is doxygen comment
147     @verbatim /** ... */ @endverbatim
148
149 -   command `brief` denotes following paragraph is a brief description
150     @verbatim @brief @endverbatim
151
152 -   empty line denotes paragraph end
153
154 -   TeX formula between `f[` and `f]` commands
155     @verbatim \f[ ... \f] @endverbatim
156
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
160
161 -   command `sa` starts "See also" paragraph containing references to some classes, methods, pages or URLs.
162     @verbatim @sa @endverbatim
163
164 Produced reference item looks like this:
165 ![Reference link](doxygen-2.png)
166
167 The "More..." link brings you to the function documentation:
168 ![Function documentation](doxygen-1.png)
169
170
171 Another example {#tutorial_documentation_quick_start_3}
172 ---------------
173
174 Different comment syntax can be used for one-line short comments:
175
176 @verbatim
177 //! type of line
178 enum LineTypes {
179     FILLED  = -1,
180     LINE_4  = 4, //!< 4-connected line
181     LINE_8  = 8, //!< 8-connected line
182     LINE_AA = 16 //!< antialiased line
183 };
184 @endverbatim
185
186 Here:
187
188 -   special C++-comment syntax denotes it is doxygen comment
189     @verbatim //! @endverbatim
190
191 -   additional symbol `<` denotes this comment is located _after_ documented entity
192     @verbatim //!< @endverbatim
193
194 Produced documentation block looks like this:
195 ![Enumeration documentation](doxygen-3.png)
196
197 More details {#tutorial_documentation_quick_start_4}
198 ------------
199
200 ### Command prefix
201
202 Doxygen commands starts with `@` or `\` sign:
203 @verbatim
204 @brief ...
205 or
206 \brief ...
207 @endverbatim
208
209 ### Comment syntax
210
211 Doxygen comment can have different forms:
212 @verbatim
213 C-style:
214 /** ... */
215 or
216 /*! ... */
217
218 C++-style
219 //! ...
220 or
221 /// ...
222
223 Lines can start with '*':
224 /**
225  * ...
226  * ...
227  */
228
229 Can be placed after documented entity:
230 //!< ...
231 /**< ... */
232 @endverbatim
233
234 ### Paragraph end
235
236 To end paragraph, insert empty line or any command starting new paragraph:
237 @verbatim
238 @brief brief description paragraph
239 brief continues
240
241 new paragraph
242
243 @note new note paragraph
244 note paragraph continues
245
246 another paragraph
247 paragraph continues
248 @endverbatim
249
250 ### Naming
251
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:
254 @verbatim
255 @page core_explanation_1 Usage explanation
256 @defgroup imgproc_transform Image transformations
257 @anchor mymodule_interesting_note
258 @endverbatim
259
260 Supported Markdown {#tutorial_documentation_quick_start_md}
261 ------------------
262
263 Doxygen supports Markdown formatting with some extensions. Short syntax reference is described
264 below, for details visit [Markdown support].
265
266 ### lists {#tutorial_documentation_md_list}
267
268 @verbatim
269 Bulleted:
270 - item1
271 - item2
272 Numbered:
273 1. item1
274 2. item2
275 or
276 -# item1
277 -# item2
278 @endverbatim
279
280 ### emphasis {#tutorial_documentation_md_emph}
281
282 @verbatim
283 _italic_
284 __bold__
285 use html in complex cases:
286 <em>"path/to/file"</em>
287 @endverbatim
288
289 ### links {#tutorial_documentation_md_links}
290
291 @verbatim
292 explicit link:
293 [OpenCV main site](http://opencv.org)
294 automatic links:
295 <http://opencv.org>
296 or even:
297 http://opencv.org
298 @endverbatim
299
300 ### images {#tutorial_documentation_md_image}
301
302 @verbatim
303 ![image caption](image path)
304 @endverbatim
305
306 ### headers {#tutorial_documentation_md_head}
307
308 @verbatim
309 Level1
310 ======
311 Level2
312 ------
313 ### Level3
314 #### Level4
315 @endverbatim
316
317 ### header id {#tutorial_documentation_md_headid}
318
319 You can assign a unique identifier to any header to reference it from other places.
320 @verbatim
321 Header {#some_unique_identifier}
322 ------
323 ...
324 See @ref some_unique_identifier for details
325 @endverbatim
326
327 ### page id {#tutorial_documentation_md_page}
328
329 Each page should have additional Level1 header at the beginning with page title and identifier:
330 @verbatim
331 Writing documentation for OpenCV {#tutorial_documentation}
332 ================================
333 @endverbatim
334
335 ### tables {#tutorial_documentation_md_table}
336
337 Example from doxygen documentation:
338 @verbatim
339 First Header  | Second Header
340 ------------- | -------------
341 Content Cell  | Content Cell
342 Content Cell  | Content Cell
343 @endverbatim
344
345 Commonly used commands {#tutorial_documentation_quick_start_5}
346 ----------------------
347
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].
350
351 ### Basic commands {#tutorial_documentation_commands_basic}
352
353 -   __brief__ - paragraph with brief entity description
354
355 -   __param__ - description of function argument.
356
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.
360
361 -   __sa__ - "See also" paragraph, contains references to classes, functions, pages or URLs
362
363 -   __note__ - visually highlighted "Note" paragraph. Multiple adjacent statements are merged into
364     one block.
365
366 -   __return, returns__ - describes returned value of a function
367
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)
370     it accepts."</em>
371
372 -   __anchor__ - places invisible named anchor, which can be referenced by `ref` command. It can be
373     used in pages only.
374
375 -   __ref__ - explicit reference to a named section, page or anchor.
376
377     If such entity can not be found - doxygen warning will be generated. This command has an
378     optional argument - link text.
379
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.
383     @verbatim
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
388     @endverbatim
389
390 -   __f__ - formula
391
392     Inline formulas are bounded with `f$` command:
393     @verbatim
394 \f$ ... \f$
395     @endverbatim
396
397     Block formulas - with `f[` and `f]` commands:
398     @verbatim
399 \f[ ... \f]
400     @endverbatim
401
402 ### Code inclusion commands {#tutorial_documentation_commands_include}
403
404 To mark some text as a code in documentation, _code_ and _endcode_ commands are used.
405 @verbatim
406 @code
407 float val = img.at<float>(borderInterpolate(100, img.rows, cv::BORDER_REFLECT_101),
408                           borderInterpolate(-5, img.cols, cv::BORDER_WRAP));
409 @endcode
410 @endverbatim
411
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:
414
415 @verbatim
416 @code{.xml}
417 @endverbatim
418
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.
423
424 @verbatim
425 @include samples/cpp/test.cpp
426 @endverbatim
427
428 If you want to include some parts of existing example file - use _snippet_ command.
429
430 First, mark the needed parts of the file with special doxygen comments:
431 @verbatim
432 //! [var_init]
433 int a = 0;
434 //! [var_init]
435 @endverbatim
436
437 Then include this snippet into documentation:
438 @verbatim
439 @snippet samples/cpp/test.cpp var_init
440 @endverbatim
441
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.
445
446 ### Toggle buttons inclusion commands {#tutorial_documentation_toggle_buttons_commands_include}
447
448 Toggle buttons are used to display the selected configuration (e.g. programming language, OS, IDE).
449
450 To use the buttons in documentation, _add_toggle_ and _end_toggle_ commands are used.
451
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_
457
458 Example:
459 @verbatim
460 @add_toggle{Button Name}
461
462   text / code / doxygen commands
463
464 @end_toggle
465 @endverbatim
466
467 For example using toggle buttons with text and [code](@ref tutorial_documentation_commands_include) snippets:
468
469 @verbatim
470
471 ### Buttons Example
472
473 @add_toggle_cpp
474
475    Text for C++ button
476    @snippet samples/cpp/tutorial_code/introduction/documentation/documentation.cpp hello_world
477
478 @end_toggle
479
480 @add_toggle_java
481
482    Text for Java button
483    @snippet samples/java/tutorial_code/introduction/documentation/Documentation.java  hello_world
484
485 @end_toggle
486
487 @add_toggle_python
488
489    Text for Python button
490    @snippet samples/python/tutorial_code/introduction/documentation/documentation.py hello_world
491
492 @end_toggle
493
494 @endverbatim
495
496 Result looks like this:
497
498 ### Buttons Example
499
500 @add_toggle_cpp
501
502    Text for C++ button
503    @snippet samples/cpp/tutorial_code/introduction/documentation/documentation.cpp hello_world
504
505 @end_toggle
506
507 @add_toggle_java
508
509    Text for Java button
510    @snippet samples/java/tutorial_code/introduction/documentation/Documentation.java  hello_world
511
512 @end_toggle
513
514 @add_toggle_python
515
516    Text for Python button
517    @snippet samples/python/tutorial_code/introduction/documentation/documentation.py hello_world
518
519 @end_toggle
520
521 As you can see, the buttons are added automatically under the previous heading.
522
523 ### Grouping commands {#tutorial_documentation_commands_group}
524
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>.
529
530 @note Doxygen groups are called "modules" and are shown on "Modules" page.
531
532 @verbatim
533 /**
534 @defgroup mymodule My great module
535     optional description
536 @{
537     @defgroup mymodule_basic Basic operations
538         optional description
539     @defgroup mymodule_experimental Experimental operations
540         optional description
541 @}
542 */
543 @endverbatim
544
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:
547
548 @verbatim
549 /** @brief Example function
550     @ingroup mymodule
551 */
552 or
553 /**
554 @addtogroup mymodule_experimental
555 @{
556 */
557 ... several functions, classes or enumerations here
558 /**
559 @}
560 */
561 @endverbatim
562
563 ### Publication reference {#tutorial_documentation_commands_cite}
564
565 Use _cite_ command to insert reference to related publications listed in @ref citelist page.
566
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:
569 @verbatim
570 @ARTICLE{Bradski98,
571     author = {Bradski, Gary R},
572     title = {Computer vision face tracking for use in a perceptual user interface},
573     year = {1998},
574     publisher = {Citeseer}
575 }
576 @endverbatim
577
578 @note Try not to add publication duplicates because it can confuse documentation readers and writers later.
579
580 Then make reference with _cite_ command:
581 @verbatim
582 @cite Bradski98
583 @endverbatim
584
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)
588
589 Step-by-step {#tutorial_documentation_steps}
590 ============
591
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.
595
596 Document the function {#tutorial_documentation_steps_fun}
597 ---------------------
598
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.
609
610 Write the tutorial {#tutorial_documentation_steps_tutorial}
611 ------------------
612
613 1.  Formulate the idea to be illustrated in the tutorial.
614
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!
618
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)).
620
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)).
622
623 3.  Collect results  of the application work. It can be "before/after" images or some numbers
624     representing performance or even a video.
625
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.
631
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.
636
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
640         @verbatim
641 @prev_tutorial{identifier}
642 @next_tutorial{identifier}
643         @endverbatim
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.
648
649         To add a youtube video, e.g. www.youtube.com/watch?v= **ViPN810E0SU**, use _youtube_{**Video ID**}:
650         @verbatim
651 @youtube{ViPN810E0SU}
652         @endverbatim
653
654     -   Add bibliographic references if any (see [here](@ref tutorial_documentation_commands_cite)).
655
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.
659     @verbatim
660 -   @subpage tutorial_windows_visual_studio_image_watch
661
662     _Languages:_ C++, Java, Python
663
664     _Compatibility:_ \>= OpenCV 2.4
665
666     _Author:_ Wolf Kienzle
667
668     You will learn how to visualize OpenCV matrices and images within Visual Studio 2012.
669     @endverbatim
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.
674
675 7.  Generate doxygen documentation and verify results.
676
677 References {#tutorial_documentation_refs}
678 ==========
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
685
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/