1 #include <opencv2/core.hpp>
2 #include <opencv2/core/utility.hpp>
3 #include <opencv2/highgui.hpp>
4 #include <opencv2/imgproc.hpp>
10 static void help(char* progName)
13 << "This program shows how to filter images with mask: the write it yourself and the"
14 << "filter2d way. " << endl
16 << progName << " [image_name -- default lena.jpg] [G -- grayscale] " << endl << endl;
20 void Sharpen(const Mat& myImage,Mat& Result);
22 int main( int argc, char* argv[])
25 const char* filename = argc >=2 ? argv[1] : "lena.jpg";
29 if (argc >= 3 && !strcmp("G", argv[2]))
30 I = imread( filename, IMREAD_GRAYSCALE);
32 I = imread( filename, IMREAD_COLOR);
34 namedWindow("Input", WINDOW_AUTOSIZE);
35 namedWindow("Output", WINDOW_AUTOSIZE);
38 double t = (double)getTickCount();
42 t = ((double)getTickCount() - t)/getTickFrequency();
43 cout << "Hand written function times passed in seconds: " << t << endl;
48 Mat kern = (Mat_<char>(3,3) << 0, -1, 0,
51 t = (double)getTickCount();
52 filter2D(I, K, I.depth(), kern );
53 t = ((double)getTickCount() - t)/getTickFrequency();
54 cout << "Built-in filter2D time passed in seconds: " << t << endl;
61 void Sharpen(const Mat& myImage,Mat& Result)
63 CV_Assert(myImage.depth() == CV_8U); // accept only uchar images
65 const int nChannels = myImage.channels();
66 Result.create(myImage.size(),myImage.type());
68 for(int j = 1 ; j < myImage.rows-1; ++j)
70 const uchar* previous = myImage.ptr<uchar>(j - 1);
71 const uchar* current = myImage.ptr<uchar>(j );
72 const uchar* next = myImage.ptr<uchar>(j + 1);
74 uchar* output = Result.ptr<uchar>(j);
76 for(int i= nChannels;i < nChannels*(myImage.cols-1); ++i)
78 *output++ = saturate_cast<uchar>(5*current[i]
79 -current[i-nChannels] - current[i+nChannels] - previous[i] - next[i]);
83 Result.row(0).setTo(Scalar(0));
84 Result.row(Result.rows-1).setTo(Scalar(0));
85 Result.col(0).setTo(Scalar(0));
86 Result.col(Result.cols-1).setTo(Scalar(0));