#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"#include <stdlib.h>#include <stdio.h>usingnamespacecv;/// 全局变量Matsrc,dst;inttop,bottom,left,right;intborderType;Scalarvalue;char*window_name="copyMakeBorder Demo";RNGrng(12345);/** @函数 main */intmain(intargc,char**argv){intc;/// 装载图像src=imread(argv[1]);if(!src.data){return-1;printf(" No data entered, please enter the path to an image file \n");}/// 使用说明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,CV_WINDOW_AUTOSIZE);/// 初始化输入参数top=(int)(0.05*src.rows);bottom=(int)(0.05*src.rows);left=(int)(0.05*src.cols);right=(int)(0.05*src.cols);dst=src;imshow(window_name,dst);while(true){c=waitKey(500);if((char)c==27){break;}elseif((char)c=='c'){borderType=BORDER_CONSTANT;}elseif((char)c=='r'){borderType=BORDER_REPLICATE;}value=Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255));copyMakeBorder(src,dst,top,bottom,left,right,borderType,value);imshow(window_name,dst);}return0;}
void copyMakeBorder(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int borderType, const Scalar&value=Scalar())
Parameters:
src – The source image
dst – The destination image; will have the same type as src and the size Size(src.cols+left+right,src.rows+top+bottom)
top, bottom, left, right – Specify how much pixels in each direction from the source image rectangle one needs to extrapolate, e.g. top=1,bottom=1,left=1,right=1 mean that 1 pixel-wide border needs to be built