opencv矩阵掩模

掩模公式: I(i,j)=5*I(i,j)-[I(i-1,j)+I(i+1,j)+I(i,j-1)+I(i,j+1)] 

Mat src, dst;
	src = imread("3.jpg");
	imshow("Normal",src);

	dst = Mat::zeros(src.size(), src.type());
	int cols = (src.cols - 1)*src.channels();
	int rows = src.rows;
	int offsetx = src.channels();

	//src.ptr(row)获取第row行的指针,下标从0开始;
	//saturate_cast(n)  限制n为0-255
	for (int row = 1; row < rows-1; row++)
	{
		uchar* previous = src.ptr(row-1);
		uchar* current = src.ptr(row);
		uchar* next = src.ptr(row + 1);
		uchar* output = dst.ptr(row);
		for (int col=offsetx; col(5 * current[col] - (previous[col] + next[col] + current[col - 1] + current[col + 1]));
		}
	}
	imshow("End", dst);

 opencv本身提供掩模函数:

Mat src, dst;
	src = imread("3.jpg");
	imshow("Normal",src);

	Mat  kernel = (Mat_(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
	int x = src.depth();
	filter2D(src, dst, src.depth(), kernel);
	imshow("End", dst);

opencv矩阵掩模_第1张图片

你可能感兴趣的:(opencv,C++)