在做卷積處理時,圖片的邊緣經常不能進行處理,因為錨點無法覆蓋每個邊緣像素點,處理這種問題的一個有效的辦法就是 邊緣填補
有以下幾種方法:
- 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

