I have a code which creates a xt::xarray from CV Mat by adapting:
std::array<size_t, 3> shape = {static_cast<size_t>(img.rows),
static_cast<size_t>(img.cols), 2};
xt::xarray<float> arr = xt::adapt(img.ptr<float>(), shape);
Later I have a helper function to generate linearly spaced indices:
xt::xarray<float> sparsified = sparsify(arr);
where:
xt::xarray<float> sparsify(const xt::xarray<float> &arr) const
{
auto yIndices = xt::linspace<int>(0, arr.shape(0) - 1, _config._sparseGridY, false);
auto xIndices = xt::linspace<int>(0, arr.shape(1) - 1, _config._sparseGridX, false);
return xt::view(arr, xt::keep(yIndices), xt::keep(xIndices), xt::all());
}
Am I making a copy of the array when xt::view() is returned as xt::xarray? I want to reduce copy of the data to minimum.
1
Am I making a copy of the array when xt::view() is returned as xt::xarray?
Yes because although xt::xview
does not perform a copy of the underlying expression we still have the return type of the function sparsify
as xt::xarray<float>
(essentially returning by value) which means even conceptually, a copy is created using the return expression.
2