1 Adding borders to your images {#tutorial_copyMakeBorder} 2 ============================= 3 4 Goal 5 ---- 6 7 In this tutorial you will learn how to: 8 9 - Use the OpenCV function @ref cv::copyMakeBorder to set the borders (extra padding to your 10 image). 11 12 Theory 13 ------ 14 15 @note The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler. 16 17 -# In our previous tutorial we learned to use convolution to operate on images. One problem that 18 naturally arises is how to handle the boundaries. How can we convolve them if the evaluated 19 points are at the edge of the image? 20 -# What most of OpenCV functions do is to copy a given image onto another slightly larger image and 21 then automatically pads the boundary (by any of the methods explained in the sample code just 22 below). This way, the convolution can be performed over the needed pixels without problems (the 23 extra padding is cut after the operation is done). 24 -# In this tutorial, we will briefly explore two ways of defining the extra padding (border) for an 25 image: 26 27 -# **BORDER_CONSTANT**: Pad the image with a constant value (i.e. black or \f$0\f$ 28 -# **BORDER_REPLICATE**: The row or column at the very edge of the original is replicated to 29 the extra border. 30 31 This will be seen more clearly in the Code section. 32 33 Code 34 ---- 35 36 -# **What does this program do?** 37 - Load an image 38 - Let the user choose what kind of padding use in the input image. There are two options: 39 40 -# *Constant value border*: Applies a padding of a constant value for the whole border. 41 This value will be updated randomly each 0.5 seconds. 42 -# *Replicated border*: The border will be replicated from the pixel values at the edges of 43 the original image. 44 45 The user chooses either option by pressing 'c' (constant) or 'r' (replicate) 46 - The program finishes when the user presses 'ESC' 47 48 -# The tutorial code's is shown lines below. You can also download it from 49 [here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp) 50 @include samples/cpp/tutorial_code/ImgTrans/copyMakeBorder_demo.cpp 51 52 Explanation 53 ----------- 54 55 -# First we declare the variables we are going to use: 56 @code{.cpp} 57 Mat src, dst; 58 int top, bottom, left, right; 59 int borderType; 60 Scalar value; 61 char* window_name = "copyMakeBorder Demo"; 62 RNG rng(12345); 63 @endcode 64 Especial attention deserves the variable *rng* which is a random number generator. We use it to 65 generate the random border color, as we will see soon. 66 67 -# As usual we load our source image *src*: 68 @code{.cpp} 69 src = imread( argv[1] ); 70 71 if( !src.data ) 72 { return -1; 73 printf(" No data entered, please enter the path to an image file \n"); 74 } 75 @endcode 76 -# After giving a short intro of how to use the program, we create a window: 77 @code{.cpp} 78 namedWindow( window_name, WINDOW_AUTOSIZE ); 79 @endcode 80 -# Now we initialize the argument that defines the size of the borders (*top*, *bottom*, *left* and 81 *right*). We give them a value of 5% the size of *src*. 82 @code{.cpp} 83 top = (int) (0.05*src.rows); bottom = (int) (0.05*src.rows); 84 left = (int) (0.05*src.cols); right = (int) (0.05*src.cols); 85 @endcode 86 -# The program begins a *while* loop. If the user presses 'c' or 'r', the *borderType* variable 87 takes the value of *BORDER_CONSTANT* or *BORDER_REPLICATE* respectively: 88 @code{.cpp} 89 while( true ) 90 { 91 c = waitKey(500); 92 93 if( (char)c == 27 ) 94 { break; } 95 else if( (char)c == 'c' ) 96 { borderType = BORDER_CONSTANT; } 97 else if( (char)c == 'r' ) 98 { borderType = BORDER_REPLICATE; } 99 @endcode 100 -# In each iteration (after 0.5 seconds), the variable *value* is updated... 101 @code{.cpp} 102 value = Scalar( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) ); 103 @endcode 104 with a random value generated by the **RNG** variable *rng*. This value is a number picked 105 randomly in the range \f$[0,255]\f$ 106 107 -# Finally, we call the function @ref cv::copyMakeBorder to apply the respective padding: 108 @code{.cpp} 109 copyMakeBorder( src, dst, top, bottom, left, right, borderType, value ); 110 @endcode 111 The arguments are: 112 113 -# *src*: Source image 114 -# *dst*: Destination image 115 -# *top*, *bottom*, *left*, *right*: Length in pixels of the borders at each side of the image. 116 We define them as being 5% of the original size of the image. 117 -# *borderType*: Define what type of border is applied. It can be constant or replicate for 118 this example. 119 -# *value*: If *borderType* is *BORDER_CONSTANT*, this is the value used to fill the border 120 pixels. 121 122 -# We display our output image in the image created previously 123 @code{.cpp} 124 imshow( window_name, dst ); 125 @endcode 126 127 Results 128 ------- 129 130 -# After compiling the code above, you can execute it giving as argument the path of an image. The 131 result should be: 132 133 - By default, it begins with the border set to BORDER_CONSTANT. Hence, a succession of random 134 colored borders will be shown. 135 - If you press 'r', the border will become a replica of the edge pixels. 136 - If you press 'c', the random colored borders will appear again 137 - If you press 'ESC' the program will exit. 138 139 Below some screenshot showing how the border changes color and how the *BORDER_REPLICATE* 140 option looks: 141 142  143