Created
December 28, 2016 22:30
-
-
Save m1keall1son/cc5f8e047f5740ce1e8392a2d1552024 to your computer and use it in GitHub Desktop.
Simple Color Balancing in OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! in - src image to balance | |
//! out - empty mat to merge results into | |
//! percent - 0 - 100. somewhere around 20 looks good. | |
void colorBalance(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,CV_SORT_EVERY_ROW + CV_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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment