010-OpenCV4(C++)图像卷积blur,卷积边缘处理即边缘填充:copyMakeBorder

一、卷积API:blur:

blur(输入,输出,卷积核大小,锚定,边缘填充)

其中卷积核格式Size( ,)

锚定默认中心点Point(-1,-1)

根据卷积原理手动写3X3卷积

边缘填充默认BORDER_DEFAULT

代码演示:

Mat dst;
blur(src, dst, Size(3, 3), Point(-1, -1),BORDER_DEFAULT);
imshow("dst", dst);

二、卷积边缘处理

边缘填充:copyMakeBorder(输入,输出,上,下,左,右,填充类型,填充颜色)

填充类型 :

010-OpenCV4(C++)图像卷积blur,卷积边缘处理即边缘填充:copyMakeBorder_第1张图片

代码演示:

int border = 8;
Mat bord_m;
copyMakeBorder(src, bord_m, border, border, border, border,             
BORDER_CONSTANT,Scalar(255,0,0));
imshow("bord_m", bord_m);

三、根据卷积原理手敲卷积代码: 

#include
#include

using namespace cv;
using namespace std;

int main(int argc, char**argv) {

	Mat src = imread("D:/images/flower.png");
	if (src.empty()) {
		printf("Could not read image");
		return -1;
	}
	int h = src.rows;
	int w = src.cols;
	Mat result = src.clone();

	for (int row = 1; row < h - 1; row++) {
		for (int col = 1; col < w - 1; col++) {
			int sb = src.at(row - 1, col - 1)[0] + src.at(row - 1, col)[0] + src.at(row - 1, col + 1)[0] +
				src.at(row, col - 1)[0] + src.at(row, col)[0] + src.at(row, col + 1)[0] +
				src.at(row + 1, col - 1)[0] + src.at(row + 1, col)[0] + src.at(row + 1, col + 1)[0];
			
			int sg = src.at(row - 1, col - 1)[1] + src.at(row - 1, col)[1] + src.at(row - 1, col + 1)[1] +
				src.at(row, col - 1)[1] + src.at(row, col)[1] + src.at(row, col + 1)[1] +
				src.at(row + 1, col - 1)[1] + src.at(row + 1, col)[1] + src.at(row + 1, col + 1)[1];

			int sr = src.at(row - 1, col - 1)[2] + src.at(row - 1, col)[2] + src.at(row - 1, col + 1)[2] +
				src.at(row, col - 1)[2] + src.at(row, col)[2] + src.at(row, col + 1)[2] +
				src.at(row + 1, col - 1)[2] + src.at(row + 1, col)[2] + src.at(row + 1, col + 1)[2];
			
			result.at(row, col)[0] = sb / 9;
			result.at(row, col)[1] = sg / 9;
			result.at(row, col)[2] = sr / 9;
		}
	}

	
	imshow("input", src);
	imshow("result", result);

	waitKey(0);
	destroyAllWindows;
	return 0;
}

你可能感兴趣的:(OpenCV初级,opencv,c++)