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;
43 update.sentTime = armarx::core::time::DateTime::Now();
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
The memory name system (MNS) client.
CommitResult commit(const Commit &commit) const
Writes a Commit to the memory.
Definition Writer.cpp:68
Represents a point in time.
Definition DateTime.h:25
static DateTime Now()
Definition DateTime.cpp:51
virtual void connect(armarx::armem::client::MemoryNameSystem &mns)
ImageWriter(const std::string &providerName)
bool commitRGBImages(const std::vector< cv::Mat > &images, const armarx::core::time::DateTime &referenceTime)
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
bool commitRGBImages(const std::vector< cv::Mat > &images, const armarx::core::time::DateTime &referenceTime)
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
A bundle of updates to be sent to the memory.
Definition Commit.h:90