function out = ocv2mat(img, sz)
    % Convert image data format to MATLAB compatible image format
    % Copyright 2018 The MathWorks, Inc.
    sec = sz(1)*sz(2);
    Im = zeros([sz(2), sz(1), sz(3)], 'uint8');
    % BGRBGR...(openCV) format to RRR...GGG...(MATLAB) format
    Im(1:sec) = img(3:3:end);
    Im(sec+1:sec*2) = img(2:3:end);
    Im(sec*2+1:sec*3) = img(1:3:end);
    % Reshape matrix
    out = permute(Im, [2 1 3]);
end