Skip to content

Commit 9ff33da

Browse files
committed
Tutorial Make Border
1 parent a6f5e1f commit 9ff33da

File tree

5 files changed

+373
-83
lines changed

5 files changed

+373
-83
lines changed

doc/tutorials/imgproc/imgtrans/copyMakeBorder/copyMakeBorder.markdown

Lines changed: 159 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
Adding borders to your images {#tutorial_copyMakeBorder}
22
=============================
33

4+
@prev_tutorial{tutorial_filter_2d}
5+
@next_tutorial{tutorial_sobel_derivatives}
6+
47
Goal
58
----
69

710
In this tutorial you will learn how to:
811

9-
- Use the OpenCV function @ref cv::copyMakeBorder to set the borders (extra padding to your
12+
- Use the OpenCV function **copyMakeBorder()** to set the borders (extra padding to your
1013
image).
1114

1215
Theory
@@ -30,10 +33,7 @@ Theory
3033

3134
This will be seen more clearly in the Code section.
3235

33-
Code
34-
----
35-
36-
-# **What does this program do?**
36+
- **What does this program do?**
3737
- Load an image
3838
- Let the user choose what kind of padding use in the input image. There are two options:
3939

@@ -45,38 +45,153 @@ Code
4545
The user chooses either option by pressing 'c' (constant) or 'r' (replicate)
4646
- The program finishes when the user presses 'ESC'
4747

48-
-# The tutorial code's is shown lines below. You can also download it from
49-
[here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp)
50-
@include samples/cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp
48+
Code
49+
----
50+
51+
The tutorial code's is shown lines below.
52+
53+
@add_toggle_cpp
54+
You can also download it from
55+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp)
56+
@include samples/cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp
57+
@end_toggle
58+
59+
@add_toggle_java
60+
You can also download it from
61+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgTrans/MakeBorder/CopyMakeBorder.java)
62+
@include samples/java/tutorial_code/ImgTrans/MakeBorder/CopyMakeBorder.java
63+
@end_toggle
64+
65+
@add_toggle_python
66+
You can also download it from
67+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py)
68+
@include samples/python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py
69+
@end_toggle
5170

5271
Explanation
5372
-----------
5473

55-
-# First we declare the variables we are going to use:
56-
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp variables
74+
#### Declare the variables
75+
76+
First we declare the variables we are going to use:
77+
78+
@add_toggle_cpp
79+
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp variables
80+
@end_toggle
81+
82+
@add_toggle_java
83+
@snippet java/tutorial_code/ImgTrans/MakeBorder/CopyMakeBorder.java variables
84+
@end_toggle
85+
86+
@add_toggle_python
87+
@snippet python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py variables
88+
@end_toggle
89+
90+
Especial attention deserves the variable *rng* which is a random number generator. We use it to
91+
generate the random border color, as we will see soon.
92+
93+
#### Load an image
94+
95+
As usual we load our source image *src*:
96+
97+
@add_toggle_cpp
98+
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp load
99+
@end_toggle
100+
101+
@add_toggle_java
102+
@snippet java/tutorial_code/ImgTrans/MakeBorder/CopyMakeBorder.java load
103+
@end_toggle
104+
105+
@add_toggle_python
106+
@snippet python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py load
107+
@end_toggle
108+
109+
#### Create a window
110+
111+
After giving a short intro of how to use the program, we create a window:
112+
113+
@add_toggle_cpp
114+
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp create_window
115+
@end_toggle
116+
117+
@add_toggle_java
118+
@snippet java/tutorial_code/ImgTrans/MakeBorder/CopyMakeBorder.java create_window
119+
@end_toggle
57120

58-
Especial attention deserves the variable *rng* which is a random number generator. We use it to
59-
generate the random border color, as we will see soon.
121+
@add_toggle_python
122+
@snippet python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py create_window
123+
@end_toggle
60124

61-
-# As usual we load our source image *src*:
62-
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp load
125+
#### Initialize arguments
63126

