Efficient C++ way to shift a cv::Mat with OpenCV
14:16 04 Feb 2015

What is an efficient way to "shift" an OpenCV cv::Mat?

With shift I mean that if I have a row like this

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

and I shift it by 3 positions, I will get a row like this

3, 4, 5, 6, 7, 8, 9, 0, 1, 2

Now I am using this function:

void shift( const cv::Mat& in, cv::Mat& out, int shift )
{
   if ( shift < 0 || shift > in.cols ) return;

   if ( shift == 0 || shift==in.cols ) {
      out = in.clone();
   } else {
      cv::hconcat(in(cv::Rect(shift,0,in.cols-shift,in.rows)),in(cv::Rect(0,0,shift,in.rows)),out);
   }
}

but I am looking for a more efficient way.

c++ opencv bit-shift