From a6a1363daaa6703192cb16952e934aa6a7d5592d Mon Sep 17 00:00:00 2001 From: thibault allenet <thibault.allenet@cea.fr> Date: Tue, 23 Jan 2024 17:49:56 +0000 Subject: [PATCH] Update tensorOpencv to avoid CopyCast with setDataType --- include/aidge/backend/opencv/utils/Utils.hpp | 26 +++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/include/aidge/backend/opencv/utils/Utils.hpp b/include/aidge/backend/opencv/utils/Utils.hpp index 3caa6f9..ee525dc 100644 --- a/include/aidge/backend/opencv/utils/Utils.hpp +++ b/include/aidge/backend/opencv/utils/Utils.hpp @@ -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()); -- GitLab