mat_mask_operations

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <sstream>

using namespace std;
using namespace cv;


/************************************************************************
*   Hand written function                                                                  
/************************************************************************/
void Sharpen1(Mat& image, Mat& result) {
	
	// Only accept uchar type image
	CV_Assert(image.depth() == CV_8U);

	int nchannels = image.channels();
	int nRows = image.rows;
	int nCols = image.cols;

	result.create(image.size(), image.type());

	for (int j = 1; j < nRows-1; ++j) {

		const uchar* previous = image.ptr<uchar>(j-1);
		const uchar* current  = image.ptr<uchar>(j);
		const uchar* next	  = image.ptr<uchar>(j+1);

		uchar* outPut = result.ptr<uchar>(j);

		for (int i = nchannels; i < nchannels * (nCols-1); ++i) {

			*outPut++ = saturate_cast<uchar>(5 * current[i] - current[i-nchannels] 
			             - current[i+nchannels] - previous[i] - next[i] );
		}
	}

	result.row(0).setTo(Scalar(0));
	result.row(nRows - 1).setTo(Scalar(0));
	result.col(0).setTo(Scalar(0));
	result.col(nCols - 1).setTo(Scalar(0));
}

/************************************************************************
*   Built-in filter2D                                                                  
/************************************************************************/
void Sharpen2(const Mat& image, Mat& result) {

	// mask
	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0,
		                              -1, 5, -1,
									  0, -1, 0);

	filter2D(image, result, image.depth(), kernel);

}

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

	Mat imgInput, imgOut_1, imgOut_2;

	const char* filename = "tiger.jpg";
	imgInput = imread(filename, CV_LOAD_IMAGE_COLOR);
	imshow("Input", imgInput);


	// Hand written function----------------------------------------------------------------------
	double time = (double)getTickCount();
	Sharpen1(imgInput, imgOut_1);
	time = ((double)getTickCount() - time) / getTickFrequency();

    cout << "Hand written function times passed in seconds: " << time << endl;
 	imshow("Output_1", imgOut_1);



	// Built-in filter2D -------------------------------------------------------------------------
	time = (double)getTickCount();
	Sharpen2(imgInput, imgOut_2);
	time = ((double)getTickCount() - time) / getTickFrequency();

    cout << "Built-in filter2D time passed in seconds:      " << time << endl;
 	imshow("Output_2 ", imgOut_2);


    waitKey(0);

	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值