#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;
}