Goal

In this tutorial you will learn how to:

  • Use the OpenCV function copyMakeBorder() to set the borders (extra padding to your image).
  • 使用OpenCV函数 copyMakeBorder() 来进行边界设置(为图像进行额外的填充)

Theory

Note

The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler.

      理论来源与《Learning OpenCV》这本书
  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. 之前学习了卷积,但如何处理边界呢?特别是当需要计算的对象位于图像的边缘部分的时候。
  3. 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).
  4. 大部分OpenCV函数做的是复制一个给定的图片到另外一个稍微大一点的图片上,并且自动填充(以下面讲解的方法)。使用这种方法,就可以没有问题的进行卷积处理了(额外填充的部分在卷积结束之后去切除)。
  5. 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.

  6. 这里简要的介绍了两种填充图像边界的方式

              1.常数,比如零

              2.复制原来图像的边缘部分。

  • 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.(常数,且以0.5秒随机变化)
      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)(选择‘c’or‘r’是这两种方式)

    • The program finishes when the user presses 'ESC'(推出按下“ESC”)

Code

#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
// Declare the variables
Mat src, dst;

int top, bottom, left, right;
int borderType = BORDER_CONSTANT;
const char* window_name = "copyMakeBorder Demo";
RNG rng(12345); //随机数种子
int main( int argc, char** argv )
{
    const char* imageName = argc >=2 ? argv[1] : "lena.jpg";
    // Loads an image
    src = imread( imageName, IMREAD_COLOR ); // Load an image
    // Check if image is loaded fine
    if( src.empty()) {
        printf(" Error opening image\n");
        printf(" Program Arguments: [image_name -- default lena.jpg] \n");
        return -1;
    }
    // Brief how-to for this program
    printf( "\n \t copyMakeBorder Demo: \n" );
    printf( "\t -------------------- \n" );
    printf( " ** Press 'c' to set the border to a random constant value \n");
    printf( " ** Press 'r' to set the border to be replicated \n");
    printf( " ** Press 'ESC' to exit the program \n");
    namedWindow( window_name, WINDOW_AUTOSIZE );
    // Initialize arguments for the filter
    //上下左右,扩展多少行和列。

    top = (int) (0.05*src.rows); bottom = top;
    left = (int) (0.05*src.cols); right = left;
    for(;;)
    {
        Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );//BGR
        copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
        //输入,输出,顶部,底部,左,右,类型,填充的值。当为BORDER_CONSTANT,最后的value才有效。
        imshow( window_name, dst );
        char c = (char)waitKey(500);
        if( c == 27 )
        { break; }
        else if( c == 'c' )
        { borderType = BORDER_CONSTANT; }
        else if( c == 'r' )
        { borderType = BORDER_REPLICATE; }
    }
    return 0;

}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

set(CMAKE_CXX_FLAGS "-std=c++11")
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )


install(TARGETS DisplayImage RUNTIME DESTINATION bin)

Results

当为"c"

Image Processing10(Adding borders to your images )

Image Processing10(Adding borders to your images )

当为“r”的时候:

Image Processing10(Adding borders to your images )

相关文章:

  • 2021-11-16
  • 2021-12-19
  • 2021-06-04
  • 2021-07-08
  • 2021-12-20
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-05
  • 2021-08-06
  • 2021-12-03
  • 2021-08-03
  • 2022-12-23
  • 2021-10-09
  • 2022-01-18
相关资源
相似解决方案