Rate This Document
Findability
Accuracy
Completeness
Readability

copyMakeBorder

Usage

Adds a border around an image. It is used for data augmentation, avoiding edge effects, or adding decorative borders to images.

Interface

1
void copyMakeBorder(InputArray src, OutputArray dst, int top, int bottom, int left, int right, int borderType, const Scalar& value = Scalar());

Parameters

Parameter

Description

Value Range

Input/Output

src

Input image.

Not null

Input

dst

Output image.

Mat class

Input

top

Border size in top direction.

(0, inf)

Input

bottom

Border size in bottom direction.

(0, inf)

Input

left

Border size in left direction.

(0, inf)

Input

right

Border size in right direction.

(0, inf)

Input

borderType

Border type.

  • BORDER_CONSTANT
  • BORDER_REPLICATE
  • BORDER_REFLECT
  • BORDER_WRAP, etc.

Input

value

Constant value used to fill the border (valid only for a constant border).

(0, inf)

Input

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;

int main() {
    // Read the image.
    Mat src = imread("input.jpg");
    if (src.empty()) {
        std::cerr << "Failed to load the image!" << std::endl;
        return -1;
    }

    // Set border sizes.
    int top = 10, bottom = 10, left = 20, right = 20;

    // Image expansion
    Mat dst;
    copyMakeBorder(src, dst, top, bottom, left, right, BORDER_CONSTANT, Scalar(255, 255, 255));

    // Display the result.
    imshow("Original image", src);
    imshow("Image after expansion", dst);
    waitKey(0);

    return 0;
}