OpenCV基础课程笔记03矩阵掩模操作

写在前面

//upDBD = Mat::zeros(src.size(), src.type());
返回一个置零的Mat对象
//upDBD = Mat::eye(src.size(), src.type());
返回一个对角线为白色,其他为黑色的Mat对象
if(!mat.data) 如果无图被读入 (因为我懒,所以所有课程笔记都没有这个消除鲁棒性的代码哈哈哈)
//output[col] = saturate_cast(5 * current[col] - (current[col - offsets] + current[col + offsets] + previous[col] + next[col]));
确保像素值有效的函数

调用API的内容:
Mat kernel = (Mat_(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);//掩模
filter2D(yuantu, jieguo, yuantu.depth(), kernel);

一般掩模操作在基础里面用于锐化、提高对比度。

代码

#include <iostream>  
#include<opencv2\opencv.hpp>   

using namespace cv;
using namespace std;


int main() {
	//Mat src = imread("A:专用\\TestForTheCV\\女友.jpg");
	//imshow("原图", src);

	手写板
	//Mat upDBD;
	//int cols = (src.cols - 1)*src.channels();
	//int offsets = src.channels();
	//int rows = src.rows;
	//upDBD = Mat::zeros(src.size(), src.type());
	//for (int row = 1; row < (rows - 1); row++) {
	//	const uchar* previous = src.ptr<uchar>(row - 1);
	//	const uchar* current = src.ptr<uchar>(row);
	//	const uchar* next = src.ptr<uchar>(row + 1);
	//	uchar* output = upDBD.ptr<uchar>(row);
	//	for (int col = offsets; col < cols; col++) {
	//		output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsets] + current[col + offsets] + previous[col] + next[col]));
	//	}

	//}

	//imshow("提高对比度", upDBD);

	//waitKey(0);


	Mat yuantu = imread("A:\\专用\\TestForTheCV\\女友2.jpg");
	Mat jieguo;
	imshow("原图", yuantu);
	//CV的API 

	double t = getTickCount();
	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);//掩模
	filter2D(yuantu, jieguo, yuantu.depth(), kernel);
	double timeconsume = (getTickCount() - t) / getTickFrequency();
	cout << timeconsume;

	imshow("结果",jieguo);

	waitKey(0);

	return 0;

}

结果

在这里插入图片描述

posted @ 2020-08-26 21:52  爱和九九  阅读(50)  评论(0)    收藏  举报