首页 最新 热门 推荐

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

OpenCV-图像色温

  • 25-03-03 21:22
  • 2020
  • 6001
blog.csdn.net

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

实现原理

       色温是表示光线中包含颜色成分的一个计量单位。从理论上说,黑体温度指绝对黑体从绝对零度(-273℃)开始加温后所呈现的颜色。黑体在受热后,逐渐由黑变红,转黄,发白,最后发出蓝色光。当加热到一定的温度,黑体发出的光所含的光谱成分,就称为这一温度下的色温,计量单位为“K”(开尔文)。

       在图像处理中,对图像进行色温调整也是常见的操作之一。一般情况下,认为暖色偏黄色,冷色偏蓝色,基于此逻辑,在提高色温的时候,对红色和绿色通道进行增强,对蓝色通道进行减弱,这样就能让图像的黄色占比提高,进而达到暖黄色的效果;反之亦然,降低色温,只需要增强蓝色通道,减少红色和绿色。

        至此,图像实现了色温的调整。C++实现代码如下。

功能函数代码

  1. // 色温调节
  2. cv::Mat ColorTemperature(cv::Mat input, int n)
  3. {
  4. cv::Mat result = input.clone();
  5. int row = input.rows;
  6. int col = input.cols;
  7. int level = n/2;
  8. for (int i = 0; i < row; ++i)
  9. {
  10. uchar* a = input.ptr<uchar>(i);
  11. uchar* r = result.ptr<uchar>(i);
  12. for (int j = 0; j < col; ++j)
  13. {
  14. int R,G,B;
  15. // R通道
  16. R = a[j * 3 + 2];
  17. R = R + level;
  18. if (R > 255) {
  19. r[j * 3 + 2] = 255;
  20. }
  21. else if (R < 0) {
  22. r[j * 3 + 2] = 0;
  23. }
  24. else {
  25. r[j * 3 + 2] = R;
  26. }
  27. // G通道
  28. G = a[j * 3 + 1];
  29. G = G + level;
  30. if (G > 255) {
  31. r[j * 3 + 1] = 255;
  32. }
  33. else if (G < 0) {
  34. r[j * 3 + 1] = 0;
  35. }
  36. else {
  37. r[j * 3 + 1] = G;
  38. }
  39. // B通道
  40. B = a[j * 3];
  41. B = B - level;
  42. if (B > 255) {
  43. r[j * 3] = 255;
  44. }
  45. else if (B < 0) {
  46. r[j * 3] = 0;
  47. }
  48. else {
  49. r[j * 3] = B;
  50. }
  51. }
  52. }
  53. return result;
  54. }

C++测试代码

  1. #include
  2. #include
  3. #include
  4. using namespace cv;
  5. using namespace std;
  6. cv::Mat ColorTemperature(cv::Mat input, int n);
  7. int main()
  8. {
  9. cv::Mat src = imread("test4.jpg");
  10. int n1 = 50;
  11. int n2 = -50;
  12. cv::Mat result1 = ColorTemperature(src, n1);
  13. cv::Mat result2 = ColorTemperature(src, n2);
  14. imshow("original", src);
  15. imshow("result1", result1);
  16. imshow("result2", result2);
  17. waitKey();
  18. return 0;
  19. }
  20. // 色温调节
  21. cv::Mat ColorTemperature(cv::Mat input, int n)
  22. {
  23. cv::Mat result = input.clone();
  24. int row = input.rows;
  25. int col = input.cols;
  26. int level = n/2;
  27. for (int i = 0; i < row; ++i)
  28. {
  29. uchar* a = input.ptr(i);
  30. uchar* r = result.ptr(i);
  31. for (int j = 0; j < col; ++j)
  32. {
  33. int R,G,B;
  34. // R通道
  35. R = a[j * 3 + 2];
  36. R = R + level;
  37. if (R > 255) {
  38. r[j * 3 + 2] = 255;
  39. }
  40. else if (R < 0) {
  41. r[j * 3 + 2] = 0;
  42. }
  43. else {
  44. r[j * 3 + 2] = R;
  45. }
  46. // G通道
  47. G = a[j * 3 + 1];
  48. G = G + level;
  49. if (G > 255) {
  50. r[j * 3 + 1] = 255;
  51. }
  52. else if (G < 0) {
  53. r[j * 3 + 1] = 0;
  54. }
  55. else {
  56. r[j * 3 + 1] = G;
  57. }
  58. // B通道
  59. B = a[j * 3];
  60. B = B - level;
  61. if (B > 255) {
  62. r[j * 3] = 255;
  63. }
  64. else if (B < 0) {
  65. r[j * 3] = 0;
  66. }
  67. else {
  68. r[j * 3] = B;
  69. }
  70. }
  71. }
  72. return result;
  73. }

测试效果

图1 原图
图2 n为50的效果图
图3 n为-50的效果图

       通过调整percent可以实现图像色温的调整。

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

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


       最近看了一部很棒的灾难片——《峰爆》,推荐给大家。

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

/ 登录

评论记录:

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

分类栏目

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