A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-opencv-cv2-copymakeborder-method/ below:

Python OpenCV | cv2.copyMakeBorder() method

Python OpenCV | cv2.copyMakeBorder() method

Last Updated : 11 Aug, 2025

The cv2.copyMakeBorder() function in OpenCV allows us to add a border around an image. This can be useful for various image processing tasks such as image padding, creating frames or preparing images for machine learning. In this article, we'll see how to use this function with different types and examples.

Lets see a basic example:

Python
import cv2
from google.colab.patches import cv2_imshow

image = cv2.imread("/content/geeks14.png")
image = cv2.copyMakeBorder(image, 10, 10, 10, 10, cv2.BORDER_CONSTANT, None, value = 0)

print(window_name)  
cv2_imshow(image)

Output:

Basic example

The output will be the original image with a 10-pixel black border added on all sides.

Syntax: 

cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value)

Parameters:

Return Value: It returns an image. 

Different Border Types

The borderType parameter controls the style of the border we add to the image. Let's see some common options:

  1. cv2.BORDER_CONSTANT: Adds a border with a constant color. We can set the color using the value parameter. For example, we can set value=(0, 0, 255) for a red border.
  2. cv2.BORDER_REFLECT: Border is a mirror reflection of the edge pixels. For example, if the image contains the sequence "abcdef", the border would be reflected as "gfedcba|abcdef|gfedcba".
  3. cv2.BORDER_REFLECT_101 (or cv2.BORDER_DEFAULT): Similar to BORDER_REFLECT but with a slight difference. If the image is "abcdefgh", the output will be "gfedcb|abcdefgh|gfedcba".
  4. cv2.BORDER_REPLICATE: Border is filled by replicating the outermost pixels of the image. For example, if the image is "abcdefgh", the output will be "aaaaa|abcdefgh|hhhhh". 
Example 1: Using Different Border Types

Here we will see how different border types affect the image. Each border type has a unique way of extending the edges of the image, either by adding solid colors, reflecting the edges or replicating the outermost pixels. This customization can be useful for a variety of image processing tasks.

Python
import cv2
from google.colab.patches import cv2_imshow

image = cv2.imread("/content/geeks14.png")


bordered_image_reflect = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REFLECT)
bordered_image_reflect_101 = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REFLECT_101)
bordered_image_replicate = cv2.copyMakeBorder(image, 50, 50, 50, 50, cv2.BORDER_REPLICATE)

print("Border with Reflect")
cv2_imshow(bordered_image_reflect)

print("Border with Reflect_101")
cv2_imshow(bordered_image_reflect_101)

print("Border with Replicate")
cv2_imshow(bordered_image_replicate)

Output:

Border with Reflect Border with Reflect_101 Border with Replicate Example 2: Customizing Border Color with fill_value

When using a constant border, we can choose the color for the border by setting the value parameter. This flexibility is helpful when we need to apply a specific color such as for creating frames or preparing images for machine learning tasks.

Python
import cv2
from google.colab.patches import cv2_imshow

image = cv2.imread('/content/geeks14.png')
bordered_image = cv2.copyMakeBorder(image, 10, 10, 10, 10,
                                    cv2.BORDER_CONSTANT, value=(0, 0, 255))  

print('Bordered Image with Red Border')
cv2_imshow(bordered_image)

Output:

Bordered Image with Red Border Use Cases for cv2.copyMakeBorder()

Adding borders to images can be helpful in various scenarios:

  1. Image Preprocessing for Neural Networks: Many machine learning models require input images to be of the same size. We can use cv2.copyMakeBorder() to pad images with borders and ensure uniformity.
  2. Enhancing Features: Sometimes, we want to add extra space around an object in the image to make it stand out more or to highlight certain areas for analysis.
  3. Visualization: Adding borders around images can help display them more clearly especially in a series of images where distinction is necessary.
Limitations Of cv2.copyMakeBorder() function
  1. Fixed Border Size: The border size must be specified manually which may not be ideal for dynamic or responsive applications.
  2. Limited Border Types: The available border types are predefined, limiting customization beyond the provided options.
  3. Color Restrictions with BORDER_CONSTANT: While the color of constant borders can be customized, it may not be suitable for complex patterns or gradients.
  4. No Automatic Resizing: It doesn’t resize the image to fit specific aspect ratios which can be a drawback in some scenarios.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4