Mask operations 翻译为中文应该是掩模操作,具体操作步骤就是根据一个操作矩阵(又名kernel)处理图片中的每一个像素点,操作矩阵会根据当前像素点的周围像素来调整当前像素值。

1.示例

以一个增强图片对比度的示例来说明Mask operations。增强对比度的公式如下:

公式左右两边都能实现同样的操作,但是右边矩阵形式看起来更加好看些。

2.测试代码

本文分别使用两种方法实现上述公式:

2.1 实现公式左边

void Sharp(const Mat& Src,Mat& Dst)
{
//只能处理8位图片
CV_Assert(Src.depth() == CV_8U);
Dst.create(Src.size(),Src.type()); const int nChannel = Src.channels(); //遍历行
for (int j = 1; j < Src.rows - 1; j++)
{
//前一行 当前行 以及下一行的指针
const uchar* pre = Src.ptr<uchar>(j - 1);
const uchar* cur = Src.ptr<uchar>(j);
const uchar* next = Src.ptr<uchar>(j + 1); //获取结果图像所在行的指针
uchar* output = Dst.ptr<uchar>(j);
for (int i = nChannel; i < nChannel*(Src.cols - 1); i++)
{
//使用公式计算后将值保存到输出矩阵中
*output++ = saturate_cast<uchar>(5*cur[i]
- cur[i-nChannel] - cur[i+nChannel] - pre[i] - next[i]);
}
} Dst.row(0).setTo(Scalar(0));
Dst.row(Dst.rows - 1).setTo(Scalar(0));
Dst.col(0).setTo(Scalar(0));
Dst.col(Dst.cols - 1).setTo(Scalar(0));
}

2.2 OpenCV 自带函数filter2D

void OpenCV_Sharp(const Mat& Src,Mat& Dst)
{
//初始化"内核"矩阵
Mat Kern = (Mat_<char>(3,3)<<
0,-1,0,
-1,5,-1,
0,-1,0);
filter2D(Src,Dst,Src.depth(),Kern);
}

3.结果对比

使用Lena图片对两个函数进行测试:

测试结果如下:

原图:

公式实现结果:

OpenCV操作结果

4.结束

最新文章

  1. Android 轮换控件
  2. canvas 图片拖拽旋转之二——canvas状态保存(save和restore)
  3. [设计模式] javascript 之 组合模式
  4. SU Demos-02Filtering-07Sumedian
  5. Lazy Load, 延迟加载图片的 jQuery 插件【备忘】
  6. LightOj1007 - Mathematically Hard(欧拉函数)
  7. Actionform
  8. Spring MVC 入门教程示例 (一)
  9. C++中string类型对象和double型变量之间的互相转换
  10. C# inherit
  11. jsp传到java的control层的方法
  12. 【Android Developers Training】 47. 序言:拍摄照片
  13. 报错TypeError: $(...).live is not a function解决方法
  14. 嵌入式linux——点亮led灯(二)
  15. 防止xss攻击。
  16. goreplay,tcpcopy
  17. [leetcode]428. Serialize and Deserialize N-ary Tree序列化与反序列化N叉树
  18. Linux那些事儿之我是Hub(大结局)挂起自动化【转】
  19. 005-四种常见的 POST 提交数据方式
  20. codeforces 461C

热门文章

  1. 【[ZJOI2010]网络扩容】
  2. 【luoguP1563】【2016NOIP-High】玩具迷题
  3. Java虚拟机垃圾回收(二) :垃圾回收算法(转载)
  4. OGG抽取进程异常问题排查一例
  5. SpringBoot学习16:springboot整合junit单元测试
  6. XPath知识点简单总结(思维导图)
  7. Lavavel5.5源代码 - Pipeline
  8. mac phpstorm 破解方法
  9. Linux基础(02)、MTPutty安装和使用
  10. linux安装python并安装pip