矩阵卷积之原矩阵内部计算

目录

note

code

test


note

矩阵卷积之原矩阵内部计算_第1张图片

 

code

// 不扩充边缘的矩阵卷积,导致输出矩阵比输入矩阵小
void MyMatConvolute1(Mat& src, Mat& kernel, Mat& res) {
	if (src.type() != CV_8UC1) {
		return;
	}
	if ((kernel.type() != CV_8UC1) || (kernel.rows != kernel.cols) || (kernel.rows / 2 == 0)) {
		return;
	}
	int left = kernel.rows / 2;
	res = Mat(src.rows-2*left, src.cols-2*left, CV_8UC1);
	int res_row = 0, res_col = 0;
	for (int i = 0; i <= src.rows-kernel.rows; ++i) {
		for (int j = 0; j <= src.cols-kernel.cols; ++j) {
			uchar out = 0;
			for (int p = 0; p < kernel.rows; ++p) {
				for (int q = 0; q < kernel.cols; ++q) {
					out += src.at(i+p,j+q) * kernel.at(p,q);
				}
			}
			res.at(res_row,res_col) = out;
			++res_col;
			if (res_col >= res.cols) {
				res_col = 0;
				++res_row;
			}
		}
	}
}

test

矩阵卷积之原矩阵内部计算_第2张图片

 

你可能感兴趣的:(图像处理,矩阵,算法,opencv,c++)