64-
-# After giving a short intro of how to use the program, we create a window:
65-
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp create_window
66-
-# Now we initialize the argument that defines the size of the borders (*top*, *bottom*, *left* and
67-
*right*). We give them a value of 5% the size of *src*.
68-
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp init_arguments
69-
-# The program runs in a **for** loop. If the user presses 'c' or 'r', the *borderType* variable
70-
takes the value of *BORDER_CONSTANT* or *BORDER_REPLICATE* respectively:
71-
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp check_keypress
72-
-# In each iteration (after 0.5 seconds), the variable *value* is updated...
73-
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp update_value
74-
with a random value generated by the **RNG** variable *rng*. This value is a number picked
75-
randomly in the range \f$[0,255]\f$
127+
Now we initialize the argument that defines the size of the borders (*top*, *bottom*, *left* and
128+
*right*). We give them a value of 5% the size of *src*.
76129

77-
-# Finally, we call the function @ref cv::copyMakeBorder to apply the respective padding:
78-
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp copymakeborder
79-
The arguments are:
130+
@add_toggle_cpp
131+
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp init_arguments
132+
@end_toggle
133+
134+
@add_toggle_java
135+
@snippet java/tutorial_code/ImgTrans/MakeBorder/CopyMakeBorder.java init_arguments
136+
@end_toggle
137+
138+
@add_toggle_python
139+
@snippet python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py init_arguments
140+
@end_toggle
141+
142+
#### Loop
143+
144+
The program runs in an infinite loop while the key **ESC** isn't pressed.
145+
If the user presses '**c**' or '**r**', the *borderType* variable
146+
takes the value of *BORDER_CONSTANT* or *BORDER_REPLICATE* respectively:
147+
148+
@add_toggle_cpp
149+
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp check_keypress
150+
@end_toggle
151+
152+
@add_toggle_java
153+
@snippet java/tutorial_code/ImgTrans/MakeBorder/CopyMakeBorder.java check_keypress
154+
@end_toggle
155+
156+
@add_toggle_python
157+
@snippet python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py check_keypress
158+
@end_toggle
159+
160+
#### Random color
161+
162+
In each iteration (after 0.5 seconds), the random border color (*value*) is updated...
163+
164+
@add_toggle_cpp
165+
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp update_value
166+
@end_toggle
167+
168+
@add_toggle_java
169+
@snippet java/tutorial_code/ImgTrans/MakeBorder/CopyMakeBorder.java update_value
170+
@end_toggle
171+
172+
@add_toggle_python
173+
@snippet python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py update_value
174+
@end_toggle
175+
176+
This value is a set of three numbers picked randomly in the range \f$[0,255]\f$.
177+
178+
#### Form a border around the image
179+
180+
Finally, we call the function **copyMakeBorder()** to apply the respective padding:
181+
182+
@add_toggle_cpp
183+
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp copymakeborder
184+
@end_toggle
185+
186+
@add_toggle_java
187+
@snippet java/tutorial_code/ImgTrans/MakeBorder/CopyMakeBorder.java copymakeborder
188+
@end_toggle
189+
190+
@add_toggle_python
191+
@snippet python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py copymakeborder
192+
@end_toggle
193+
194+
- The arguments are:
80195

81196
-# *src*: Source image
82197
-# *dst*: Destination image
@@ -87,8 +202,21 @@ Explanation
87202
-# *value*: If *borderType* is *BORDER_CONSTANT*, this is the value used to fill the border
88203
pixels.
89204

90-
-# We display our output image in the image created previously
91-
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp display
205+
#### Display the results
206+
207+
We display our output image in the image created previously
208+
209+
@add_toggle_cpp
210+
@snippet cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp display
211+
@end_toggle
212+
213+
@add_toggle_java
214+
@snippet java/tutorial_code/ImgTrans/MakeBorder/CopyMakeBorder.java display
215+
@end_toggle
216+
217+
@add_toggle_python
218+
@snippet python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py display
219+
@end_toggle
92220

93221
Results
94222
-------

doc/tutorials/imgproc/table_of_content_imgproc.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ In this section you will learn about the image processing (manipulation) functio
8787

8888
- @subpage tutorial_copyMakeBorder
8989

90+
*Languages:* C++, Java, Python
91+
9092
*Compatibility:* \> OpenCV 2.0
9193

9294
*Author:* Ana Huamán

samples/cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
using namespace cv;
1212

