OpenCV学习笔记(6):滤波filter2D()

#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;
void Sharpen(const Mat&src, Mat & result)
{
	CV_Assert(src.depth() == CV_8U);
	const int channels = src.channels();
	result.create(src.size(), src.type());

	for (int i = 1; i < src.rows - 1; i++)
	{
		const uchar*previous = src.ptr<uchar>(i - 1);
		const uchar*current = src.ptr<uchar>(i);
		const uchar*next = src.ptr<uchar>(i + 1);

		 uchar *out = result.ptr<uchar>(i);
		for (int j = channels; j < channels*(src.cols - 1); j++)
		{
			*out++ = saturate_cast<uchar>(5 * current[j] - current[j - channels] - current[j + channels] -
				previous[j] - next[j]);
		}
	}
	result.row(0).setTo(Scalar(0));
	result.row(result.rows - 1).setTo(Scalar(0));
	result.col(0).setTo(Scalar(0));
	result.col(result.cols - 1).setTo(Scalar(0));
}
int main(int argc, char* argv[])
{
	Mat I, J,K;
	const char * filename = argc >= 2 ? argv[1] : "lena.png";
	if (argc >= 3 && !strcmp(argv[2], "G"))
		I=imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
	else
		I=imread(filename, CV_LOAD_IMAGE_COLOR);
	if (!I.data)
		cout << "cannot load image!" << endl;

	const char * window1 = "src";
	const char * window2 = "result";
	namedWindow(window1, WINDOW_AUTOSIZE);
	namedWindow(window2, WINDOW_AUTOSIZE);
	imshow(window1, I);
	double time = getTickCount();
	Sharpen(I, J);
	time = ((double)getTickCount() - time) / getTickFrequency();
	cout << "time is:" << time << endl;
	imshow(window2, J);
	waitKey(0);


	Mat kernl = (Mat_<char>(3, 3) << 0, -1, 0,
					-1, 5, -1,
					0, -1, 0);
	time = (double)getTickCount();
	filter2D(I, K, I.depth(), kernl);
	time = ((double)getTickCount() - time) / getTickFrequency();
	cout << "time is:" << time << endl;
	imshow(window2, K);
	waitKey(0);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值