首页 最新 热门 推荐

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

用OpenCV实现Photoshop算法(五): 亮度对比度调整

  • 25-02-19 08:40
  • 3590
  • 8312
blog.csdn.net

系列文章:

用OpenCV实现Photoshop算法(一): 图像旋转

用OpenCV实现Photoshop算法(二): 图像剪切

用OpenCV实现Photoshop算法(三): 曲线调整

用OpenCV实现Photoshop算法(四): 色阶调整

用OpenCV实现Photoshop算法(五): 亮度对比度调整

用OpenCV实现Photoshop算法(六): 变为黑白图像

用OpenCV实现Photoshop算法(七): 调整色相饱和度

用OpenCV实现Photoshop算法(八): 可选颜色

用OpenCV实现Photoshop算法(九): 高反差保留




五、亮度对比度调整

(一)算法
  亮度对比度调整的算法,我在网上找了很多篇,个人觉得以下这个算法效果较好,比较接近Photoshop的效果。
Algorithm of Brightness Contrast transformation
The formula is:
y = [x - 127.5 * (1 - B)] * k + 127.5 * (1 + B);

x is the input pixel value
y is the output pixel value
B is brightness, value range is [-1,1]
k is used to adjust contrast
k = tan( (45 + 44 * c) / 180 * PI );
c is contrast, value range is [-1,1]

于是,用OpenCV C++编写了一个 adjustBrightnessContrast()函数,可同时调整亮度、对比度。

(二)源码及例程

例程如下, 其中包含 adjustBrightnessContrast()函数。

  1. #include
  2. #include "opencv2/core.hpp"
  3. #include "opencv2/imgproc.hpp"
  4. #include "opencv2/highgui.hpp"
  5. using namespace std;
  6. using namespace cv;
  7. #define SWAP(a, b, t) do { t = a; a = b; b = t; } while(0)
  8. #define CLIP_RANGE(value, min, max) ( (value) > (max) ? (max) : (((value) < (min)) ? (min) : (value)) )
  9. #define COLOR_RANGE(value) CLIP_RANGE(value, 0, 255)
  10. /**
  11. * Adjust Brightness and Contrast
  12. *
  13. * @param src [in] InputArray
  14. * @param dst [out] OutputArray
  15. * @param brightness [in] integer, value range [-255, 255]
  16. * @param contrast [in] integer, value range [-255, 255]
  17. *
  18. * @return 0 if success, else return error code
  19. */
  20. int adjustBrightnessContrast(InputArray src, OutputArray dst, int brightness, int contrast)
  21. {
  22. Mat input = src.getMat();
  23. if( input.empty() ) {
  24. return -1;
  25. }
  26. dst.create(src.size(), src.type());
  27. Mat output = dst.getMat();
  28. brightness = CLIP_RANGE(brightness, -255, 255);
  29. contrast = CLIP_RANGE(contrast, -255, 255);
  30. /**
  31. Algorithm of Brightness Contrast transformation
  32. The formula is:
  33. y = [x - 127.5 * (1 - B)] * k + 127.5 * (1 + B);
  34. x is the input pixel value
  35. y is the output pixel value
  36. B is brightness, value range is [-1,1]
  37. k is used to adjust contrast
  38. k = tan( (45 + 44 * c) / 180 * PI );
  39. c is contrast, value range is [-1,1]
  40. */
  41. double B = brightness / 255.;
  42. double c = contrast / 255. ;
  43. double k = tan( (45 + 44 * c) / 180 * M_PI );
  44. Mat lookupTable(1, 256, CV_8U);
  45. uchar *p = lookupTable.data;
  46. for (int i = 0; i < 256; i++)
  47. p[i] = COLOR_RANGE( (i - 127.5 * (1 - B)) * k + 127.5 * (1 + B) );
  48. LUT(input, lookupTable, output);
  49. return 0;
  50. }
  51. //=====主程序开始====
  52. static string window_name = "photo";
  53. static Mat src;
  54. static int brightness = 255;
  55. static int contrast = 255;
  56. static void callbackAdjust(int , void *)
  57. {
  58. Mat dst;
  59. adjustBrightnessContrast(src, dst, brightness - 255, contrast - 255);
  60. imshow(window_name, dst);
  61. }
  62. int main()
  63. {
  64. src = imread("building.jpg");
  65. if ( !src.data ) {
  66. cout << "error read image" << endl;
  67. return -1;
  68. }
  69. namedWindow(window_name);
  70. createTrackbar("brightness", window_name, &brightness, 2*brightness, callbackAdjust);
  71. createTrackbar("contrast", window_name, &contrast, 2*contrast, callbackAdjust);
  72. callbackAdjust(0, 0);
  73. waitKey();
  74. return 0;
  75. }


运行效果:

原图:



调整参数, 实施亮度对比度调整后:



系列文章:

用OpenCV实现Photoshop算法(一): 图像旋转

用OpenCV实现Photoshop算法(二): 图像剪切

用OpenCV实现Photoshop算法(三): 曲线调整

用OpenCV实现Photoshop算法(四): 色阶调整

用OpenCV实现Photoshop算法(五): 亮度对比度调整

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

/ 登录

评论记录:

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

分类栏目

后端 (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