1313
//![variables]
14+
// Declare the variables
1415
Mat src, dst;
1516
int top, bottom, left, right;
16-
int borderType;
17+
int borderType = BORDER_CONSTANT;
1718
const char* window_name = "copyMakeBorder Demo";
1819
RNG rng(12345);
1920
//![variables]
@@ -23,65 +24,61 @@ RNG rng(12345);
2324
*/
2425
int main( int argc, char** argv )
2526
{
26-
//![load]
27-
String imageName("../data/lena.jpg"); // by default
28-
if (argc > 1)
29-
{
30-
imageName = argv[1];
31-
}
32-
src = imread( imageName, IMREAD_COLOR ); // Load an image
27+
//![load]
28+
const char* imageName = argc >=2 ? argv[1] : "../data/lena.jpg";
3329

34-
if( src.empty() )
35-
{
36-
printf(" No data entered, please enter the path to an image file \n");
37-
return -1;
38-
}
39-
//![load]
30+
// Loads an image
31+
src = imread( imageName, IMREAD_COLOR ); // Load an image
4032

41-
/// Brief how-to for this program
42-
printf( "\n \t copyMakeBorder Demo: \n" );
43-
printf( "\t -------------------- \n" );
44-
printf( " ** Press 'c' to set the border to a random constant value \n");
45-
printf( " ** Press 'r' to set the border to be replicated \n");
46-
printf( " ** Press 'ESC' to exit the program \n");
33+
// Check if image is loaded fine
34+
if( src.empty()) {
35+
printf(" Error opening image\n");
36+
printf(" Program Arguments: [image_name -- default ../data/lena.jpg] \n");
37+
return -1;
38+
}
39+
//![load]
4740

48-
//![create_window]
49-
namedWindow( window_name, WINDOW_AUTOSIZE );
50-
//![create_window]
41+
// Brief how-to for this program
42+
printf( "\n \t copyMakeBorder Demo: \n" );
43+
printf( "\t -------------------- \n" );
44+
printf( " ** Press 'c' to set the border to a random constant value \n");
45+
printf( " ** Press 'r' to set the border to be replicated \n");
46+
printf( " ** Press 'ESC' to exit the program \n");
5147

52-
//![init_arguments]
53-
/// Initialize arguments for the filter
54-
top = (int) (0.05*src.rows); bottom = (int) (0.05*src.rows);
55-
left = (int) (0.05*src.cols); right = (int) (0.05*src.cols);
56-
//![init_arguments]
48+
//![create_window]
49+
namedWindow( window_name, WINDOW_AUTOSIZE );
50+
//![create_window]
5751

58-
dst = src;
59-
imshow( window_name, dst );
52+
//![init_arguments]
53+
// Initialize arguments for the filter
54+
top = (int) (0.05*src.rows); bottom = top;
55+
left = (int) (0.05*src.cols); right = left;
56+
//![init_arguments]
6057

61-
for(;;)
62-
{
63-
//![check_keypress]
64-
char c = (char)waitKey(500);
65-
if( c == 27 )
66-
{ break; }
67-
else if( c == 'c' )
68-
{ borderType = BORDER_CONSTANT; }
69-
else if( c == 'r' )
70-
{ borderType = BORDER_REPLICATE; }
71-
//![check_keypress]
58+
for(;;)
59+
{
60+
//![update_value]
61+
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
62+
//![update_value]
7263

73-
//![update_value]
74-
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
75-
//![update_value]
64+
//![copymakeborder]
65+
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
66+
//![copymakeborder]
7667

77-
//![copymakeborder]
78-
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
79-
//![copymakeborder]
68+
//![display]
69+
imshow( window_name, dst );
70+
//![display]
8071

81-
//![display]
82-
imshow( window_name, dst );
83-
//![display]
84-
}
72+
//![check_keypress]
73+
char c = (char)waitKey(500);
74+
if( c == 27 )
75+
{ break; }
76+
else if( c == 'c' )
77+
{ borderType = BORDER_CONSTANT; }
78+
else if( c == 'r' )
79+
{ borderType = BORDER_REPLICATE; }
80+
//![check_keypress]
81+
}
8582

86-
return 0;
83+
return 0;
8784
}

0 commit comments

Comments
 (0)