OpenCV图像色温

目录

实现原理

代码实现

结果


实现原理

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

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

代码实现

function.h

#pragma once
#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp> 
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

Mat ColorTemperature(Mat input, int n);

function.cpp

#include "function.h"

// 色温调节
Mat ColorTemperature(Mat input, int n)
{
	Mat result = input.clone();
	int row = input.rows;
	int col = input.cols;
	int level = n / 2;
	for (int i = 0; i < row; ++i)
	{
		uchar* a = input.ptr<uchar>(i);
		uchar* r = result.ptr<uchar>(i);
		for (int j = 0; j < col; ++j)
		{
			int R, G, B;
			// R通道
			R = a[j * 3 + 2];
			R = R + level;
			if (R > 255) {
				r[j * 3 + 2] = 255;
			}
			else if (R < 0) {
				r[j * 3 + 2] = 0;
			}
			else {
				r[j * 3 + 2] = R;
			}
			// G通道
			G = a[j * 3 + 1];
			G = G + level;
			if (G > 255) {
				r[j * 3 + 1] = 255;
			}
			else if (G < 0) {
				r[j * 3 + 1] = 0;
			}
			else {
				r[j * 3 + 1] = G;
			}
			// B通道
			B = a[j * 3];
			B = B - level;
			if (B > 255) {
				r[j * 3] = 255;
			}
			else if (B < 0) {
				r[j * 3] = 0;
			}
			else {
				r[j * 3] = B;
			}
		}
	}
	return result;
}

main.cpp

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp> 
#include <opencv2/opencv.hpp>
#include "function.h"
using namespace cv;
using namespace std;

int main()
{
	Mat scr = imread("F:\\picture\\江.jpg");
	Mat res = ColorTemperature(scr, 50);
	imshow("原图", scr);
	imshow("处理后", res);
	waitKey(0);
	return 0;
}

结果

原图像

n为50
n为-50

 如果以上内容对你有所帮助,不妨点赞关注一下,感谢观看!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值