作者:Steven
著作權宣告:著作權歸作者所有,商業轉載請聯系作者獲得授權,非商業轉載請注明出處
實作原理
色溫是表示光線中包含顏色成分的一個計量單位,從理論上說,黑體溫度指絕對黑體從絕對零度(-273℃)開始加溫后所呈現的顏色,黑體在受熱后,逐漸由黑變紅,轉黃,發白,最后發出藍色光,當加熱到一定的溫度,黑體發出的光所含的光譜成分,就稱為這一溫度下的色溫,計量單位為“K”(開爾文),
在影像處理中,對影像進行色溫調整也是常見的操作之一,一般情況下,認為暖色偏黃色,冷色偏藍色,基于此邏輯,在提高色溫的時候,對紅色和綠色通道進行增強,對藍色通道進行減弱,這樣就能讓影像的黃色占比提高,進而達到暖黃色的效果;反之亦然,降低色溫,只需要增強藍色通道,減少紅色和綠色,
至此,影像實作了色溫的調整,C++實作代碼如下,
功能函式代碼
// 色溫調節
cv::Mat ColorTemperature(cv::Mat input, int percent)
{
cv::Mat result = input.clone();
int row = input.rows;
int col = input.cols;
int level = percent/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;
}
C++測驗代碼
#include <iostream>
#include <opencv2\opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
cv::Mat ColorTemperature(cv::Mat input, int percent);
int main()
{
cv::Mat src = imread("test4.jpg");
int percent1 = 50;
int percent2 = -50;
cv::Mat result1 = ColorTemperature(src, percent1);
cv::Mat result2 = ColorTemperature(src, percent2);
imshow("original", src);
imshow("result1", result1);
imshow("result2", result2);
waitKey();
return 0;
}
// 色溫調節
cv::Mat ColorTemperature(cv::Mat input, int percent)
{
cv::Mat result = input.clone();
int row = input.rows;
int col = input.cols;
int level = percent/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;
}
測驗效果
通過調整percent可以實作影像色溫的調整,
如果函式有什么可以改進完善的地方,非常歡迎大家指出,一同進步何樂而不為呢~
如果文章幫助到你了,可以點個贊讓我知道,我會很快樂~加油!
最近看了一部很棒的災難片——《峰爆》,推薦給大家,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301625.html
標籤:其他
