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