我假设您正在使用 copyMakeBorder
C++: void copyMakeBorder(InputArray src, OutputArray dst, int top, int bottom, int left, int right, int borderType, const Scalar& value=Scalar() )
Python: cv2.copyMakeBorder(src, top, bottom, left, right, borderType[, dst[, value]]) → dst
在 value 中,您指定填充的颜色(当使用 BORDER_CONSTANT
时),它在顶部、底部、左侧和右侧都相同。
value – Border value if borderType==BORDER_CONSTANT.
Mat3b img; // Your original image
...
Mat3b padded1;
copyMakeBorder(img, padded1, 10, 10, 10, 10, BORDER_CONSTANT, Scalar(0,255,0));
// ^^^ ^^^^^^ ^^^^ ^^^^^
// top bottom left right
// Now padded1 has a green border of 10 pixels around the original image.
如果你真的想为顶部、底部等设置不同的填充颜色,你可以这样做:
Mat3b img; // Your original image
...
Mat3b padded2;
copyMakeBorder(img , padded2, 10, 0, 0, 0, BORDER_CONSTANT, Scalar(255,0,0));
copyMakeBorder(padded2, padded2, 0, 10, 0, 0, BORDER_CONSTANT, Scalar(0,255,0));
copyMakeBorder(padded2, padded2, 0, 0, 10, 0, BORDER_CONSTANT, Scalar(0,0,255));
copyMakeBorder(padded2, padded2, 0, 0, 0, 10, BORDER_CONSTANT, Scalar(255,0,255));
// Now padded2 has different padding color in each side.
更新
通常您只想为 CV_8UC1
图像创建一些填充。很简单:
Mat3b img; // Your original image
...
Mat1b gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
// Add a black padding (of 10 pixels) around the image
Mat1b padded3;
copyMakeBorder(gray, padded3, 10, 10, 10, 10, BORDER_CONSTANT, Scalar(0));
我是一名优秀的程序员,十分优秀!