5 Best Ways to Add Borders to an Image with OpenCV in Python

πŸ’‘ Problem Formulation: Adding borders to images can be essential in various applications such as photography, graphic design, or when preparing images for machine learning tasks to maintain a consistent size. For instance, you might have an image of 200×300 pixels and want to add a 50-pixel-wide border to all sides, resulting in a final image size of 300×400 pixels.

Method 1: Using the copyMakeBorder function

OpenCV’s copyMakeBorder function provides a versatile way to add borders around an image. It allows for different border types, including constant, reflect, replicate, and more. A constant border type can add a solid color border around the image.

Here’s an example:

import cv2
import numpy as np

# Read the image
image = cv2.imread('path_to_image.jpg')

# Define the border width and color
top, bottom, left, right = [50]*4
border_color = (255, 0, 0)  # Blue color border

# Create border using copyMakeBorder
bordered_image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=border_color)

# Save or display the image
cv2.imwrite('bordered_image.jpg', bordered_image)

Output: This code will produce an image with a blue border with a width of 50 pixels on all sides.

The code snippet demonstrates how to apply a solid color border to an existing image using OpenCV. After importing the necessary modules, an image is loaded with cv2.imread. The border color and widths are defined, and then cv2.copyMakeBorder applies the specified border. Finally, the image is saved with the new border applied.

Method 2: Utilizing Array Operations

Array operations can manually add borders to an image by concatenating arrays of the border color on each side. This method requires understanding shape and data type of the image array.

Here’s an example:

import cv2
import numpy as np

# Read the image
image = cv2.imread('path_to_image.jpg')

# Define the border width and color
border_width = 50
border_color = [255, 0, 0]  # Blue color border

# Create the border arrays
top_border = np.full((border_width, image.shape[1], 3), border_color, dtype=np.uint8)
bottom_border = top_border
left_border = np.full((image.shape[0], border_width, 3), border_color, dtype=np.uint8)
right_border = left_border

# Concatenate the borders to the image
bordered_image = np.vstack((top_border, image, bottom_border))
bordered_image = np.hstack((left_border, bordered_image, right_border))

# Save or display the image
cv2.imwrite('bordered_image.jpg', bordered_image)

Output: This will create an image that has a blue border with a width of 50 pixels on all sides, similar to Method 1.

Instead of a built-in function, this method manually creates arrays for the border using NumPy’s np.full, and then concatenates these to the original image using np.vstack and np.hstack. This allows for significant flexibility but requires more comprehensive handling of array shapes and types.

Method 3: Border Reflection

OpenCV’s copyMakeBorder function can be used to create reflective borders, which can give the effect of the image reflecting on its edges. This can be particularly useful for creating artistic effects or when the border needs to be visually continuous with the image content.

Here’s an example:

import cv2

# Read the image
image = cv2.imread('path_to_image.jpg')

# Define the border width
border_width = 50

# Create reflective border using copyMakeBorder
bordered_image = cv2.copyMakeBorder(image, border_width, border_width, border_width, border_width, cv2.BORDER_REFLECT)

# Save or display the image
cv2.imwrite('bordered_image.jpg', bordered_image)

Output: The produced image will have a reflective border around the edges.

This code utilizes cv2.copyMakeBorder with the BORDER_REFLECT option to show how the pixel values at the edges of the image are mirrored in the border, creating a seamless extension of the image at its edges.

Method 4: Border Replication

Replicating the edge pixels to form a border can sometimes be desirable, especially when you don’t want the border to distract from the main image content. OpenCV’s copyMakeBorder function with BORDER_REPLICATE option does exactly this.

Here’s an example:

import cv2

# Read the image
image = cv2.imread('path_to_image.jpg')

# Define the border width
border_width = 50

# Create replicated border using copyMakeBorder
bordered_image = cv2.copyMakeBorder(image, border_width, border_width, border_width, border_width, cv2.BORDER_REPLICATE)

# Save or display the image
cv2.imwrite('bordered_image.jpg', bordered_image)

Output: An image with borders replicating the edge pixels.

The snippet applies cv2.copyMakeBorder with BORDER_REPLICATE, causing the outermost pixels to be stretched across the border width, which can be a useful technique where a nondistracting border is needed.

Bonus One-Liner Method 5: Adding a White Border

For a quick solution, adding a white border can be done with a one-liner using NumPy for array manipulation combined with OpenCV.

Here’s an example:

import cv2
import numpy as np

# Read the image and add a white border in one line
bordered_image = cv2.copyMakeBorder(cv2.imread('path_to_image.jpg'), 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=[255, 255, 255])

# Save or display the image
cv2.imwrite('bordered_image.jpg', bordered_image)

Output: The resulting image will feature a simple, clean, white border of 50 pixels on all sides.

In this concise example, copyMakeBorder is used to add a constant white border. The method is straightforward, demonstrating the power of OpenCV to perform complex tasks in single lines of code.

Summary/Discussion

  • Method 1: Using copyMakeBorder with constant value. Flexible and straightforward. Allows for custom color choices, though requires multiple parameters.
  • Method 2: Utilizing Array Operations. Offers great flexibility and manual control. Can be more complex and verbose.
  • Method 3: Border Reflection. Creates visually continuous borders. May not be suitable for all images, especially with distinctive edge content.
  • Method 4: Border Replication. Simple and unobtrusive. The border competes less with the main image but lacks the option for color customization.
  • Method 5: One-Liner for White Border. Quick and extremely efficient for a common bordering need. Not suitable for color customization or different border types.