当前位置:网站首页>Chapter_03 矩阵的掩膜操作
Chapter_03 矩阵的掩膜操作
2022-06-09 06:25:00 【Fioman_Hammer】
一. 掩码操作简介
矩阵的掩码操作非常的简单.这个想法是,我们根据掩码矩阵(也称为内核)重新计算图像中每个像素的值.此掩码保存的值将调整邻近像素(和当前像素)对新像素值的影响程度.从数学的观点来看,我们用我们指定的值做加权平均.
二. 我们的测试用例
让我们考虑一下图像对比度增强方法的问题.基本上,我们想对图像的每个像素应用一下的公式:

第一种表示方法是使用公式,而第二种表示方法是使用掩码的压缩版本.你可以通过将掩码就很的中心(由0-0索引标记的大写)放到要计算的像素上,并将像素值乘以重叠矩阵值相加来使用掩码.这是一样的,但是在大矩阵的情况下后一种表示法更容易看.
代码:
#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 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_<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(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<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));
}
基本方法:
现在让我们看看如何使用基本像素访问方法或者是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<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));
}
首先,我们确保输入的图像的数据是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<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]);
}
}
在图形的边界上,上面的符号导致不存在的像素位置(如-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_<char>(3,3) << 0, -1, 0,
-1, 5, -1,
0, -1, 0);
然后调用filter2D()函数,指定输入,输出图像和使用的内核:
filter2D( src, dst1, src.depth(), kernel );
该函数甚至还有第五个可选参数来指定内核的中心,第六个参数用来为结果额外的增加一个值,以及第七个参数来确定操作未定义的区域(边界)中该怎么填充.
这个函数更短,更简洁,而且由于进行了一些优化,它通常比手工的编码的方法更快.例如,在我的测试中,第二个只花了13毫秒,而第一个花了31毫秒.很大的区别.
边栏推荐
- Sudo: GEDIT: command not found
- Bladed v4.3 installation (Pojie) process
- zgc gc消息类型及触发时机
- error: subprocess-exited-with-error(fasttext)
- 工业级AM335X核心模块选型
- adam 神经网络
- Avez - vous vraiment compris l'entropie (y compris l'entropie croisée)
- Encounter nodejs
- ImportError: cannot import name ‘joblib‘ from ‘sklearn.externals‘
- [early spring 2022] [leetcode] 91 Decoding method
猜你喜欢

基於國產全志A40I的機器人示教器解决方案

Binary tree

Introduction to bladed fault simulation method

二叉树的递归套路

error: subprocess-exited-with-error(fasttext)

Comparison between the most cost-effective processor and domestic processor i.mx6ul/a40i/t3

无需剪辑软件,教你简单快速进行剪辑视频

Transplant qt5.12 to t507 development board

全志V3s学习记录(9)buildroot文件系统构建

MT2712 SOC DMIPS
随机推荐
Logical judgment of four beam lidar wind measurement system
Bitmake common command parameters
Two integers compare sizes. Why is 100 equal to 1001000 not equal to 1000?
yum 命令Error: rpmdb open failed
全志平台BSP裁剪(1)kernel裁剪--调试工具和调试信息的裁剪
Bladed software display incomplete solution
Svn account password search
全志平台BSP裁剪(3)附件二 Kernel hacking配置说明
CodeBlocks project window management
Talk about bladed software
Allocate stall principle of ZGC
Binary tree
全志V3s学习记录(11)音频、视频使用总结
Yocto compiling libdrm
AM335X与全志A40i大比拼
MT2712 Boot Flow Introduction
sparksql处理数据倾斜问题常见思路
两个Integer比较大小,为什么100等于100,1000不等于1000?
Exponential moving weighted average
基於國產全志A40I的機器人示教器解决方案