- 87.3k
- 14
- 104
- 322
Your performance is never going to approach that of the library you're comparing to, unless you change the data layout.
I don't know Matlab, but I've done a small amount of work with OpenCV, so I can speculate how the reshape()
function works.
Consider this 4✕2 array:
A B C D
a b c d
which can be represented like this in OpenCV (row-major order):
width=4, height=2,
content = { A, B, C, D, a, b, c, d }
To reshape to a 4✕2 array, all we do is change the width
and height
members, without touching the content.
But my guess is that Matlab indexes the content array in the opposite (column-major) order:
height=2, width=4
content = { A, a, B, b, C, c, D, d }
Reshaping this by swapping width
and height
yields
A C
a c
B D
b d
So the key to performance may well be to change order of dimensions in the translated code so that CV reshape does what we want - instead of declaring arrays as height✕width and indexing using (y, x), declare them as width✕height and index them as (x, y). Extending this from 2 dimensions to 3 shouldn't be a problem.
To avoid error-prone edits all over the code, I recommend writing a small wrapper around cv::Mat_
which can be addressed in Matlab order.
- 87.3k
- 14
- 104
- 322