矩阵的掩模操作(通常也叫做卷积操作)非常简单。本文的中心思想是基于掩模矩阵(也称为内核或者卷积核)重新计算图像每个像素的值。此掩模矩阵(卷积核)的值定义了当前像素和相邻像素对新像素值进行影响的值。From a mathematical point of view we make a weighted average, with our specified values.(从数学的角度来看,基于掩模矩阵指定的值进行mask操作后再进行加权平均)。具体(实现锐化效果的)描述如下:
第一个是公式的形式; 第二个是基于掩模矩阵的版本。两个等价。
实现锐化效果的例子代码如下:
#include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> using namespace std; using namespace cv; static void help(char* progName) { cout << endl << "This program shows how to filter images with mask: the write it yourself and the" << "filter2d way. " << endl << "Usage:" << endl << progName << " [image_name -- default lena.jpg] [G -- grayscale] " << endl << endl; } void Sharpen(const Mat& myImage,Mat& Result); int main( int argc, char* argv[]) { help(argv[0]); const char* filename = argc >=2 ? argv[1] : "lena.jpg"; Mat src, dst0, dst1; if (argc >= 3 && !strcmp("G", argv[2])) src = imread( filename, IMREAD_GRAYSCALE); else src = imread( filename, IMREAD_COLOR); if (src.empty()) { cerr << "Can't open image [" << filename << "]" << endl; return -1; } namedWindow("Input", WINDOW_AUTOSIZE); namedWindow("Output", WINDOW_AUTOSIZE); imshow( "Input", src ); double t = (double)getTickCount(); Sharpen( src, dst0 ); t = ((double)getTickCount() - t)/getTickFrequency(); cout << "Hand written function times passed in seconds: " << t << endl; imshow( "Output", dst0 ); waitKey(); //掩模矩阵定义初始化,用于filter2D函数 Mat kernel = (Mat_<char>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); t = (double)getTickCount(); filter2D( src, dst1, src.depth(), kernel ); t = ((double)getTickCount() - t)/getTickFrequency(); cout << "Built-in filter2D time passed in seconds: " << t << endl; imshow( "Output", dst1 ); waitKey(); return 0; } //基于公式计算的版本 void Sharpen(const Mat& myImage,Mat& Result)//公式计算方式 { CV_Assert(myImage.depth() == CV_8U); // accept only uchar images const int nChannels = myImage.channels(); Result.create(myImage.size(),myImage.type()); for(int j = 1 ; j < myImage.rows-1; ++j) { const uchar* previous = myImage.ptr<uchar>(j - 1); const uchar* current = myImage.ptr<uchar>(j ); const uchar* next = myImage.ptr<uchar>(j + 1); uchar* output = Result.ptr<uchar>(j); for(int i= nChannels;i < nChannels*(myImage.cols-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(Result.rows-1).setTo(Scalar(0)); Result.col(0).setTo(Scalar(0)); Result.col(Result.cols-1).setTo(Scalar(0)); }