1

I am looking for some advices how to speed up the following operation. Is there some STL library function, or opencv or Intel IPP function that I can use?

I have a 3840 x 2160 image buffer containing BGRA pixel-packed data. the image is interleaved from two camera sources. i.e. the memory layout is as follows

left eye row 1 ;
right eye row 1 ;
left eye row 2 ;
right eye row 2 ;
...
left eye row 1080 ;
right eye row 1080 ;

Now I need to split left and right so that in the new image buffer the first half ( the top half) is left eye and the second half (bottom half) is right eye.

new image frame:

left eye row 1 ;
left eye row 2 ;
...
left eye row 1080 ;
right eye row 1 ;
right eye row 2 ;
...
right eye row 1080 ;

//here is my current code.  

unsigned char* data;   //data points to the original interleaved buffer


unsigned char top_bottom_frame [3840 * 2160 * 4];   //left right de-interleaved frame

const int stride = 4* 3840;

//copy left eye rows 
for(int i=0, j=0; i< 2160; i = i+2, ++j)
{
   memcpy(&top_bottom_frame[j*stride], data + i * stride, stride);
}

//copy right eye rows 
for(int i=1, j= 1080 ; i<2160; i = i+2, ++j)
{
   memcpy(&top_bottom_frame[j*stride], data + i * stride, stride);
}
leon
  • 35
  • 4
  • I would try a single loop with 2 `memcpy()` in it so you only go through memory once. But the key is to measure it properly. – Mark Setchell Apr 30 '19 at 09:13
  • I'm not sure how fast is [copyTo()](https://docs.opencv.org/3.1.0/d3/d63/classcv_1_1Mat.html#afb01ff6b2231b72f55618bfb66a5326b) but maybe you can try something like this : `cv::Mat leftEyeSubBuffer(3840, 1080, CV_8UC4, data, 2160)` (have a look to [this constructor](https://docs.opencv.org/3.1.0/d3/d63/classcv_1_1Mat.html#a51615ebf17a64c968df0bf49b4de6a3a)) then do `leftEyeSubBuffer.copyTo(newLeftEyeBuffer)`, and similarly for right eye. – Annyo Apr 30 '19 at 09:34
  • what kind of performance are you looking for here anyway tolerance-wise ?? I ran your code 1000 times and it took 8 seconds on coliru - at least this probably rules out any more advanced solutions, like 'mp' etc. , as the overhead will be too much. – darune Apr 30 '19 at 11:45
  • 1
    You can abuse cv::resize() (or the GPU version aka cv::cuda::resize()) as answered in similar question: https://stackoverflow.com/questions/27905929/opencv-get-a-copy-of-even-rows-of-a-mat – Paul E Apr 30 '19 at 13:56

0 Answers0