目录
实现原理
色温是表示光线中包含颜色成分的一个计量单位。从理论上说,黑体温度指绝对黑体从绝对零度(-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;
}
结果
如果以上内容对你有所帮助,不妨点赞关注一下,感谢观看!