图像的掩膜操作

vs2017&opencv4.0.1

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include
using namespace cv;

int main(int argc, char** argv) {
	Mat src, dst;
	src = imread("D:\\time.jpg");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input image", WINDOW_AUTOSIZE);
	imshow("input image", src);
	/*//两种方法获取掩膜
	int cols = (src.cols - 1)*src.channels();//不太明白
	int offsetx = src.channels();//图像的通道
	int rows = src.rows;//图像的行数
	dst = Mat::zeros(src.size(), src.type());//定义一个与src大小和类型相同的0矩阵
	//读取掩膜
	for (int row = 1; row < (row - 1); row++) {
		const uchar* previous = src.ptr(row - 1);
		const uchar* current = src.ptr(row);
		const uchar* next = src.ptr(row + 1);
		uchar* output = dst.ptr(row);
		for (int col = offsetx; col < cols; col++  ) {
			output[col] =saturate_cast(5 * current[col]-(current[col-offsetx]+current[col+offsetx]+previous[col]+next[col]));
		}
	}
	*/
	
	double t = getTickCount();
	Mat kernel = (Mat_(3, 3) << 0, -1, 0,- 1, 5, -1,0, -1, 0);//定义掩膜
	filter2D(src, dst, src.depth(), kernel);//filter2D( src, dst, src.depth(), kernel );其中src与dst是Mat类型变量、src.depth表示位图深度
	double timecosume = (getTickCount() - t) / getTickFrequency();//计算时间

	namedWindow("contrast image demo",WINDOW_AUTOSIZE);
	imshow("contrast image demo", dst);

	waitKey(0);
	return 0;
}

你可能感兴趣的:(图像的掩膜操作)