1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include "opencv2/opencv.hpp"
#include <algorithm>
cv::Mat src_img;
cv::Mat lut_img;
int slider_intensity = 0;
void Lut(const cv::Mat& src, const float intensity, cv::Mat& result)
{
auto width = src.cols;
auto height = src.rows ;
result = src_img.clone();
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
int r = src.at<cv::Vec3b>(i, j)[2];
int g = src.at<cv::Vec3b>(i, j)[1];
int b = src.at<cv::Vec3b>(i, j)[0];
//计算从8x8里面的第几块方块取色
int block1 = std::floor(64 * static_cast<float>(b) / 256.0);
int block2 = std::ceil(64 * static_cast<float>(b) / 256.0);
int block_x1 = block1 % 8;
int block_x2 = block2 % 8;
int block_y1 = block1 / 8;
int block_y2 = block2 / 8;
//计算每块里面的具体index
int lut_x1 = block_x1 * 64 + 64 * (r / 256.0);
int lut_x2 = block_x2 * 64 + 64 * (r / 256.0);
int lut_y1 = block_y1 * 64 + 64 * (g / 256.0);
int lut_y2 = block_y2 * 64 + 64 * (g / 256.0);
auto color1 = lut_img.at<cv::Vec3b>(cv::Point(lut_x1, lut_y1));
auto color2 = lut_img.at<cv::Vec3b>(cv::Point(lut_x2, lut_y2));
result.at<cv::Vec3b>(i, j)[0] = std::clamp((int)std::round((color1[0] + color2[0]) / 2.0), 0, 255);
result.at<cv::Vec3b>(i, j)[1] = std::clamp((int)std::round((color1[1] + color2[1]) / 2.0), 0, 255);
result.at<cv::Vec3b>(i, j)[2] = std::clamp((int)std::round((color1[2] + color2[2]) / 2.0), 0, 255);
}
}
//依照强度进行混合
cv::addWeighted(src_img,1.0 - intensity, result, intensity, 0.0, result);
}
void LutTrack(int, void*)
{
cv::Mat result;
float intensity = slider_intensity / 100.0;
Lut(src_img, intensity, result);
cv::imshow("lut", result);
}
int main(int argc, char** argv)
{
src_img = cv::imread("test.jpg");
lut_img = cv::imread("lut.png");
cv::namedWindow("lut");
cv::createTrackbar("intensity", "lut", &slider_intensity, 100, LutTrack, nullptr);
LutTrack(0, nullptr);
cv::waitKey(0);
return 0;
}
|