ImageWriter.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package VisionX::ArmarXObjects::armem_images
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2021
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "ImageWriter.h"
24 
26 {
27  bool
28  detail::RGBImageWriter::commitRGBImages(const std::vector<cv::Mat>& images,
29  const armarx::core::time::DateTime& referenceTime)
30  {
31  auto mid = armarx::armem::MemoryID();
32  mid.memoryName = properties().memoryName;
33  mid.coreSegmentName = properties().coreSegmentName;
34  mid.providerSegmentName = properties().providerName;
35  mid.entityName = "images";
36 
37  auto commit = armarx::armem::Commit();
38  auto& update = commit.add();
39 
40  update.confidence = 1.0;
41  update.referencedTime = referenceTime;
42  update.entityID = mid;
44 
45  for (const auto& i : images)
46  {
47  visionx::armem_images::arondto::ImageRGB aron;
48  aron.image = i;
49  update.instancesData.push_back(aron.toAron());
50  }
51 
52  auto res = this->memoryWriter().commit(commit);
53  return res.allSuccess();
54  }
55 
56  bool
57  detail::RGBImageWriter::commitRGBImages(const std::vector<CByteImage>& images,
58  const armarx::core::time::DateTime& referenceTime)
59  {
60  std::vector<cv::Mat> mats;
61  for (const auto& i : images)
62  {
63  auto& m = mats.emplace_back();
64  if (i.type == CByteImage::ImageType::eRGB24)
65  {
66  m = cv::Mat(i.height, i.width, CV_8UC3);
67  }
68  else if (i.type == CByteImage::ImageType::eGrayScale)
69  {
70  m = cv::Mat(i.height, i.width, CV_8UC1);
71  }
72  else
73  {
74  throw armarx::LocalException("Could not convert a CByteImage to cv::Mat. Only RGB "
75  "and Grayscale are allowed.");
76  }
77 
78  ARMARX_CHECK(i.width == m.cols);
79  ARMARX_CHECK(i.height == m.rows);
80  ARMARX_CHECK(i.bytesPerPixel == m.elemSize());
81  std::memcpy(m.data, i.pixels, m.rows * m.cols * m.elemSize());
82  }
83  return commitRGBImages(mats, referenceTime);
84  }
85 
86  std::string
88  {
89  return "rgbWriter.";
90  }
91 
94  {
96  p.memoryName = "Vision";
97  p.coreSegmentName = "ImageRGB";
98  p.providerName = providerName;
99  return p;
100  }
101 
102  ImageWriter::ImageWriter(const std::string& providerName) : rgbWriter(providerName)
103  {
104  }
105 
106  void
108  {
109  rgbWriter.registerPropertyDefinitions(def);
110  }
111 
112  void
114  {
115  rgbWriter.connect(mns);
116  }
117 
118  bool
119  ImageWriter::commitRGBImages(const std::vector<cv::Mat>& images,
120  const armarx::core::time::DateTime& referenceTime)
121  {
122  return rgbWriter.commitRGBImages(images, referenceTime);
123  }
124 
125  bool
126  ImageWriter::commitRGBImages(const std::vector<CByteImage>& images,
127  const armarx::core::time::DateTime& referenceTime)
128  {
129  return rgbWriter.commitRGBImages(images, referenceTime);
130  }
131 
132 } // namespace visionx::armem_images::client
armarx::armem::client::util::SimpleWriterBase::Properties::providerName
std::string providerName
Definition: SimpleWriterBase.h:53
visionx::armem_images::client::ImageWriter::registerPropertyDefinitions
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Definition: ImageWriter.cpp:107
visionx::armem_images::client::ImageWriter::ImageWriter
ImageWriter(const std::string &providerName)
Definition: ImageWriter.cpp:102
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::armem::client::util::SimpleWriterBase::Properties::coreSegmentName
std::string coreSegmentName
Definition: SimpleWriterBase.h:52
armarx::armem::client::util::SimpleWriterBase::Properties
Definition: SimpleWriterBase.h:49
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::armem::client::util::SimpleWriterBase::registerPropertyDefinitions
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Definition: SimpleWriterBase.cpp:13
visionx::armem_images::client::ImageWriter::commitRGBImages
bool commitRGBImages(const std::vector< cv::Mat > &images, const armarx::core::time::DateTime &referenceTime)
Definition: ImageWriter.cpp:119
armarx::armem::client::util::SimpleWriterBase::memoryWriter
armem::client::Writer & memoryWriter()
Definition: SimpleWriterBase.cpp:51
visionx::armem_images::client::detail::RGBImageWriter::commitRGBImages
bool commitRGBImages(const std::vector< cv::Mat > &images, const armarx::core::time::DateTime &referenceTime)
Definition: ImageWriter.cpp:28
armarx::armem::client::util::SimpleWriterBase::Properties::memoryName
std::string memoryName
Definition: SimpleWriterBase.h:51
armarx::armem::client::util::SimpleWriterBase::connect
void connect(armarx::armem::client::MemoryNameSystem &mns)
Definition: SimpleWriterBase.cpp:33
visionx::armem_images::client::detail::RGBImageWriter::defaultProperties
Properties defaultProperties() const final
Definition: ImageWriter.cpp:93
armarx::armem::client::Writer::commit
CommitResult commit(const Commit &commit) const
Writes a Commit to the memory.
Definition: Writer.cpp:59
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::armem_images::client::ImageWriter::connect
virtual void connect(armarx::armem::client::MemoryNameSystem &mns)
Definition: ImageWriter.cpp:113
visionx::armem_images::client
Definition: ImageReader.cpp:29
armarx::armem::client::util::SimpleWriterBase::properties
const Properties & properties() const
Definition: SimpleWriterBase.cpp:57
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
ImageWriter.h
armarx::armem::CommitResult::allSuccess
bool allSuccess() const
Definition: Commit.cpp:64
visionx::armem_images::client::detail::RGBImageWriter::propertyPrefix
std::string propertyPrefix() const final
Definition: ImageWriter.cpp:87
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29