Created
January 14, 2015 07:00
-
-
Save royshil/1449e22993e98414e9eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/// perform the Simplest Color Balancing algorithm | |
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,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); | |
} | |
// Usage example | |
void main() { | |
Mat tmp,im = imread("lily.png"); | |
SimplestCB(im,tmp,1); | |
imshow("orig",im); | |
imshow("balanced",tmp); | |
waitKey(0); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment