title: Most efficient way to clamp values in an OpenCv Mat
body: <p>I have an OpenCv <code>Mat</code> that I'm going to use for per-pixel remapping, called <code>remap</code>, that has <code>CV_32FC2</code> elements.</p>
  
  <p>Some of these elements might be outside of the allowed range for the remap. So I need to clamp them between <code>Point2f(0, 0)</code> and <code>Point2f(w, h)</code>. What is the shortest, or most efficient, way of accomplishing this with OpenCv 2.x?</p>
  
  <p>Here's one solution:</p>
  
  <pre><code>void clamp(Mat&amp; mat, Point2f lowerBound, Point2f upperBound) {
      vector&lt;Mat&gt; 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);   
  }
  </code></pre>
  
  <p>But I'm not sure if it's the shortest, or if split/merge is efficient.</p>