-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorBalance.cpp
More file actions
86 lines (65 loc) · 2.28 KB
/
ColorBalance.cpp
File metadata and controls
86 lines (65 loc) · 2.28 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<opencv.hpp>
#include<iostream>
#include"firstheader.h"
using namespace cv;
using namespace std;
void SimplestCB(Mat& in, Mat& out, float percent) {
assert(in.channels() == 3);
assert(percent > 0 && percent < 100);
float half_percent = percent / 200.0f;
vector<Mat> tmpsplit; split(in, tmpsplit);
for (int i = 0; i < 3; i++) {
//find the low and high precentile values (based on the input percentile)
Mat flat; tmpsplit[i].reshape(1, 1).copyTo(flat);
cv::sort(flat, flat, SORT_EVERY_ROW + SORT_ASCENDING);
int lowval = flat.at<uchar>(cvFloor(((float)flat.cols) * half_percent));
int highval = flat.at<uchar>(cvCeil(((float)flat.cols) * (1.0 - half_percent)));
cout << lowval << " " << highval << endl;
//saturate below the low percentile and above the high percentile
tmpsplit[i].setTo(lowval, tmpsplit[i] < lowval);
tmpsplit[i].setTo(highval, tmpsplit[i] > highval);
//scale the channel
normalize(tmpsplit[i], tmpsplit[i], 0, 255, NORM_MINMAX);
}
merge(tmpsplit, out);
}
// Usage example
//void main() {
// Mat tmp, im = imread("nar2.jpg");
//
// SimplestCB(im, tmp, 1);
//
// imshow("orig", im);
// imshow("balanced", tmp);
// waitKey(0);
// return;
//}
int main(int argc, char** argv) {
Mat input = imread("nar2.jpg");
if (!input.data) {
cout << "Error Loading image" << endl;
return -1;
}
namedWindow("prew", WINDOW_AUTOSIZE);
//slider global values
int iSliderValue1 = 0;
int iSliderValue2 = 0;
int iSliderValue3 = 0;
createTrackbar("R", "prew", &iSliderValue1, 215);
createTrackbar("G", "prew", &iSliderValue2, 215);
createTrackbar("B", "prew", &iSliderValue3, 215);
Mat img;
input.copyTo(img);
for (int x = 0; x < input.cols - 1; x++) {
for (int y = 0; y < input.rows - 1; y++) {
img.at<Vec3b>(y, x)[0] = img.at<Vec3b>(y, x)[0] * (iSliderValue1) / 200;
img.at<Vec3b>(y, x)[1] = img.at<Vec3b>(y, x)[1] * (iSliderValue2) / 200;
img.at<Vec3b>(y, x)[2] = img.at<Vec3b>(y, x)[2] * (iSliderValue3) / 200;
}
}
SimplestCB(input, img, 50.0);
imshow("Input image", input);
imshow("balanced", img);
waitKey(0);
return 0;
}