从头开始opencv(四)——core:Mask operations on matrices
Mask operations on matrices are quite simple. The idea is that we recalculate each pixels value in an image according to a mask matrix (also known as kernel). This mask holds values that will adjust how much influence neighboring pixels (and the current pixel) have on the new pixel value. From a mathematical point of view we make a weighted average, with our specified values.
对矩阵进行mask operations操作非常简单。核心思想:我们根据mask matrix(或者说kernel)来对图像中的每一个像素点进行新值的计算。mask中保存的值(根据他们对相邻和当前像素的影响大小来决定这些矩阵中的值)。
our test case
在本篇文章中,我们讨论图像对比度增强的方法。
#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_path -- default ../data/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] : "D:/0PKU/opencv/test.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 time passed in seconds: " << t << endl;
imshow("Output", dst0);
waitKey();
//![kern]
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0,
-1, 5, -1,
0, -1, 0);
//![kern]
t = (double)getTickCount();
//![filter2D]
filter2D(src, dst1, src.depth(), kernel);
//![filter2D]
t = ((double)getTickCount() - t) / getTickFrequency();
cout << "Built-in filter2D time passed in seconds: " << t << endl;
imshow("Output", dst1);
waitKey();
return 0;
}
//! [basic_method]
void Sharpen(const Mat& myImage, Mat& Result)
{
//! [8_bit]
CV_Assert(myImage.depth() == CV_8U); // accept only uchar images
//! [8_bit]
//! [create_channels]
const int nChannels = myImage.channels();
Result.create(myImage.size(), myImage.type());
//! [create_channels]
//! [basic_method_loop]
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]);
}
}
//! [basic_method_loop]
//! [borders]
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));
//! [borders]
}
//! [basic_method]
The Basic Method
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));
}
可以发现,这种方法比普通的C++方法方便了很多。
The filter2D function
Mat kernel = (Mat_<char>(3,3) << 0, -1, 0,
-1, 5, -1,
0, -1, 0);
filter2D( src, dst1, src.depth(), kernel );
好用极了!
定义:
void cv::filter2D( InputArray _src, OutputArray _dst, int ddepth,
InputArray _kernel, Point anchor0,
double delta, int borderType )
(路径:opencv-3.4.1/modules/imgproc/filter.cpp
)
函数原型:
void cv::filter2D( InputArray _src, OutputArray _dst, int ddepth,
InputArray _kernel, Point anchor0,
double delta, int borderType )
{
//opencv的profiling,可以在内部追踪函数执行状况,默认情况下是关闭的,不会产生性能开销.
CV_INSTRUMENT_REGION()
//若平台支持OpenCL则使用OpenCL执行
CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2,
ocl_filter2D(_src, _dst, ddepth, _kernel, anchor0, delta, borderType))
//将传入的参数转换为Mat结构
Mat src = _src.getMat(), kernel = _kernel.getMat();
//在api说明中知道调用cv::filter2D是允许将ddepth设置小于0的
if( ddepth < 0 )
ddepth = src.depth();
//为函数输出创建Mat结构
_dst.create( src.size(), CV_MAKETYPE(ddepth, src.channels()) );
Mat dst = _dst.getMat();
Point anchor = normalizeAnchor(anchor0, kernel.size());
//计算src在进行了边界填充后的数组中的位置
Point ofs;
Size wsz(src.cols, src.rows);
if( (borderType & BORDER_ISOLATED) == 0 )
src.locateROI( wsz, ofs );
//调用hal命名空间下的filter2D函数
hal::filter2D(src.type(), dst.type(), kernel.type(),
src.data, src.step, dst.data, dst.step,
dst.cols, dst.rows, wsz.width, wsz.height, ofs.x, ofs.y,
kernel.data, kernel.step, kernel.cols, kernel.rows,
anchor.x, anchor.y,
delta, borderType, src.isSubmatrix());
}
从cv::filter2D
代码的组织形式看,opencv是优先使用opencl的,可以推测,opencv中opencl的实现性能是比较优秀的(整体来说,源码没太看懂,涉及到知识盲区了)。