在进行卷积操作的时候,我们需要对图像边缘的像素也进行卷积操作,就需要在边缘外在进行填充才行。
openCV中默认的处理方法是: BORDER_DEFAULT,此外
常用的还有如下几种:
- BORDER_CONSTANT – 填充边缘用指定像素值
- BORDER_REPLICATE – 填充边缘像素用已知的边缘像素值。
- BORDER_WRAP – 用另外一边的像素来补偿填充

int top = (int)(0.05*src.rows);
int bottom = (int)(0.05*src.rows);
int left = (int)(0.05*src.cols);
int right = (int)(0.05*src.cols);
RNG rng(12345);
int borderType = BORDER_DEFAULT;
int c = 0;
while (true) {
c = waitKey(500);//等待500ms
// ESC退出
if ((char)c == 27) {
break;
}
if ((char)c == 'r') {
borderType = BORDER_REPLICATE;
} else if((char)c == 'w') {
borderType = BORDER_WRAP;
} else if((char)c == 'c') {
borderType = BORDER_CONSTANT;
}
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));//随机生成颜色
copyMakeBorder(src, dst, top, bottom, left, right, borderType, color);//填充
imshow(OUTPUT_WIN, dst);
}
这篇博客探讨了在OpenCV中进行卷积操作时如何处理图像边缘的问题。作者介绍了BORDER_DEFAULT等不同类型的边界填充方法,包括BORDER_CONSTANT(使用指定像素值填充)、BORDER_REPLICATE(复制边缘像素)和BORDER_WRAP(使用相邻边的像素填充)。通过示例代码展示了如何使用这些方法,并允许用户实时选择填充类型以观察效果。
9871

被折叠的 条评论
为什么被折叠?



