Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to clamp values in an OpenCv Mat

I have an OpenCv Mat that I'm going to use for per-pixel remapping, called remap, that has CV_32FC2 elements.

Some of these elements might be outside of the allowed range for the remap. So I need to clamp them between Point2f(0, 0) and Point2f(w, h). What is the shortest, or most efficient, way of accomplishing this with OpenCv 2.x?

Here's one solution:

void clamp(Mat& mat, Point2f lowerBound, Point2f upperBound) {
    vector<Mat> matc;
    split(mat, matc);
    min(max(matc[0], lowerBound.x), upperBound.x, matc[0]);
    min(max(matc[1], lowerBound.y), upperBound.y, matc[1]);
    merge(matc, mat);   
}

But I'm not sure if it's the shortest, or if split/merge is efficient.

like image 612
Kyle McDonald Avatar asked Sep 26 '11 09:09

Kyle McDonald


1 Answers

Try splitting, using cvThreshold and then merging. You may also get away with using cvSetImageCOI to avoid the splitting. I'm not sure if thresholding code supports the COI.

You may want to profile both versions and compare their performance. I have a feeling it will do the same thing.

like image 186
mpenkov Avatar answered Sep 18 '22 20:09

mpenkov