OpenCV
4.5.1
Open Source Computer Vision
|
Prev Tutorial: Making your own linear filters!
Next Tutorial: Sobel Derivatives
Original author | Ana Huamán |
Compatibility | OpenCV >= 3.0 |
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.
First we declare the variables we are going to use:
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:
After giving a short intro of how to use the program, we create a window:
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.
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:
In each iteration (after 0.5 seconds), the random border color (value) is updated...
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:
We display our output image in the image created previously
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: