opencv4 学习 11 padding 操作
1. opencv内置函数的使用:
copyMakeBorder(InputArray src, OutputArray dst, int top, int bottom, int left, int right, int borderType, const Scalar &value)
其中 borderType 参数支持两种模式:
- BORDER_CONSTANT: Pad the image with a constant value
- BORDER_REPLICATE: The row or column at the very edge of the original is replicated to the extra border。
int top, int bottom, int left, int right:分别表示各个方向的填充尺寸。
#include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> using namespace cv; Mat src, dst; int top, bottom, left=0, right=0; // border size int borderType = BORDER_CONSTANT; const char* window_name = "copyMakeBorder Demo"; RNG rng(12345); int main(int arc, char* argv){ const char* imageName = "lena.jpg"; src = imread(imageName, IMREAD_COLOR); 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)); copyMakeBorder(src, dst, top, bottom, left, right, borderType, value); imshow(window_name, dst); char c = (char)waitKey(500); if(c == 27) break; if(c == 'c') borderType = BORDER_CONSTANT; if(c == 'r') borderType = BORDER_REPLICATE; } std::cout << src.rows << dst.rows << left << std::endl; return 0; }
参考:
https://docs.opencv.org/3.4/dc/da3/tutorial_copyMakeBorder.html