在做卷积处理时,图片的边缘经常不能进行处理,因为锚点无法覆盖每个边缘像素点,处理这种问题的一个有效的办法就是 边缘填补
有以下几种方法:
- BORDER_DEFAULT :将最近的像素进行映射
- BORDER_CONSTANT :用指定的像素填充边缘
- BORDER_REPLICATE :复制最近的一行或一列像素并一直延伸至添加边缘的宽度或高度
- BORDER_WRAP :用对面的像素值填充边缘
相关API:
copyMakeBorder(src, dst, top, bottom, left, right, border_type, color);
实例代码:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace std;
using namespace cv;
Mat src, dst;
int main(int argc, char** argv) {
src = imread("D:/OpenCVprj/image/test2.jpg");
imshow("src", src);
int top = (int)(src.rows*0.05);
int bottom = (int)(src.rows*0.05);
int left = (int)(src.cols*0.05);
int right = (int)(src.cols*0.05);
int border_type = 0;
RNG rng;
while (true) {
int c = waitKey(500);
if (c == 27) {
break;
}
else if ((char)c == 'c') {
border_type = BORDER_CONSTANT;
}
else if ((char)c == 'r') {
border_type = BORDER_REPLICATE;
}
else if ((char)c == 'w') {
border_type = BORDER_WRAP;
}
else {
border_type = BORDER_DEFAULT;
}
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
copyMakeBorder(src, dst, top, bottom, left, right, border_type, color);
imshow("dst", dst);
}
return 0;
}
结果:
BORDER_DEFAULT

BORDER_CONSTANT

BORDER_REPLICATE

BORDER_WRAP
