kylemcdonald/ofxCv — Issue #30: implement a clamp() function
Author: kylemcdonald
Created: 2012-01-17 04:18:36+00:00
State: open

it's a common enough operation, but using min/max isn't always obvious. something like this:

// clamp out of place
template
void clamp(S& src, float lowerBound, float upperBound, D& dst) {
ofxCv::min(ofxCv::max(src, lowerBound), upperBound, dst);
}

// clamp in place
template
void clamp(SD& srcDst, float lowerBound, float upperBound) {
ofxCv::min(ofxCv::max(srcDst, lowerBound), upperBound, srcDst);
}

this requires ofxCv::min/max to be defined as well.

also see http://stackoverflow.com/questions/7552896/most-efficient-way-to-clamp-values-in-an-opencv-mat