OpenCV
4.1.0
Open Source Computer Vision
|
Prev Tutorial: Making your own linear filters!
Next Tutorial: Sobel Derivatives
In this tutorial you will learn how to:
In this tutorial, we will briefly explore two ways of defining the extra padding (border) for an image:
This will be seen more clearly in the Code section.
Let the user choose what kind of padding use in the input image. There are two options:
The user chooses either option by pressing 'c' (constant) or 'r' (replicate)
The tutorial code's is shown lines below.
[block] [block] [block]First we declare the variables we are going to use:
[block] [block] [block]Especial attention deserves the variable rng which is a random number generator. We use it to generate the random border color, as we will see soon.
As usual we load our source image src:
[block] [block] [block]After giving a short intro of how to use the program, we create a window:
[block] [block] [block]Now we initialize the argument that defines the size of the borders (top, bottom, left and right). We give them a value of 5% the size of src.
[block] [block] [block]The program runs in an infinite loop while the key ESC isn't pressed. If the user presses 'c' or 'r', the borderType variable takes the value of BORDER_CONSTANT or BORDER_REPLICATE respectively:
[block] [block] [block]In each iteration (after 0.5 seconds), the random border color (value) is updated...
[block] [block] [block]This value is a set of three numbers picked randomly in the range \([0,255]\).
Finally, we call the function copyMakeBorder() to apply the respective padding:
[block] [block] [block]The arguments are:
We display our output image in the image created previously
[block] [block] [block]After compiling the code above, you can execute it giving as argument the path of an image. The result should be:
Below some screenshot showing how the border changes color and how the BORDER_REPLICATE option looks: