GSoC 2016 - Adding ALIASES for tutorial (#7041)
* GSoC 2016 - Adding toggle files to be used by tutorials. Add a toggle option for tutorials. * adds a button on the HTML tutorial pages to switch between blocks * the default option is for languages: one can write a block for C++ and another one for Python without re-writing the tutorial Add aliases to the doxyfile. * adding alises to make a link to previous and next tutorial. * adding alias to specify the toggle options in the tutorials index. * adding alias to add a youtube video directly from link. Add a sample tutorial (mat_mask_opertaions) using the developed aliases: * youtube alias * previous and next tutorial alias * buttons * languages info for tutorial table of content * code referances with snippets (and associated sample code files) * Removing the automatic ordering. Adding specific toggles for cpp, java and python. Move all the code to the footer / header and Doxyfile. Updating documentation.
This commit is contained in:
committed by
Maksim Shabunin
parent
36b5abf6b7
commit
fcddfa4f86
@@ -1,6 +1,9 @@
|
||||
Mask operations on matrices {#tutorial_mat_mask_operations}
|
||||
===========================
|
||||
|
||||
@prev_tutorial{tutorial_how_to_scan_images}
|
||||
@next_tutorial{tutorial_mat_operations}
|
||||
|
||||
Mask operations on matrices are quite simple. The idea is that we recalculate each pixels value in
|
||||
an image according to a mask matrix (also known as kernel). This mask holds values that will adjust
|
||||
how much influence neighboring pixels (and the current pixel) have on the new pixel value. From a
|
||||
@@ -25,113 +28,150 @@ the zero-zero index) on the pixel you want to calculate and sum up the pixel val
|
||||
the overlapped matrix values. It's the same thing, however in case of large matrices the latter
|
||||
notation is a lot easier to look over.
|
||||
|
||||
@add_toggle_cpp
|
||||
Now let us see how we can make this happen by using the basic pixel access method or by using the
|
||||
@ref cv::filter2D function.
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_java
|
||||
Now let us see how we can make this happen by using the basic pixel access method or by using the
|
||||
**Imgproc.filter2D()** function.
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_python
|
||||
Now let us see how we can make this happen by using the basic pixel access method or by using the
|
||||
**cv2.filter2D()** function.
|
||||
@end_toggle
|
||||
|
||||
The Basic Method
|
||||
----------------
|
||||
|
||||
Here's a function that will do this:
|
||||
@code{.cpp}
|
||||
void Sharpen(const Mat& myImage, Mat& Result)
|
||||
{
|
||||
CV_Assert(myImage.depth() == CV_8U); // accept only uchar images
|
||||
@add_toggle_cpp
|
||||
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp basic_method
|
||||
|
||||
Result.create(myImage.size(), myImage.type());
|
||||
const int nChannels = myImage.channels();
|
||||
|
||||
for(int j = 1; j < myImage.rows - 1; ++j)
|
||||
{
|
||||
const uchar* previous = myImage.ptr<uchar>(j - 1);
|
||||
const uchar* current = myImage.ptr<uchar>(j );
|
||||
const uchar* next = myImage.ptr<uchar>(j + 1);
|
||||
|
||||
uchar* output = Result.ptr<uchar>(j);
|
||||
|
||||
for(int i = nChannels; i < nChannels * (myImage.cols - 1); ++i)
|
||||
{
|
||||
*output++ = saturate_cast<uchar>(5 * current[i]
|
||||
-current[i - nChannels] - current[i + nChannels] - previous[i] - next[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Result.row(0).setTo(Scalar(0));
|
||||
Result.row(Result.rows - 1).setTo(Scalar(0));
|
||||
Result.col(0).setTo(Scalar(0));
|
||||
Result.col(Result.cols - 1).setTo(Scalar(0));
|
||||
}
|
||||
@endcode
|
||||
At first we make sure that the input images data is in unsigned char format. For this we use the
|
||||
@ref cv::CV_Assert function that throws an error when the expression inside it is false.
|
||||
@code{.cpp}
|
||||
CV_Assert(myImage.depth() == CV_8U); // accept only uchar images
|
||||
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp 8_bit
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_java
|
||||
@snippet samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java basic_method
|
||||
|
||||
At first we make sure that the input images data in unsigned 8 bit format.
|
||||
@snippet samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java 8_bit
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_python
|
||||
@snippet samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py basic_method
|
||||
|
||||
At first we make sure that the input images data in unsigned 8 bit format.
|
||||
@code{.py}
|
||||
my_image = cv2.cvtColor(my_image, cv2.CV_8U)
|
||||
@endcode
|
||||
|
||||
@end_toggle
|
||||
|
||||
We create an output image with the same size and the same type as our input. As you can see in the
|
||||
@ref tutorial_how_to_scan_images_storing "storing" section, depending on the number of channels we may have one or more
|
||||
subcolumns. We will iterate through them via pointers so the total number of elements depends from
|
||||
subcolumns.
|
||||
|
||||
@add_toggle_cpp
|
||||
We will iterate through them via pointers so the total number of elements depends on
|
||||
this number.
|
||||
@code{.cpp}
|
||||
Result.create(myImage.size(), myImage.type());
|
||||
const int nChannels = myImage.channels();
|
||||
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp create_channels
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_java
|
||||
@snippet samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java create_channels
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_python
|
||||
@code{.py}
|
||||
height, width, n_channels = my_image.shape
|
||||
result = np.zeros(my_image.shape, my_image.dtype)
|
||||
@endcode
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_cpp
|
||||
We'll use the plain C [] operator to access pixels. Because we need to access multiple rows at the
|
||||
same time we'll acquire the pointers for each of them (a previous, a current and a next line). We
|
||||
need another pointer to where we're going to save the calculation. Then simply access the right
|
||||
items with the [] operator. For moving the output pointer ahead we simply increase this (with one
|
||||
byte) after each operation:
|
||||
@code{.cpp}
|
||||
for(int j = 1; j < myImage.rows - 1; ++j)
|
||||
{
|
||||
const uchar* previous = myImage.ptr<uchar>(j - 1);
|
||||
const uchar* current = myImage.ptr<uchar>(j );
|
||||
const uchar* next = myImage.ptr<uchar>(j + 1);
|
||||
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp basic_method_loop
|
||||
|
||||
uchar* output = Result.ptr<uchar>(j);
|
||||
|
||||
for(int i = nChannels; i < nChannels * (myImage.cols - 1); ++i)
|
||||
{
|
||||
*output++ = saturate_cast<uchar>(5 * current[i]
|
||||
-current[i - nChannels] - current[i + nChannels] - previous[i] - next[i]);
|
||||
}
|
||||
}
|
||||
@endcode
|
||||
On the borders of the image the upper notation results inexistent pixel locations (like minus one -
|
||||
minus one). In these points our formula is undefined. A simple solution is to not apply the kernel
|
||||
in these points and, for example, set the pixels on the borders to zeros:
|
||||
@code{.cpp}
|
||||
Result.row(0).setTo(Scalar(0)); // The top row
|
||||
Result.row(Result.rows - 1).setTo(Scalar(0)); // The bottom row
|
||||
Result.col(0).setTo(Scalar(0)); // The left column
|
||||
Result.col(Result.cols - 1).setTo(Scalar(0)); // The right column
|
||||
@endcode
|
||||
|
||||
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp borders
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_java
|
||||
We need to access multiple rows and columns which can be done by adding or subtracting 1 to the current center (i,j).
|
||||
Then we apply the sum and put the new value in the Result matrix.
|
||||
@snippet samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java basic_method_loop
|
||||
|
||||
On the borders of the image the upper notation results in inexistent pixel locations (like (-1,-1)).
|
||||
In these points our formula is undefined. A simple solution is to not apply the kernel
|
||||
in these points and, for example, set the pixels on the borders to zeros:
|
||||
|
||||
@snippet samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java borders
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_python
|
||||
We need to access multiple rows and columns which can be done by adding or subtracting 1 to the current center (i,j).
|
||||
Then we apply the sum and put the new value in the Result matrix.
|
||||
@snippet samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py basic_method_loop
|
||||
@end_toggle
|
||||
|
||||
The filter2D function
|
||||
---------------------
|
||||
|
||||
Applying such filters are so common in image processing that in OpenCV there exist a function that
|
||||
will take care of applying the mask (also called a kernel in some places). For this you first need
|
||||
to define a *Mat* object that holds the mask:
|
||||
@code{.cpp}
|
||||
Mat kern = (Mat_<char>(3,3) << 0, -1, 0,
|
||||
-1, 5, -1,
|
||||
0, -1, 0);
|
||||
@endcode
|
||||
Then call the @ref cv::filter2D function specifying the input, the output image and the kernell to
|
||||
to define an object that holds the mask:
|
||||
@add_toggle_cpp
|
||||
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp kern
|
||||
|
||||
Then call the @ref cv::filter2D function specifying the input, the output image and the kernel to
|
||||
use:
|
||||
@code{.cpp}
|
||||
filter2D(I, K, I.depth(), kern);
|
||||
@endcode
|
||||
The function even has a fifth optional argument to specify the center of the kernel, and a sixth one
|
||||
for determining what to do in the regions where the operation is undefined (borders). Using this
|
||||
function has the advantage that it's shorter, less verbose and because there are some optimization
|
||||
techniques implemented it is usually faster than the *hand-coded method*. For example in my test
|
||||
while the second one took only 13 milliseconds the first took around 31 milliseconds. Quite some
|
||||
difference.
|
||||
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp filter2D
|
||||
|
||||
The function even has a fifth optional argument to specify the center of the kernel, a sixth
|
||||
for adding an optional value to the filtered pixels before storing them in K and a seventh one
|
||||
for determining what to do in the regions where the operation is undefined (borders).
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_java
|
||||
@snippet samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java kern
|
||||
|
||||
Then call the **Imgproc.filter2D()** function specifying the input, the output image and the kernel to
|
||||
use:
|
||||
@snippet samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java filter2D
|
||||
The function even has a fifth optional argument to specify the center of the kernel, a sixth
|
||||
for adding an optional value to the filtered pixels before storing them in K and a seventh one
|
||||
for determining what to do in the regions where the operation is undefined (borders).
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_python
|
||||
@snippet samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py kern
|
||||
|
||||
Then call the **cv2.filter2D()** function specifying the input, the output image and the kernell to
|
||||
use:
|
||||
@snippet samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py filter2D
|
||||
@end_toggle
|
||||
|
||||
This function is shorter, less verbose and, because there are some optimizations, it is usually faster
|
||||
than the *hand-coded method*. For example in my test while the second one took only 13
|
||||
milliseconds the first took around 31 milliseconds. Quite some difference.
|
||||
|
||||
For example:
|
||||
|
||||

|
||||
|
||||
@add_toggle_cpp
|
||||
You can download this source code from [here
|
||||
](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp) or look in the
|
||||
OpenCV source code libraries sample directory at
|
||||
@@ -139,9 +179,15 @@ OpenCV source code libraries sample directory at
|
||||
|
||||
Check out an instance of running the program on our [YouTube
|
||||
channel](http://www.youtube.com/watch?v=7PF1tAU9se4) .
|
||||
@youtube{7PF1tAU9se4}
|
||||
@end_toggle
|
||||
|
||||
\htmlonly
|
||||
<div align="center">
|
||||
<iframe width="560" height="349" src="https://www.youtube.com/embed/7PF1tAU9se4?hd=1" frameborder="0" allowfullscreen></iframe>
|
||||
</div>
|
||||
\endhtmlonly
|
||||
@add_toggle_java
|
||||
You can look in the OpenCV source code libraries sample directory at
|
||||
`samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java`.
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_python
|
||||
You can look in the OpenCV source code libraries sample directory at
|
||||
`samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py`.
|
||||
@end_toggle
|
||||
|
||||
@@ -25,6 +25,8 @@ understanding how to manipulate the images on a pixel level.
|
||||
|
||||
- @subpage tutorial_mat_mask_operations
|
||||
|
||||
*Languages:* C++, Java, Python
|
||||
|
||||
*Compatibility:* \> OpenCV 2.0
|
||||
|
||||
*Author:* Bernát Gábor
|
||||
|
||||
@@ -439,6 +439,83 @@ Then include this snippet into documentation:
|
||||
compatibility with the old rST documentation. But newly created samples should be included with the
|
||||
_snippet_ command, since this method is less affected by the changes in processed file.
|
||||
|
||||
### Toggle buttons inclusion commands {#tutorial_documentation_toggle_buttons_commands_include}
|
||||
|
||||
Toggle buttons are used to display the selected configuration (e.g. programming language, OS, IDE).
|
||||
|
||||
To use the buttons in documentation, _add_toggle_ and _end_toggle_ commands are used.
|
||||
|
||||
The command _add_toggle_ can be
|
||||
- general: _add_toggle{Button Name}_
|
||||
- for C++: _add_toggle_cpp_
|
||||
- for Java: _add_toggle_java_
|
||||
- for Python: _add_toggle_python_
|
||||
|
||||
Example:
|
||||
@verbatim
|
||||
@add_toggle{Button Name}
|
||||
|
||||
text / code / doxygen commands
|
||||
|
||||
@end_toggle
|
||||
@endverbatim
|
||||
|
||||
For example using toggle buttons with text and [code](@ref tutorial_documentation_commands_include) snippets:
|
||||
|
||||
@verbatim
|
||||
|
||||
### Buttons Example
|
||||
|
||||
@add_toggle_cpp
|
||||
|
||||
Text for C++ button
|
||||
@snippet samples/cpp/tutorial_code/introduction/documentation/documentation.cpp hello_world
|
||||
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_java
|
||||
|
||||
Text for Java button
|
||||
@snippet samples/java/tutorial_code/introduction/documentation/Documentation.java hello_world
|
||||
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_python
|
||||
|
||||
Text for Python button
|
||||
@snippet samples/python/tutorial_code/introduction/documentation/documentation.py hello_world
|
||||
|
||||
@end_toggle
|
||||
|
||||
@endverbatim
|
||||
|
||||
Result looks like this:
|
||||
|
||||
### Buttons Example
|
||||
|
||||
@add_toggle_cpp
|
||||
|
||||
Text for C++ button
|
||||
@snippet samples/cpp/tutorial_code/introduction/documentation/documentation.cpp hello_world
|
||||
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_java
|
||||
|
||||
Text for Java button
|
||||
@snippet samples/java/tutorial_code/introduction/documentation/Documentation.java hello_world
|
||||
|
||||
@end_toggle
|
||||
|
||||
@add_toggle_python
|
||||
|
||||
Text for Python button
|
||||
@snippet samples/python/tutorial_code/introduction/documentation/documentation.py hello_world
|
||||
|
||||
@end_toggle
|
||||
|
||||
As you can see, the buttons are added automatically under the previous heading.
|
||||
|
||||
### Grouping commands {#tutorial_documentation_commands_group}
|
||||
|
||||
All code entities should be put into named groups representing OpenCV modules and their internal
|
||||
@@ -536,6 +613,8 @@ Write the tutorial {#tutorial_documentation_steps_tutorial}
|
||||
|
||||
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)).
|
||||
|
||||
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)).
|
||||
|
||||
3. Collect results of the application work. It can be "before/after" images or some numbers
|
||||
representing performance or even a video.
|
||||
|
||||
@@ -552,22 +631,21 @@ Write the tutorial {#tutorial_documentation_steps_tutorial}
|
||||
|
||||
5. Modify your new page:
|
||||
- Add page title and identifier, usually prefixed with <em>"tutorial_"</em> (see [here](@ref tutorial_documentation_md_page)).
|
||||
You can add a link to the previous and next tutorial using the identifier
|
||||
@verbatim
|
||||
@prev_tutorial{identifier}
|
||||
@next_tutorial{identifier}
|
||||
@endverbatim
|
||||
@warning Do **not** write the **hashtag (#)**, example: \n Incorrect: @verbatim @prev_tutorial{#tutorial_documentation} @endverbatim Correct: @verbatim @prev_tutorial{tutorial_documentation} @endverbatim
|
||||
- Add brief description of your idea and tutorial goals.
|
||||
- Describe your program and/or its interesting pieces.
|
||||
- Describe your results, insert previously added images or other results.
|
||||
|
||||
To add a video use _htmlonly_, _endhtmlonly_ commands with raw html block inside:
|
||||
To add a youtube video, e.g. www.youtube.com/watch?v= **ViPN810E0SU**, use _youtube_{**Video ID**}:
|
||||
@verbatim
|
||||
@htmlonly
|
||||
<div align="center">
|
||||
<iframe
|
||||
title="my title" width="560" height="349"
|
||||
src="http://www.youtube.com/embed/ViPN810E0SU?rel=0&loop=1"
|
||||
frameborder="0" allowfullscreen align="middle">
|
||||
</iframe>
|
||||
</div>
|
||||
@endhtmlonly
|
||||
@youtube{ViPN810E0SU}
|
||||
@endverbatim
|
||||
|
||||
- Add bibliographic references if any (see [here](@ref tutorial_documentation_commands_cite)).
|
||||
|
||||
6. Add newly created tutorial to the corresponding table of contents. Just find
|
||||
@@ -576,6 +654,8 @@ Write the tutorial {#tutorial_documentation_steps_tutorial}
|
||||
@verbatim
|
||||
- @subpage tutorial_windows_visual_studio_image_watch
|
||||
|
||||
_Languages:_ C++, Java, Python
|
||||
|
||||
_Compatibility:_ \>= OpenCV 2.4
|
||||
|
||||
_Author:_ Wolf Kienzle
|
||||
|
||||
Reference in New Issue
Block a user