在卷積開始之前增加邊緣像素淘讥,填充的像素值為0或者RGB黑色,比如3x3在
四周各填充1個像素的邊緣,這樣就確保圖像的邊緣被處理扣草,在卷積處理之
后再去掉這些邊緣。openCV中默認(rèn)的處理方法是: BORDER_DEFAULT颜屠,此外
常用的還有如下幾種:
- BORDER_CONSTANT – 填充邊緣用指定像素值
- BORDER_REPLICATE – 填充邊緣像素用已知的邊緣像素值辰妙。
- BORDER_WRAP – 用另外一邊的像素來補(bǔ)償填充
copyMakeBorder(
- Mat src, // 輸入圖像
- Mat dst, // 添加邊緣圖像
- int top, // 邊緣長度,一般上下左右都取相同值甫窟,
- int bottom,
- int left,
- int right,
- int borderType // 邊緣類型
- Scalar value
)
示例代碼
#include "pch.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
int main(int argc, char** argv) {
Mat src, dst;
src = imread("D:/11.jpg");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char INPUT_WIN[] = "input image";
namedWindow(INPUT_WIN, 0);
namedWindow("bordefault", 0);
namedWindow("borreplicate", 0);
namedWindow("borwrap", 0);
namedWindow("borconstant", 0);
imshow(INPUT_WIN, src);
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 bordefault = BORDER_DEFAULT;
int borreplicate = BORDER_REPLICATE;
int borwrap = BORDER_WRAP;
int borconstant = BORDER_CONSTANT;
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
copyMakeBorder(src, dst, top, bottom, left, right, bordefault, color);
imshow("bordefault", dst);
copyMakeBorder(src, dst, top, bottom, left, right, borreplicate, color);
imshow("borreplicate", dst);
copyMakeBorder(src, dst, top, bottom, left, right, borwrap, color);
imshow("borwrap", dst);
copyMakeBorder(src, dst, top, bottom, left, right, borconstant, color);
imshow("borconstant", dst);
waitKey(0);
return 0;
}