diff --git a/include/aidge/backend/opencv/data/TensorImpl.hpp b/include/aidge/backend/opencv/data/TensorImpl.hpp index 50a0531a0d4f197b4d2c543ac42bca7ed9757391..1cd9fc812173451bec496f8cb9986b1ce5c0ea21 100644 --- a/include/aidge/backend/opencv/data/TensorImpl.hpp +++ b/include/aidge/backend/opencv/data/TensorImpl.hpp @@ -37,15 +37,19 @@ private: const Tensor &mTensor; // Impl needs to access Tensor information, but is not // supposed to change it! - cv::Mat mData; - - std::unique_ptr<cv::Mat> mDataOwner = std::unique_ptr<cv::Mat>(new cv::Mat(0,0,OpenCvType<T>::type)); + cv::Mat* mData; + std::unique_ptr<cv::Mat> mDataOwner; public: static constexpr const char *Backend = "opencv"; + TensorImpl_opencv() = delete; TensorImpl_opencv(const Tensor &tensor) - : TensorImpl(Backend), mTensor(tensor) {} + : TensorImpl(Backend), mTensor(tensor) + { + mDataOwner = std::unique_ptr<cv::Mat>(new cv::Mat(0,0,OpenCvType<T>::type)); + mData = mDataOwner.get(); + } bool operator==(const TensorImpl &otherImpl) const override final { // Create iterators for both matrices @@ -69,11 +73,11 @@ public: } // native interface - const cv::Mat data() const { return mData; } + const cv::Mat data() const { return *mData; } std::size_t scalarSize() const override { return sizeof(T); } - std::size_t size() const override { return mData.total() * mData.channels(); } + std::size_t size() const override { return mData->total() * mData->channels();} void setDevice(DeviceIdx_t device) override { AIDGE_ASSERT(device == 0, "device cannot be != 0 for Opencv backend"); @@ -160,29 +164,29 @@ public: void *rawPtr(NbElts_t offset = 0) override { lazyInit(); - return (mData.ptr<T>() + offset); + return (mData->ptr<T>() + offset); }; const void *rawPtr(NbElts_t offset = 0) const override { AIDGE_ASSERT(size() >= mTensor.size(), "accessing uninitialized const rawPtr"); - return (mData.ptr<T>() + offset); + return (mData->ptr<T>() + offset); }; void *hostPtr(NbElts_t offset = 0) override { lazyInit(); - return (mData.ptr<T>() + offset); + return (mData->ptr<T>() + offset); }; const void *hostPtr(NbElts_t offset = 0) const override { AIDGE_ASSERT(size() >= mTensor.size(), "accessing uninitialized const hostPtr"); - AIDGE_ASSERT(mData.isContinuous(), "CV Matrix not continuous"); - return (mData.ptr<T>() + offset); + AIDGE_ASSERT(mData->isContinuous(), "CV Matrix not continuous"); + return (mData->ptr<T>() + offset); }; const cv::Mat& getCvMat() const override { return *mDataOwner.get(); } void setCvMat(const cv::Mat& mat) override { mDataOwner.reset(new cv::Mat(std::move(mat))); - mData = *mDataOwner.get();} + mData = mDataOwner.get();} virtual ~TensorImpl_opencv() = default; @@ -192,7 +196,7 @@ private: void lazyInit() { if (size() < mTensor.size()) { // Need more data, a re-allocation will occur - AIDGE_ASSERT(mData.empty() || mDataOwner != nullptr, "trying to enlarge non-owned data"); + AIDGE_ASSERT(mData->empty() || mDataOwner != nullptr, "trying to enlarge non-owned data"); cv::Mat myNewMatrix; if (mTensor.nbDims() < 3) { @@ -214,7 +218,7 @@ private: } mDataOwner.reset(new cv::Mat(std::forward<cv::Mat>(myNewMatrix))); - mData = *mDataOwner.get(); + mData = mDataOwner.get(); } }