- 一. 掩码操作简介
- 二. 我们的测试用例
二. 我们的测试用例矩阵的掩码操作非常的简单.这个想法是,我们根据掩码矩阵(也称为内核)重新计算图像中每个像素的值.此掩码保存的值将调整邻近像素(和当前像素)对新像素值的影响程度.从数学的观点来看,我们用我们指定的值做加权平均.
让我们考虑一下图像对比度增强方法的问题.基本上,我们想对图像的每个像素应用一下的公式:
第一种表示方法是使用公式,而第二种表示方法是使用掩码的压缩版本.你可以通过将掩码就很的中心(由0-0索引标记的大写)放到要计算的像素上,并将像素值乘以重叠矩阵值相加来使用掩码.这是一样的,但是在大矩阵的情况下后一种表示法更容易看.
代码:
#include#include #include #include 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 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.bmp"; Mat src, dst0, dst1; if (argc >= 3 && !strcmp("G", argv[2])) { src = imread(samples::findFile(filename), IMREAD_GRAYSCALE); } else { src = imread(samples::findFile(filename), IMREAD_COLOR); } if (src.empty()) { cerr << "Can't open image[" << filename << "]" << endl; return EXIT_FAILURE; } 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); Mat kernel = (Mat_ (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(0); return EXIT_SUCCESS; } void Sharpen(const Mat &myImage, Mat &Result) { // accept only uchar images CV_Assert(myImage.depth() == CV_8U); 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 (j - 1); const uchar *current = myImage.ptr (j); const uchar *next = myImage.ptr (j + 1); uchar *output = Result.ptr (j); for (int i = nChannels; i < nChannels * (myImage.cols - 1); i++) { *output++ = saturate_cast (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)); }
基本方法:
现在让我们看看如何使用基本像素访问方法或者是filter2D函数实现这一点.
这里是一个做到这一点的函数:
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(j - 1);
const uchar* current = myImage.ptr(j );
const uchar* next = myImage.ptr(j + 1);
uchar* output = Result.ptr(j);
for(int i= nChannels;i < nChannels*(myImage.cols-1); ++i)
{
*output++ = saturate_cast(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));
}
首先,我们确保输入的图像的数据是unsigined char格式的.为此,我们使用cv::CV_Assert函数,当它内部的表达式为false的时候抛出错误.
CV_Assert(myImage.depth() == CV_8U); // accept only uchar images
我们创建一个和输入相同大小和类型的输出图像.正如你再存储部分看到的那样,根据通道的数量,我们可能有一个子列或者多个子列.
我们将通过真真迭代他们,所以元素的总数取决于这个数字.
const int nChannels = myImage.channels(); Result.create(myImage.size(),myImage.type());
我们将使用普通的C[]操作符来访问像素.因为我们需要同时访问多行,所以我们将获取每一行的指针(前一行,当前行和下一行).我们需要另一个指针指向我们需要保存计算的地方.然后只需要使用[]操作符访问正确的项.为了向前移动输出指针,我们只需要再每次操作后增加这个(一个字节):
for(int j = 1 ; j < myImage.rows-1; ++j)
{
const uchar* previous = myImage.ptr(j - 1);
const uchar* current = myImage.ptr(j );
const uchar* next = myImage.ptr(j + 1);
uchar* output = Result.ptr(j);
for(int i= nChannels;i < nChannels*(myImage.cols-1); ++i)
{
*output++ = saturate_cast(5*current[i]
-current[i-nChannels] - current[i+nChannels] - previous[i] - next[i]);
}
}
在图形的边界上,上面的符号导致不存在的像素位置(如-1----1).这这些点上,公式没有定义.一个简单的解决方案是在这些点上不应用内核,例如,将边界上的像素设置为零:
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));
应用这样的过滤器在图像处理中是很常见的,在Opencv中有一个函数负责应用掩码(在某些地方也称为内核).为此,你首先需要定义一个保存掩码的对象:
Mat kernel = (Mat_(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
然后调用filter2D()函数,指定输入,输出图像和使用的内核:
filter2D( src, dst1, src.depth(), kernel );
该函数甚至还有第五个可选参数来指定内核的中心,第六个参数用来为结果额外的增加一个值,以及第七个参数来确定操作未定义的区域(边界)中该怎么填充.
这个函数更短,更简洁,而且由于进行了一些优化,它通常比手工的编码的方法更快.例如,在我的测试中,第二个只花了13毫秒,而第一个花了31毫秒.很大的区别.



