OpenCVConverter.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 // STD/STL
25 #include <numeric>
26 
27 // Header
28 #include "OpenCVConverter.h"
29 
31 {
32  cv::Mat
34  {
36 
37  if (nav->getShape().size() < 3)
38  {
40  __PRETTY_FUNCTION__, "The size of an NDArray does not match.", nav->getPath());
41  }
42  auto dims = nav->getShape();
43 
44  cv::Mat ret(std::vector<int>({dims.begin(), std::prev(dims.end())}),
45  std::stoi(nav->getType()));
46  auto size = std::accumulate(std::begin(dims), std::end(dims), 1, std::multiplies<int>());
47  memcpy(reinterpret_cast<unsigned char*>(ret.data), nav->getData(), size);
48  return ret;
49  }
50 
53  {
54  std::vector<int> dims;
55  for (int i = 0; i < mat.dims; ++i)
56  {
57  dims.push_back(mat.size[i]);
58  }
59  dims.push_back(mat.elemSize());
60 
61  auto ret = std::make_shared<data::NDArray>(p);
62  ret->setShape(dims);
63  ret->setType(std::to_string(mat.type()));
64  ret->setData(std::accumulate(std::begin(dims), std::end(dims), 1, std::multiplies<int>()),
65  reinterpret_cast<const unsigned char*>(mat.data));
66 
67  return ret;
68  }
69 } // namespace armarx::aron::data::converter
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::aron::data::NDArrayPtr
std::shared_ptr< NDArray > NDArrayPtr
Definition: NDArray.h:46
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::data::converter::AronOpenCVConverter::ConvertFromMat
static data::NDArrayPtr ConvertFromMat(const cv::Mat &, const armarx::aron::Path &={})
Definition: OpenCVConverter.cpp:52
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
OpenCVConverter.h
armarx::aron::data::converter::AronOpenCVConverter::ConvertToMat
static cv::Mat ConvertToMat(const data::NDArrayPtr &)
Definition: OpenCVConverter.cpp:33
armarx::aron::data::converter
Definition: aron_conversions.cpp:3