OpenCV在图像对比度的示例分析
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍了OpenCV在图像对比度的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。实现原理图像对比度指的是一幅图像中明
千家信息网最后更新 2026年11月02日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(i); uchar *s = src.ptr(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(thresh + (r - thresh) / (1 - alpha)); newg = static_cast(thresh + (g - thresh) / (1 - alpha)); newb = static_cast(thresh + (b - thresh) / (1 - alpha)); } else { newr = static_cast(thresh + (r - thresh) * (1 + alpha)); newg = static_cast(thresh + (g - thresh) * (1 + alpha)); newb = static_cast(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(newr); t[3 * j + 1] = static_cast(newg); t[3 * j] = static_cast(newb); } } return temp;} C++测试代码
#include#include 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 (i); uchar *s = src.ptr (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 (thresh + (r - thresh) / (1 - alpha)); newg = static_cast (thresh + (g - thresh) / (1 - alpha)); newb = static_cast (thresh + (b - thresh) / (1 - alpha)); } else { newr = static_cast (thresh + (r - thresh) * (1 + alpha)); newg = static_cast (thresh + (g - thresh) * (1 + alpha)); newb = static_cast (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 (newr); t[3 * j + 1] = static_cast (newg); t[3 * j] = static_cast (newb); } } return temp;}
测试效果
图1 原图
图2 参数为50的效果图
图3 参数为-50的效果图
通过调整percent可以实现图像对比度的调整。
感谢你能够认真阅读完这篇文章,希望小编分享的"OpenCV在图像对比度的示例分析"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
图像
对比度
调整
篇文章
代码
参数
效果
效果图
数值
示例
分析
代表
差异
算法
范围
通道
C++
参考
测试
不同
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
金融科技还是互联网
深圳科技互联网
为想互联网科技
互联网科技论文
奇酷互联网络科技公司
北京禾田雨橡互联网科技
互联网科技英文翻译
甘肃拓世互联网科技有限公司
衡水科技产业互联网平台
中国科技互联网公司收入
阿里互联网科技公司
广州恒生互联网科技有限
佳云科技和互联网广告
互联网与医疗科技
晋城互联网科技
工业互联网山东科技大学
怎样加入科技互联网
互联网风格科技公司名称
萌小萌互联网科技河南有限公司
互联网科技博主张洲
没上市的科技互联网公司
深圳互联网科技公司
期待互联网科技
佩信高科技互联网
剑昆互联网络科技有限公司临桂
允成互联网科技有限公司融资
上海京穆互联网科技有限公司
全球科技与互联网公司市值变化
容联科技互联网电商
利用互联网等科技手段