diff --git a/include/aidge/backend/opencv/data/TensorImpl.hpp b/include/aidge/backend/opencv/data/TensorImpl.hpp index 1cd9fc812173451bec496f8cb9986b1ce5c0ea21..85285704cb2a605e987c1fc4719b2dbe6cd6e9d9 100644 --- a/include/aidge/backend/opencv/data/TensorImpl.hpp +++ b/include/aidge/backend/opencv/data/TensorImpl.hpp @@ -28,7 +28,7 @@ namespace Aidge { class TensorImpl_opencv_ { public: - virtual const cv::Mat& getCvMat() const = 0; + virtual const cv::Mat& data() const = 0; virtual void setCvMat(const cv::Mat& mat ) = 0; }; @@ -37,8 +37,7 @@ 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; + cv::Mat mData; public: static constexpr const char *Backend = "opencv"; @@ -46,21 +45,18 @@ public: TensorImpl_opencv() = delete; TensorImpl_opencv(const Tensor &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 - cv::MatConstIterator_<T> it1 = mDataOwner->begin<T>(); + cv::MatConstIterator_<T> it1 = mData.begin<T>(); - const cv::Mat otherData = reinterpret_cast<const TensorImpl_opencv<T> &>(otherImpl).data(); + const cv::Mat & otherData = reinterpret_cast<const TensorImpl_opencv<T> &>(otherImpl).data(); cv::MatConstIterator_<T> it2 = otherData.begin<T>(); // Iterate over the elements and compare them - for (; it1 != mDataOwner->end<T>(); ++it1, ++it2) { + for (; it1 != mData.end<T>(); ++it1, ++it2) { if (*it1 != *it2) { return false; } @@ -73,11 +69,11 @@ public: } // native interface - const cv::Mat data() const { return *mData; } + const cv::Mat & data() const override { 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"); @@ -164,29 +160,26 @@ 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();} + void setCvMat(const cv::Mat& mat) override {mData=mat;} virtual ~TensorImpl_opencv() = default; @@ -196,11 +189,10 @@ 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() , "trying to enlarge non-owned data"); - cv::Mat myNewMatrix; if (mTensor.nbDims() < 3) { - myNewMatrix = cv::Mat(((mTensor.nbDims() > 1) ? static_cast<int>(mTensor.dims()[1]) + mData = cv::Mat(((mTensor.nbDims() > 1) ? static_cast<int>(mTensor.dims()[1]) : (mTensor.nbDims() > 0) ? 1 : 0), (mTensor.nbDims() > 0) ? static_cast<int>(mTensor.dims()[0]) : 0, @@ -214,11 +206,8 @@ private: OpenCvType<T>::type)); } - cv::merge(channels, myNewMatrix); + cv::merge(channels, mData); } - - mDataOwner.reset(new cv::Mat(std::forward<cv::Mat>(myNewMatrix))); - mData = mDataOwner.get(); } } diff --git a/src/utils/Utils.cpp b/src/utils/Utils.cpp index af7809cdeb064a10d235455207e6475211872360..dcf2701e708e4394285be260faea50481a6b39b9 100644 --- a/src/utils/Utils.cpp +++ b/src/utils/Utils.cpp @@ -22,7 +22,7 @@ std::shared_ptr<Aidge::Tensor> Aidge::convertCpu(std::shared_ptr<Aidge::Tensor> // Get the cv::Mat from the tensor backend Opencv Aidge::TensorImpl_opencv_* tImplOpencv = dynamic_cast<Aidge::TensorImpl_opencv_*>(tensorOpencv->getImpl().get()); - cv::Mat dataOpencv = tImplOpencv->getCvMat(); + cv::Mat dataOpencv = tImplOpencv->data(); // Convert the cv::Mat into a vector of cv::Mat (vector of channels) std::vector<cv::Mat> channels; diff --git a/unit_tests/Test_Stimuli.cpp b/unit_tests/Test_Stimuli.cpp index 7f50012c9d24bb3f023c0a050f6766928631a78d..cf11584e9134adf1d64ba8ceae912ba529407756 100644 --- a/unit_tests/Test_Stimuli.cpp +++ b/unit_tests/Test_Stimuli.cpp @@ -28,8 +28,8 @@ TEST_CASE("Stimuli creation", "[Stimuli][OpenCV]") { // Access the cv::Mat with the tensor TensorImpl_opencv_* tImpl_opencv = dynamic_cast<TensorImpl_opencv_*>(tensor_load->getImpl().get()); - REQUIRE(tImpl_opencv->getCvMat().size() == true_mat.size()); - REQUIRE(cv::countNonZero(tImpl_opencv->getCvMat() != true_mat) == 0); + REQUIRE(tImpl_opencv->data().size() == true_mat.size()); + REQUIRE(cv::countNonZero(tImpl_opencv->data() != true_mat) == 0); // This time the tensor is already loaded in memory @@ -39,8 +39,8 @@ TEST_CASE("Stimuli creation", "[Stimuli][OpenCV]") { // Access the cv::Mat with the tensor TensorImpl_opencv_* tImpl_opencv_2 = dynamic_cast<TensorImpl_opencv_*>(tensor_load_2->getImpl().get()); - REQUIRE(tImpl_opencv_2->getCvMat().size() == true_mat.size()); - REQUIRE(cv::countNonZero(tImpl_opencv_2->getCvMat() != true_mat) == 0); + REQUIRE(tImpl_opencv_2->data().size() == true_mat.size()); + REQUIRE(cv::countNonZero(tImpl_opencv_2->data() != true_mat) == 0); } } diff --git a/unit_tests/Test_StimuliImpl_opencv_imread.cpp b/unit_tests/Test_StimuliImpl_opencv_imread.cpp index fd5173ba6b85658f0623ae736e8d31c52ce5cc79..ef3b048f31397cfb884bf5df603bf2558d905e51 100644 --- a/unit_tests/Test_StimuliImpl_opencv_imread.cpp +++ b/unit_tests/Test_StimuliImpl_opencv_imread.cpp @@ -27,8 +27,8 @@ TEST_CASE("StimuliImpl_opencv_imread creation", "[StimuliImpl_opencv_imread][Ope // Access the cv::Mat with the tensor TensorImpl_opencv_* tImpl_opencv = dynamic_cast<TensorImpl_opencv_*>(tensor_load->getImpl().get()); - REQUIRE(tImpl_opencv->getCvMat().size() == true_mat.size()); - REQUIRE(cv::countNonZero(tImpl_opencv->getCvMat() != true_mat) == 0); + REQUIRE(tImpl_opencv->data().size() == true_mat.size()); + REQUIRE(cv::countNonZero(tImpl_opencv->data() != true_mat) == 0); } } diff --git a/unit_tests/Test_TensorImpl.cpp b/unit_tests/Test_TensorImpl.cpp index 7da8cca4b7d5fe638fc7196da08e8d55f93b2475..2b5a7a61cc0e4c90b50a10ff2273bd0a6e4a9280 100644 --- a/unit_tests/Test_TensorImpl.cpp +++ b/unit_tests/Test_TensorImpl.cpp @@ -62,11 +62,11 @@ TEST_CASE("Tensor creation opencv", "[Tensor][OpenCV]") { } SECTION("OpenCV tensor features") { - REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->getCvMat().rows == 2); - REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->getCvMat().cols == 2); - REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->getCvMat().dims == 2); - REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->getCvMat().total() == 4); - REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->getCvMat().channels() == 2); + REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->data().rows == 2); + REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->data().cols == 2); + REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->data().dims == 2); + REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->data().total() == 4); + REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->data().channels() == 2); } SECTION("Access to array") { diff --git a/unit_tests/Tests_Utils.cpp b/unit_tests/Tests_Utils.cpp index d2a80c4f46c5a3bfaedda0cc10b432cc6228a91c..0fa81384d6e96ad3e96bbf944990226146aac4e0 100644 --- a/unit_tests/Tests_Utils.cpp +++ b/unit_tests/Tests_Utils.cpp @@ -64,7 +64,7 @@ TEMPLATE_TEST_CASE("Opencv Utils", "[Utils][OpenCV]", signed char, unsigned char // Check the matrix inside the tensor coorresponds to the matrix TensorImpl_opencv_* tImpl_opencv = dynamic_cast<TensorImpl_opencv_*>(tensorOcv->getImpl().get()); - auto mat_tensor = tImpl_opencv->getCvMat(); + auto mat_tensor = tImpl_opencv->data(); REQUIRE(mat_tensor.size() == mat.size()); REQUIRE(cv::countNonZero(mat_tensor != mat) == 0);