2021.3.25OpenCV03 matrix mask operation-function call filter2D function

//#include has a syntax error, pay attention to choose debug or release, x86 or x64
//03-Mask operation of matrix
/*
Function call filter2D function to
define the mask: Mat kernel = (Mat_<char>(3,3) < <0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D( src, dst, src.depth(), kernel ); where src and dst are Mat type variables, src. depth represents the depth of the bitmap, 32, 24, 8, etc.
*/

//#include出现语法错误,注意选择debug还是release,x86还是x64
//03-矩阵的掩膜操作
/*
函数调用filter2D功能
定义掩膜:Mat kernel = (Mat_<char>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D( src, dst, src.depth(), kernel );其中src与dst是Mat类型变量、src.depth表示位图深度,有32、24、8等
*/
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;

int main(int argc, char *argv[])
{
	Mat src, dst;
	//src = imread("E:\pictures\operation_bg.jpg");
	src = imread("E:/pictures/operation_bg.jpg");
	if (!src.data)//两种方式都可以
		//if(src.empty())
	{
		printf("不能加载图像");
		return -1;
	}
		namedWindow("pic",CV_WINDOW_AUTOSIZE);
		imshow("pic",src);


		//自行代码处理方法
		/*
		int cols = (src.cols-1)* src.channels();//实际col的长度=图像的col乘以图像的通道数。宽度
		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] = 5 * current[col] - (current[col-offsetx]+current[col+offsetx]+previous[col]+next[col]);//查看效果
				output[col] = saturate_cast<uchar>(5 * current[col] - (current[col-offsetx]+current[col+offsetx]+previous[col]+next[col]));//保持处理的RGB值在0-255,不失真。对比度提高
			}
		}
		*/
		//对比度提高的效果

		//API函数实现方法
		//获取执行时间
		double t = getTickCount();
		Mat kernel = (Mat_<char>(3,3)<<0,-1,0,-1,5,-1,0,-1,0);//掩膜
		filter2D(src,dst,src.depth(),kernel);//src.depth()表示与原图像深度一样,也可以用-1表示
		//filter2D(src, dst, -1, kernel);
		double timeconsume = (getTickCount() - t) / getTickFrequency();
		printf("time consume %.2f",timeconsume);

		namedWindow("对比度图像",CV_WINDOW_AUTOSIZE);
		imshow("对比度图像",dst);
		waitKey(0);
	return 0;
}

 

Guess you like

Origin blog.csdn.net/txwtech/article/details/115221214