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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

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.