首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

OpenCV-图像对比度

  • 25-03-03 21:21
  • 3846
  • 11027
blog.csdn.net

作者:翟天保Steven
版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处

实现原理

       图像对比度指的是一幅图像中明暗区域最亮的白和最暗的黑之间不同亮度层级的测量,即指一幅图像灰度反差的大小。差异范围越大代表对比越大,差异范围越小代表对比越小。设置一个基准值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三通道数值为:

RGB=thresh+(RGB-thresh)/(1-percent)

       3.若percent小于0时,对比降低,此时调整后的图像RGB三通道值为:

RGB=thresh+(RGB-thresh)*(1+percent)

       4.若percent等于1时,大于thresh则等于255,小于则等于0。

       至此,图像实现了明度的调整,算法逻辑参考xingyanxiao。C++实现代码如下。

功能函数代码

  1. // 对比度
  2. cv::Mat Contrast(cv::Mat src, int percent)
  3. {
  4. float alpha = percent / 100.f;
  5. alpha = max(-1.f, min(1.f, alpha));
  6. cv::Mat temp = src.clone();
  7. int row = src.rows;
  8. int col = src.cols;
  9. int thresh = 127;
  10. for (int i = 0; i < row; ++i)
  11. {
  12. uchar *t = temp.ptr<uchar>(i);
  13. uchar *s = src.ptr<uchar>(i);
  14. for (int j = 0; j < col; ++j)
  15. {
  16. uchar b = s[3 * j];
  17. uchar g = s[3 * j + 1];
  18. uchar r = s[3 * j + 2];
  19. int newb, newg, newr;
  20. if (alpha == 1)
  21. {
  22. t[3 * j + 2] = r > thresh ? 255 : 0;
  23. t[3 * j + 1] = g > thresh ? 255 : 0;
  24. t[3 * j] = b > thresh ? 255 : 0;
  25. continue;
  26. }
  27. else if (alpha >= 0)
  28. {
  29. newr = static_cast<int>(thresh + (r - thresh) / (1 - alpha));
  30. newg = static_cast<int>(thresh + (g - thresh) / (1 - alpha));
  31. newb = static_cast<int>(thresh + (b - thresh) / (1 - alpha));
  32. }
  33. else {
  34. newr = static_cast<int>(thresh + (r - thresh) * (1 + alpha));
  35. newg = static_cast<int>(thresh + (g - thresh) * (1 + alpha));
  36. newb = static_cast<int>(thresh + (b - thresh) * (1 + alpha));
  37. }
  38. newr = max(0, min(255, newr));
  39. newg = max(0, min(255, newg));
  40. newb = max(0, min(255, newb));
  41. t[3 * j + 2] = static_cast<uchar>(newr);
  42. t[3 * j + 1] = static_cast<uchar>(newg);
  43. t[3 * j] = static_cast<uchar>(newb);
  44. }
  45. }
  46. return temp;
  47. }

C++测试代码

  1. #include
  2. #include
  3. using namespace cv;
  4. using namespace std;
  5. cv::Mat Contrast(cv::Mat src, int percent);
  6. int main()
  7. {
  8. cv::Mat src = imread("5.jpg");
  9. cv::Mat result = Contrast(src, 50.f);
  10. imshow("original", src);
  11. imshow("result", result);
  12. waitKey(0);
  13. return 0;
  14. }
  15. // 对比度
  16. cv::Mat Contrast(cv::Mat src, int percent)
  17. {
  18. float alpha = percent / 100.f;
  19. alpha = max(-1.f, min(1.f, alpha));
  20. cv::Mat temp = src.clone();
  21. int row = src.rows;
  22. int col = src.cols;
  23. int thresh = 127;
  24. for (int i = 0; i < row; ++i)
  25. {
  26. uchar *t = temp.ptr(i);
  27. uchar *s = src.ptr(i);
  28. for (int j = 0; j < col; ++j)
  29. {
  30. uchar b = s[3 * j];
  31. uchar g = s[3 * j + 1];
  32. uchar r = s[3 * j + 2];
  33. int newb, newg, newr;
  34. if (alpha == 1)
  35. {
  36. t[3 * j + 2] = r > thresh ? 255 : 0;
  37. t[3 * j + 1] = g > thresh ? 255 : 0;
  38. t[3 * j] = b > thresh ? 255 : 0;
  39. continue;
  40. }
  41. else if (alpha >= 0)
  42. {
  43. newr = static_cast<int>(thresh + (r - thresh) / (1 - alpha));
  44. newg = static_cast<int>(thresh + (g - thresh) / (1 - alpha));
  45. newb = static_cast<int>(thresh + (b - thresh) / (1 - alpha));
  46. }
  47. else {
  48. newr = static_cast<int>(thresh + (r - thresh) * (1 + alpha));
  49. newg = static_cast<int>(thresh + (g - thresh) * (1 + alpha));
  50. newb = static_cast<int>(thresh + (b - thresh) * (1 + alpha));
  51. }
  52. newr = max(0, min(255, newr));
  53. newg = max(0, min(255, newg));
  54. newb = max(0, min(255, newb));
  55. t[3 * j + 2] = static_cast(newr);
  56. t[3 * j + 1] = static_cast(newg);
  57. t[3 * j] = static_cast(newb);
  58. }
  59. }
  60. return temp;
  61. }

测试效果

图1 原图
图2 参数为50的效果图
图3 参数为-50的效果图

       通过调整percent可以实现图像对比度的调整。

       如果函数有什么可以改进完善的地方,非常欢迎大家指出,一同进步何乐而不为呢~

       如果文章帮助到你了,可以点个赞让我知道,我会很快乐~加油!

注:本文转载自blog.csdn.net的翟天保Steven的文章"https://zhaitianbao.blog.csdn.net/article/details/120107171"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top