OpenCV Python ŌĆō How to add borders to an image?
In image processing, adding borders to images can be useful for different purposes such as decorative purposes, image segmentation, etc. In this article, we will learn how to add borders to an image using OpenCV Python.
Prerequisites
Before we can begin, we need to have OpenCV installed on our system. You can install OpenCV using pip by running the following command:
pip install opencv-python
Adding a Border to an Image
We can add borders to an image using the cv2.copyMakeBorder()
function from the OpenCV library. The cv2.copyMakeBorder()
function takes the following parameters:
cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value)
Here is what each parameter means:
src
: The source imagetop
: The number of pixels to add to the top of the imagebottom
: The number of pixels to add to the bottom of the imageleft
: The number of pixels to add to the left of the imageright
: The number of pixels to add to the right of the imageborderType
: The type of border to add (we will cover this later in the article)value
: The value of the border pixels (we will cover this later in the article)
Let’s take a look at an example.
And we want to add a 10-pixel border around it. We can do that by using the cv2.copyMakeBorder()
function as follows:
import cv2
# Load the image
img = cv2.imread("sample_image.jpg")
# Add a 10-pixel border around the image
bordered_img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_CONSTANT, None)
Here, we have added a 10-pixel border around the image using cv2.copyMakeBorder()
function with the cv2.BORDER_CONSTANT
flag to specify that we want to add a constant border. We have also passed None
as the value
parameter, which means that the value of the border pixels will be black (0, 0, 0).
As we can see, a 10-pixel border has been added to the image.
Border Types
In the previous example, we used the cv2.BORDER_CONSTANT
flag to specify that we want to add a constant border. There are several other border types that we can use depending on the purpose of the border.
Replicate Border
The replicate border type adds a copy of the border pixels to the edges of the image. This is useful for image segmentation, where we want to extend the edges of an object. We can use the cv2.BORDER_REPLICATE
flag to specify this border type.
# Add a 10-pixel replicate border around the image
bordered_img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_REPLICATE, None)
Reflect Border
The reflect border type reflects the pixels along the border edge. This is useful for decorative purposes or image distortion correction. We can use the cv2.BORDER_REFLECT
flag to specify this border type.
# Add a 10-pixel reflect border around the image
bordered_img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_REFLECT, None)
Reflect 101 Border
The reflect 101 border type reflects the pixels along the border edge but also reverses the order of the reflected pixels. This is useful for image distortion correction. We can use the cv2.BORDER_REFLECT_101
flag to specify this border type.
# Add a 10-pixel reflect 101 border around the image
bordered_img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_REFLECT_101, None)
Wrap Border
The wrap border type wraps the image around itself. This is useful for creating images that are seamlessly tiling. We can use the cv2.BORDER_WRAP
flag to specify this border type.
# Add a 10-pixel wrap border around the image
bordered_img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_WRAP, None)
Border Colors
In the previous examples, we used None
as the value
parameter, which means that the value of the border pixels will be black (0, 0, 0). We can also specify a color for the border pixels by passing a tuple of (B,G,R)
values as the value
parameter. Let’s take a look at an example:
# Add a 10-pixel border around the image with a red color
bordered_img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_CONSTANT, (0, 0, 255))
In this example, we have added a 10-pixel border around the image with a red color (0, 0, 255)
.
Conclusion
In this article, we have learned how to add borders to an image using OpenCV Python. We have also learned about the different border types and border colors that we can use when adding borders to images. Adding borders to images is just one of the many image processing techniques that we can use to enhance or modify images. With the power of OpenCV Python, we can explore and experiment with many other image processing techniques to take our image processing skills to the next level.