OpenCV 图像对比度的实践
作者:翟天保Steven 发布时间:2023-07-29 09:09:27
标签:OpenCV,图像对比度
本文主要介绍了OpenCV 图像对比度,具有一定的参考价值,感兴趣的可以了解一下
实现原理
图像对比度指的是一幅图像中明暗区域最亮的白和最暗的黑之间不同亮度层级的测量,即指一幅图像灰度反差的大小。差异范围越大代表对比越大,差异范围越小代表对比越小。设置一个基准值thresh,当percent大于0时,需要令图像中的颜色对比更强烈,即数值距离thresh越远,则变化越大;当percent等于1时,对比强到极致,只有255和0的区分;当percent等于0时,不变;当percent小于0时,对比下降,即令远离thresh的数值更近些;当percent等于-1时,没有对比了,全是thresh值。
对比度调整算法的实现流程如下:
1.设置调整参数percent,取值为-100到100,类似PS中设置,归一化后为-1到1。
2.针对图像所有像素点单个处理。当percent大于等于0时,对比增强,调整后的RGB三通道数值为:
3.若percent小于0时,对比降低,此时调整后的图像RGB三通道值为:
4.若percent等于1时,大于thresh则等于255,小于则等于0。
至此,图像实现了明度的调整,算法逻辑参考xingyanxiao。C++实现代码如下。
功能函数代码
// 对比度
cv::Mat Contrast(cv::Mat src, int percent)
{
float alpha = percent / 100.f;
alpha = max(-1.f, min(1.f, alpha));
cv::Mat temp = src.clone();
int row = src.rows;
int col = src.cols;
int thresh = 127;
for (int i = 0; i < row; ++i)
{
uchar *t = temp.ptr<uchar>(i);
uchar *s = src.ptr<uchar>(i);
for (int j = 0; j < col; ++j)
{
uchar b = s[3 * j];
uchar g = s[3 * j + 1];
uchar r = s[3 * j + 2];
int newb, newg, newr;
if (alpha == 1)
{
t[3 * j + 2] = r > thresh ? 255 : 0;
t[3 * j + 1] = g > thresh ? 255 : 0;
t[3 * j] = b > thresh ? 255 : 0;
continue;
}
else if (alpha >= 0)
{
newr = static_cast<int>(thresh + (r - thresh) / (1 - alpha));
newg = static_cast<int>(thresh + (g - thresh) / (1 - alpha));
newb = static_cast<int>(thresh + (b - thresh) / (1 - alpha));
}
else {
newr = static_cast<int>(thresh + (r - thresh) * (1 + alpha));
newg = static_cast<int>(thresh + (g - thresh) * (1 + alpha));
newb = static_cast<int>(thresh + (b - thresh) * (1 + alpha));
}
newr = max(0, min(255, newr));
newg = max(0, min(255, newg));
newb = max(0, min(255, newb));
t[3 * j + 2] = static_cast<uchar>(newr);
t[3 * j + 1] = static_cast<uchar>(newg);
t[3 * j] = static_cast<uchar>(newb);
}
}
return temp;
}
C++测试代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
cv::Mat Contrast(cv::Mat src, int percent);
int main()
{
cv::Mat src = imread("5.jpg");
cv::Mat result = Contrast(src, 50.f);
imshow("original", src);
imshow("result", result);
waitKey(0);
return 0;
}
// 对比度
cv::Mat Contrast(cv::Mat src, int percent)
{
float alpha = percent / 100.f;
alpha = max(-1.f, min(1.f, alpha));
cv::Mat temp = src.clone();
int row = src.rows;
int col = src.cols;
int thresh = 127;
for (int i = 0; i < row; ++i)
{
uchar *t = temp.ptr<uchar>(i);
uchar *s = src.ptr<uchar>(i);
for (int j = 0; j < col; ++j)
{
uchar b = s[3 * j];
uchar g = s[3 * j + 1];
uchar r = s[3 * j + 2];
int newb, newg, newr;
if (alpha == 1)
{
t[3 * j + 2] = r > thresh ? 255 : 0;
t[3 * j + 1] = g > thresh ? 255 : 0;
t[3 * j] = b > thresh ? 255 : 0;
continue;
}
else if (alpha >= 0)
{
newr = static_cast<int>(thresh + (r - thresh) / (1 - alpha));
newg = static_cast<int>(thresh + (g - thresh) / (1 - alpha));
newb = static_cast<int>(thresh + (b - thresh) / (1 - alpha));
}
else {
newr = static_cast<int>(thresh + (r - thresh) * (1 + alpha));
newg = static_cast<int>(thresh + (g - thresh) * (1 + alpha));
newb = static_cast<int>(thresh + (b - thresh) * (1 + alpha));
}
newr = max(0, min(255, newr));
newg = max(0, min(255, newg));
newb = max(0, min(255, newb));
t[3 * j + 2] = static_cast<uchar>(newr);
t[3 * j + 1] = static_cast<uchar>(newg);
t[3 * j] = static_cast<uchar>(newb);
}
}
return temp;
}
测试效果
图1 原图
图2 参数为50的效果图
图3 参数为-50的效果图
通过调整percent可以实现图像对比度的调整。
来源:https://blog.csdn.net/zhaitianbao/article/details/120107171
0
投稿
猜你喜欢
- Python 爬虫之超链接 url中含有中文出错及解决办法python3.5 爬虫错误:UnicodeEncodeError: 'a
- virtualenv简介在开发Python应用程序的时候,我们的系统上通常只会安装一个Python版本:例如 3.7。所有使用 pip 安装
- 类 型描 述EmptyVariable 没有被初始化,它是数字的话,它的值就为0,如果它是字符串,那么它的值就为1N
- 我就废话不多说了,大家还是直接看代码吧!database = [ { "name"
- 单例模式概念单例模式:“保证一个类仅有一个实例,并提供一个访问它的在这里插入代码片全局访问点。单例模式会阻止其他对象实例化其自己的单例对象的
- 使用cookie来判断来访者身份,是否是首次登陆, asp代码实例如下:< %@ LANGUAGE=&q
- 0x01 生成shellcode首先通过下列命令生成一个shellcode,使用msfvenom -p选项来指定paylaod,这里选用wi
- 客户端调用XMLHTTP的过程很简单,只有5个步骤: 1、创建XMLHTTP对象 2、打开与服务端的连接,同时定义指令发送方式,服务网页(U
- 前言一首歌热门了,参与评论的人也很多,这时无论好坏评论都来了,没有人控评得话,指不定乱七八糟但是自己有喜欢看评论,不想影响好心情,想看看精彩
- JavaScript: <script type="text/javascript"> var level1
- 一、安装cuda1、在英伟达官网下载最新版的cuda驱动https://developer.nvidia.com/zh-cn/cuda-do
- 首先需要安装Win32-ODBC模块,具体的步骤如下:1:从TOOLS栏目中下载Win32-ODBC.zip,下载完后用winzip解开到一
- 有时候,预先不知道函数需要接受多少个实参,好在Python允许函数从调用语句中调用语句中收集任意数量的实参。在参数前加上*号。来看一个制作披
- 实例如下:from win32com.client import Dispatch import win32com.client
- 本文实例讲述了Python Tensor FLow简单使用方法。分享给大家供大家参考,具体如下:1、基础概念Tensor表示张量,是一种多维
- 这年头,信息和获得信息的渠道越来越多。随着信息量的增大,先有了分类,又有了导航,再有了搜索,后面的发展还不得而知。在此只是根据平日的所看所想
- Server对象提供对服务器上访问的方法和属性.大多数方法和属性是作为实用程序的功能提供的。语法:Server.property|metho
- 本篇文章记录如何使用python将pdf文件切分成一张一张图片,包括环境配置、版本兼容问题。环境配置(mac)安装ImageMagickbr
- 首先,这片文章纯粹是我的个人经验之谈,适用于我常见的环境及项目中。个人建议,数据库字符集尽量使用utf8(HTML页面对应的是utf-8),
- 近期有个需要进行音频转码的小任务需要用到ffmpeg,安装和使用的过程中遇到了很多问题没有办法解决,从网上找了各种教程也是一言难近,本文二哥