Skip to content
Snippets Groups Projects
Commit dac67651 authored by Thibault Allenet's avatar Thibault Allenet
Browse files

Merge branch 'TensorImpl' into setBackend

parents 5fb9b7f9 a6a1363d
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
}
......
......@@ -36,37 +36,39 @@ namespace Aidge {
std::vector<DimSize_t> matDims = std::vector<DimSize_t>({static_cast<DimSize_t>(mat.cols),
static_cast<DimSize_t>(mat.rows),
static_cast<DimSize_t>(mat.channels())});
// Create tensor from the dims of the Cv::Mat
std::shared_ptr<Tensor> tensor = std::make_shared<Tensor>(matDims);
// Set beackend opencv
tensor->setBackend("opencv");
// Set Data Type
Aidge::DataType type;
switch (mat.depth()) {
case CV_8U:
tensor->setDataType(Aidge::DataType::UInt8);
type = Aidge::DataType::UInt8;
break;
case CV_8S:
tensor->setDataType(Aidge::DataType::Int8);
type = Aidge::DataType::Int8;
break;
case CV_16U:
tensor->setDataType(Aidge::DataType::UInt16);
type = Aidge::DataType::UInt16;
break;
case CV_16S:
tensor->setDataType(Aidge::DataType::Int16);
type = Aidge::DataType::Int16;
break;
case CV_32S:
tensor->setDataType(Aidge::DataType::Int32);
type = Aidge::DataType::Int32;
break;
case CV_32F:
tensor->setDataType(Aidge::DataType::Float32);
type = Aidge::DataType::Float32;
break;
case CV_64F:
tensor->setDataType(Aidge::DataType::Float64);
type = Aidge::DataType::Float64;
break;
default:
throw std::runtime_error(
"Cannot convert cv::Mat to Tensor: incompatible types.");
}
// Create tensor from the dims of the Cv::Mat
std::shared_ptr<Tensor> tensor = std::make_shared<Tensor>(matDims,type);
// Set beackend opencv
tensor->setBackend("opencv");
// Cast the tensorImpl to access setCvMat function
TensorImpl_opencv_* tImpl_opencv = dynamic_cast<TensorImpl_opencv_*>(tensor->getImpl().get());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment