MultiImageProvider.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::MultiImageProvider
17 * @author Mirko Waechter
18 * @date 2019
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#include "MultiImageProvider.h"
24
26
31
34
35#include <Image/ImageProcessor.h>
36
37namespace armarx
38{
39
40
41 void
43 {
44 auto providersRaw = getProperty<std::string>("ImageProviders").getValue();
45 auto providerSplit = armarx::Split(providersRaw, ";", true, true);
46 ARMARX_CHECK_POSITIVE(providerSplit.size());
47 for (std::string providerStr : providerSplit)
48 {
49 auto split = armarx::Split(providerStr, ":", true, true);
51 auto name = split[0];
52 std::set<size_t> imageSelection;
53 if (split.size() >= 2)
54 {
55 auto imageSelectionVec = armarx::Split<size_t>(split.at(1), ",", true, true);
56 imageSelection =
57 std::set<size_t>(imageSelectionVec.begin(), imageSelectionVec.end());
58 }
59 else
60
61 {
62 ARMARX_LOG << "name" << name;
63 }
65 imageProviderData[name].imageSelection = imageSelection;
66 }
67 }
68
69 void
71 {
72 std::unique_lock lock(mutex);
73 visionx::ImageType imageDisplayType = visionx::tools::typeNameToImageType("rgb");
74 ::visionx::ImageDimension maxDim;
75 for (auto& pair : imageProviderData)
76 {
77 auto name = pair.first;
78 auto& data = pair.second;
79 auto& selection = data.imageSelection;
80 data.providerInfo = getImageProvider(name, imageDisplayType);
81 const auto& imageProviderInfo = data.providerInfo;
82 if (selection.empty())
83 {
84 for (int i = 0; i < imageProviderInfo.numberImages; i++)
85 {
86 selection.insert(i);
87 }
88 }
89 data.numImages = imageProviderInfo.numberImages;
90 if (imageProviderInfo.imageFormat != imageProviderInfo.imageFormat)
91 {
92 ARMARX_ERROR << "image format does not match.";
93 }
94 maxDim.width = std::max(imageProviderInfo.imageFormat.dimension.width, maxDim.width);
95 maxDim.height = std::max(imageProviderInfo.imageFormat.dimension.height, maxDim.height);
96
97 data.srcCameraImages.clear();
98
99 for (auto& index : selection)
100 {
101 (void)index; // Unused.
102 data.srcCameraImages.push_back(visionx::CByteImageUPtr(
103 new CByteImage(imageProviderInfo.imageFormat.dimension.width,
104 imageProviderInfo.imageFormat.dimension.height,
105 CByteImage::eRGB24)));
106 }
107 }
108 ARMARX_INFO << "max image dimension: " << maxDim.width << "x" << maxDim.height;
109 totalNumberOfImages = 0;
110 for (auto& pair : imageProviderData)
111 {
112 auto name = pair.first;
113 auto& data = pair.second;
114 auto& selection = data.imageSelection;
115 data.targetCameraImages.clear();
116 data.resize = maxDim.width != data.providerInfo.imageFormat.dimension.width ||
117 maxDim.height != data.providerInfo.imageFormat.dimension.height;
118 ARMARX_INFO << name
119 << ": image dimension: " << data.providerInfo.imageFormat.dimension.width
120 << "x" << data.providerInfo.imageFormat.dimension.height
121 << " resizing: " << data.resize;
122
123 for (auto& index : selection)
124 {
125 (void)index; // Unused.
126 data.targetCameraImages.push_back(visionx::CByteImageUPtr(
127 new CByteImage(maxDim.width, maxDim.height, CByteImage::eRGB24)));
128 totalNumberOfImages++;
129 }
130 }
131 enableResultImages(totalNumberOfImages, maxDim, imageDisplayType);
132 }
133
134 void
136 {
137 // std::unique_lock lock(mutex);
138 // for (int i = 0; i < numImages; i++)
139 // {
140 // delete cameraImages[i];
141 // }
142
143 // delete [] cameraImages;
144
145 // if (scaleFactorX > 0.0)
146 // {
147 // for (int i = 0; i < numImages; i++)
148 // {
149 // delete scaledCameraImages[i];
150 // }
151 // }
152 // delete [] scaledCameraImages;
153 }
154
155 void
157 {
158 std::unique_lock lock(mutex);
159 std::vector<CByteImage*> images;
160 for (auto& pair : imageProviderData)
161 {
162 const auto name = pair.first;
163 auto& data = pair.second;
164 // const auto& selection = data.imageSelection;
165 if (!isNewImageAvailable(name))
166 {
167 if (!waitForImages(name))
168 {
169 ARMARX_WARNING << "Timeout while waiting for images";
170 return;
171 }
172 }
173 armarx::MetaInfoSizeBasePtr info;
174 std::vector<CByteImage*> tmpImageVec;
175 for (auto& image : data.srcCameraImages)
176 {
177 tmpImageVec.push_back(image.get());
178 }
179 auto numImg = getImages(name, tmpImageVec.data(), info);
180
181 if (numImg != data.numImages)
182 {
184 << "Unable to transfer or read images: returned number of images "
185 << numImg << " expected: " << data.numImages;
186 return;
187 }
188 size_t i = 0;
189 for (auto& image : data.srcCameraImages)
190 {
191 if (!data.resize)
192 {
193 images.push_back(image.get());
194 }
195 else
196 {
197 auto targetImg = data.targetCameraImages.at(i).get();
198 ::ImageProcessor::Resize(image.get(), targetImg);
199 images.push_back(targetImg);
200 }
201 i++;
202 }
203 }
204 ARMARX_CHECK_EQUAL(totalNumberOfImages, images.size());
205 provideResultImages(images.data());
206 }
207
215
216} // namespace armarx
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
uint8_t index
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
Brief description of class MultiImageProvider.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
static std::string GetDefaultName()
void enableResultImages(int numberImages, ImageDimension imageDimension, ImageType imageType, const std::string &name="")
Enables visualization.
void usingImageProvider(std::string name)
Registers a delayed topic subscription and a delayed provider proxy retrieval which all will be avail...
bool waitForImages(int milliseconds=1000)
Wait for new images.
ImageProviderInfo getImageProvider(std::string name, ImageType destinationImageType=eRgb, bool waitForProxy=false)
Select an ImageProvider.
int getImages(CByteImage **ppImages)
Poll images from provider.
void provideResultImages(CByteImage **images, armarx::MetaInfoSizeBasePtr info=nullptr)
sends result images for visualization
#define ARMARX_CHECK_POSITIVE(number)
This macro evaluates whether number is positive (> 0) and if it turns out to be false it will throw a...
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_LOG
Definition Logging.h:165
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
ImageType typeNameToImageType(const std::string &imageTypeName)
Converts an image type name as string into an ImageType integer.
std::unique_ptr< CByteImage > CByteImageUPtr