What are the types of borders which can be made using OpenCV

This recipe explains what are the types of borders which can be made using OpenCV

Recipe Objective: What are the types of borders which can be made using OpenCV?

Let us learn about the different types of borders that we can create in detail using OpenCV.

Step 1: Import libraries and read the image

First, let us import the necessary libraries and read the image in default mode using the cv2.imread() function. The image that we are using in this recipe is the one below.

ProjectPro logo

import cv2
image = cv2.imread('project.jpg')

Step 2: Understanding different types of borders in OpenCV

We can create borders around an image in OpenCV using the cv2.copyMakeBorder(), which takes the following input parameters.

  • src: The image on which we have to draw a border
  • top: Width of the border in the top.
  • bottom: Width of the border in the bottom
  • left: Width of the border in the left
  • right: Width of the border in the right
  • borderType: Type of the border

Of all the above parameters, the borderType parameter is the one that determines the type of the border, and its flag values are listed below.

  • cv2.BORDER_CONSTANT
  • cv2.BORDER_REFLECT
  • cv2.BORDER_REFLECT_101
  • cv2.BORDER_REPLICATE
  • cv2.BORDER_WRAP

Let us see each one of them in detail with an example.

The cv2.BORDER_CONSTANT adds a constant colored border to the image. The color of the border can be passed as an optional parameter called value. Otherwise, a plain black colored border is created.


image_bordered_1 = cv2.copyMakeBorder(src=image, top=15, bottom=15, left=15, right=15, borderType=cv2.BORDER_CONSTANT)
cv2.imshow('Bordered Constant',image_bordered_1)
cv2.waitKey(0)

Output:

border_constant.jpg 

image_bordered_2 = cv2.copyMakeBorder(src=image, top=15, bottom=15, left=15, right=15, borderType=cv2.BORDER_CONSTANT, value=(255,0,0))
cv2.imshow('Bordered Constant with value',image_bordered_2)
cv2.waitKey(0)

Output:

border_constant_value.jpg 

The cv2.BORDER_REFLECT creates borders which is a mirror reflection of border elements like this: PtcejorP|ProjectPro|orPtcejorP

image_bordered_3 = cv2.copyMakeBorder(src=image, top=120, bottom=120, left=120, right=120, borderType=cv2.BORDER_REFLECT)
cv2.imshow('Bordered Reflect',image_bordered_3)
cv2.waitKey(0)

Output:

border_reflect.jpg 

The cv2.BORDER_REFLECT_101 produces the border same as cv2.BORDER_REFLECT but with a little bit of a difference like this: rojectPr|ProjectPro|rojectPro

image_bordered_4 = cv2.copyMakeBorder(src=image, top=120, bottom=120, left=120, right=120, borderType=cv2.BORDER_REFLECT_101)
cv2.imshow('Bordered Reflect 101',image_bordered_4)
cv2.waitKey(0)

Output:

border_reflect_101.jpg 

The cv2.BORDER_REPLICATE creates a border by replicating the last element of the image like this: PPPPPPP|ProjectPro|ooooooo

image_bordered_5 = cv2.copyMakeBorder(src=image, top=120, bottom=120, left=120, right=120, borderType=cv2.BORDER_REPLICATE)
cv2.imshow('Bordered Replicate',image_bordered_5)
cv2.waitKey(0)

Output:

border_replicate.jpg 

The cv2.BORDER_WRAP forms a border like this: ojectPro|ProjectPro|ProjectP

image_bordered_6 = cv2.copyMakeBorder(src=image, top=120, bottom=120, left=120, right=120, borderType=cv2.BORDER_WRAP)
cv2.imshow('Bordered Wrap',image_bordered_6)
cv2.waitKey(0)

Output:

border_wrap.jpg 

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .