卷积边缘问题
图像卷积的时候边界像素,不能被卷积操作,原因在于边界像素没有完全跟kernel重叠,所以当3x3滤波时候有1个像素的边缘没有被处理,5x5滤波的时候有2个像素的边缘没有被处理。
边缘类型
BORDER_CONSTANT 填充边缘用指定像素
BORDER_REPLICATE 填充边缘像素用已知的边缘像素值
BORDER_WRAP 用另一边的像素来补偿填充
相关API
copyMakeBorder(
src, dst
int top, int bottom, int left, int right //边缘长度,一般上下左右都取相同的值
int borderType //边缘类型
Scalar value)
代码展示
几种边缘处理对比
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std
;
using namespace cv
;
int main()
{
Mat src
, dst
;
src
= imread("F:/Opencvlearn/picture/2.jpg");
if (src
.empty())
{
printf("could not load image...\n");
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE
);
imshow("input", src
);
char output
[] = "output";
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);
namedWindow(output
, WINDOW_AUTOSIZE
);
int c
= 0;
int bordertype
= BORDER_DEFAULT
;
RNG
rng(12345);
while(true)
{
c
= waitKey(500);
if((char)c
== 27)
{
break;
}
else if ((char)c
== 'c')
{
bordertype
= BORDER_CONSTANT
;
}
else if((char)c
== 'r')
{
bordertype
= BORDER_REPLICATE
;
}
else if ((char)c
== 'w')
{
bordertype
= BORDER_WRAP
;
}
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
, dst
);
}
waitKey(0);
return 0;
}
边缘处理实例
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std
;
using namespace cv
;
int main()
{
Mat src
, dst
;
src
= imread("F:/Opencvlearn/picture/2.jpg");
if (src
.empty())
{
printf("could not load image...\n");
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE
);
imshow("input", src
);
GaussianBlur(src
, dst
, Size(3, 3), 0, 0, BORDER_REFLECT
);
imshow("output", dst
);
waitKey(0);
return 0;
}