OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Adding borders to your images

Goal

In this tutorial you will learn how to:

Theory

Note
The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler.
  1. In our previous tutorial we learned to use convolution to operate on images. One problem that naturally arises is how to handle the boundaries. How can we convolve them if the evaluated points are at the edge of the image?
  2. What most of OpenCV functions do is to copy a given image onto another slightly larger image and then automatically pads the boundary (by any of the methods explained in the sample code just below). This way, the convolution can be performed over the needed pixels without problems (the extra padding is cut after the operation is done).
  3. In this tutorial, we will briefly explore two ways of defining the extra padding (border) for an image:

    1. BORDER_CONSTANT: Pad the image with a constant value (i.e. black or \(0\)
    2. BORDER_REPLICATE: The row or column at the very edge of the original is replicated to the extra border.

    This will be seen more clearly in the Code section.

Code

  1. What does this program do?
    • Load an image
    • Let the user choose what kind of padding use in the input image. There are two options:

      1. Constant value border: Applies a padding of a constant value for the whole border. This value will be updated randomly each 0.5 seconds.
      2. Replicated border: The border will be replicated from the pixel values at the edges of the original image.

      The user chooses either option by pressing 'c' (constant) or 'r' (replicate)

    • The program finishes when the user presses 'ESC'
  2. The tutorial code's is shown lines below. You can also download it from here
    1 
    8 #include "opencv2/imgcodecs.hpp"
    10 #include <stdlib.h>
    11 #include <stdio.h>
    12 
    13 using namespace cv;
    14 
    16 Mat src, dst;
    17 int top, bottom, left, right;
    18 int borderType;
    19 const char* window_name = "copyMakeBorder Demo";
    20 RNG rng(12345);
    21 
    25 int main( int, char** argv )
    26 {
    27 
    28  int c;
    29 
    31  src = imread( argv[1] );
    32 
    33  if( src.empty() )
    34  {
    35  printf(" No data entered, please enter the path to an image file \n");
    36  return -1;
    37  }
    38 
    40  printf( "\n \t copyMakeBorder Demo: \n" );
    41  printf( "\t -------------------- \n" );
    42  printf( " ** Press 'c' to set the border to a random constant value \n");
    43  printf( " ** Press 'r' to set the border to be replicated \n");
    44  printf( " ** Press 'ESC' to exit the program \n");
    45 
    47  namedWindow( window_name, WINDOW_AUTOSIZE );
    48 
    50  top = (int) (0.05*src.rows); bottom = (int) (0.05*src.rows);
    51  left = (int) (0.05*src.cols); right = (int) (0.05*src.cols);
    52  dst = src;
    53 
    54  imshow( window_name, dst );
    55 
    56  for(;;)
    57  {
    58  c = waitKey(500);
    59 
    60  if( (char)c == 27 )
    61  { break; }
    62  else if( (char)c == 'c' )
    63  { borderType = BORDER_CONSTANT; }
    64  else if( (char)c == 'r' )
    65  { borderType = BORDER_REPLICATE; }
    66 
    67  Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
    68  copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
    69 
    70  imshow( window_name, dst );
    71  }
    72 
    73  return 0;
    74 }
    aaaaaa|abcdefgh|hhhhhhh
    Definition: base.hpp:235
    Mat imread(const String &filename, int flags=IMREAD_COLOR)
    Loads an image from a file.
    void imshow(const String &winname, InputArray mat)
    Displays an image in the specified window.
    void copyMakeBorder(InputArray src, OutputArray dst, int top, int bottom, int left, int right, int borderType, const Scalar &value=Scalar())
    Forms a border around an image.
    void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
    Creates a window.
    Random Number Generator.
    Definition: core.hpp:2600
    Definition: highgui.hpp:138
    iiiiii|abcdefgh|iiiiiii with some specified i
    Definition: base.hpp:234
    int main(int argc, const char *argv[])
    Definition: facerec_demo.cpp:67
    n-dimensional dense array class
    Definition: mat.hpp:726
    int waitKey(int delay=0)
    Waits for a pressed key.

Explanation

  1. First we declare the variables we are going to use:
    Mat src, dst;
    int top, bottom, left, right;
    int borderType;
    Scalar value;
    char* window_name = "copyMakeBorder Demo";
    RNG rng(12345);
    Especial attention deserves the variable rng which is a random number generator. We use it to generate the random border color, as we will see soon.
  2. As usual we load our source image src:
    src = imread( argv[1] );
    if( !src.data )
    { return -1;
    printf(" No data entered, please enter the path to an image file \n");
    }
  3. After giving a short intro of how to use the program, we create a window:
    namedWindow( window_name, WINDOW_AUTOSIZE );
  4. Now we initialize the argument that defines the size of the borders (top, bottom, left and right). We give them a value of 5% the size of src.
    top = (int) (0.05*src.rows); bottom = (int) (0.05*src.rows);
    left = (int) (0.05*src.cols); right = (int) (0.05*src.cols);
  5. The program begins a while loop. If the user presses 'c' or 'r', the borderType variable takes the value of BORDER_CONSTANT or BORDER_REPLICATE respectively:
    while( true )
    {
    c = waitKey(500);
    if( (char)c == 27 )
    { break; }
    else if( (char)c == 'c' )
    { borderType = BORDER_CONSTANT; }
    else if( (char)c == 'r' )
    { borderType = BORDER_REPLICATE; }
  6. In each iteration (after 0.5 seconds), the variable value is updated...
    value = Scalar( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
    with a random value generated by the RNG variable rng. This value is a number picked randomly in the range \([0,255]\)
  7. Finally, we call the function cv::copyMakeBorder to apply the respective padding:
    copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
    The arguments are:
    1. src: Source image
    2. dst: Destination image
    3. top, bottom, left, right: Length in pixels of the borders at each side of the image. We define them as being 5% of the original size of the image.
    4. borderType: Define what type of border is applied. It can be constant or replicate for this example.
    5. value: If borderType is BORDER_CONSTANT, this is the value used to fill the border pixels.
  8. We display our output image in the image created previously
    imshow( window_name, dst );

Results

  1. After compiling the code above, you can execute it giving as argument the path of an image. The result should be:

    • By default, it begins with the border set to BORDER_CONSTANT. Hence, a succession of random colored borders will be shown.
    • If you press 'r', the border will become a replica of the edge pixels.
    • If you press 'c', the random colored borders will appear again
    • If you press 'ESC' the program will exit.

    Below some screenshot showing how the border changes color and how the BORDER_REPLICATE option looks:

    CopyMakeBorder_Tutorial_Results.jpg