OpenCV-学习历程2-矩阵掩膜操作(mask)+Kernal+filter2D+getTickCount (时间统计)

OPENCV系列博客主要记录自己学习OPENCV的历程,以及存储已经实现的代码,以备后续回顾使用,代码中包含了主要的备注。

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;

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

	//Step1 读取图片
	Mat src, dst;
	src = imread("sample.jpg");

	if (!src.data) {
			printf("Could not lod image \n");
		}

	namedWindow("input_image", CV_WINDOW_AUTOSIZE);
	imshow("input_image",src);

	//Step2 通过操作像素的方式来进行mask卷积
	/*
	int cols = src.cols * src.channels();
	int offsetx = src.channels();
	int rows = src.rows;

	dst = 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 = dst.ptr<uchar>(row);

		for (int col = offsetx; col < cols; col++) {
			output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
		}
	}
	*/

	//Step3 使用kernal和filter2D的方式来对照片进行滤波

	double t = getTickCount();										//开始执行时间

	Mat kernal = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);//掩膜矩阵3x3
	filter2D(src, dst, src.depth(), kernal);

	double timeconsume = (getTickCount() - t) / getTickFrequency(); //结束执行时间-开始执行时间=总共用时
	printf("Time consume %.2f\n", timeconsume);

	namedWindow("constrast image demo",CV_WINDOW_AUTOSIZE);
	imshow("constrast image demo",dst);

	waitKey();

}

效果如下,增加了对比度:

猜你喜欢

转载自blog.csdn.net/weixin_42503785/article/details/113903114