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
29
32
33#include <Image/ImageProcessor.h>
34
35namespace armarx
36{
37
38
39 void
41 {
42 auto providersRaw = getProperty<std::string>("ImageProviders").getValue();
43 auto providerSplit = armarx::Split(providersRaw, ";", true, true);
44 ARMARX_CHECK_POSITIVE(providerSplit.size());
45 for (std::string providerStr : providerSplit)
46 {
47 auto split = armarx::Split(providerStr, ":", true, true);
49 auto name = split[0];
50 std::set<size_t> imageSelection;
51 if (split.size() >= 2)
52 {
53 auto imageSelectionVec = armarx::Split<size_t>(split.at(1), ",", true, true);
54 imageSelection =
55 std::set<size_t>(imageSelectionVec.begin(), imageSelectionVec.end());
56 }
57 else
58
59 {
60 ARMARX_LOG << "name" << name;
61 }
63 imageProviderData[name].imageSelection = imageSelection;
64 }
65 }
66
67 void
69 {
70 std::unique_lock lock(mutex);
71 visionx::ImageType imageDisplayType = visionx::tools::typeNameToImageType("rgb");
72 ::visionx::ImageDimension maxDim;
73 for (auto& pair : imageProviderData)
74 {
75 auto name = pair.first;
76 auto& data = pair.second;
77 auto& selection = data.imageSelection;
78 data.providerInfo = getImageProvider(name, imageDisplayType);
79 const auto& imageProviderInfo = data.providerInfo;
80 if (selection.empty())
81 {
82 for (int i = 0; i < imageProviderInfo.numberImages; i++)
83 {
84 selection.insert(i);
85 }
86 }
87 data.numImages = imageProviderInfo.numberImages;
88 if (imageProviderInfo.imageFormat != imageProviderInfo.imageFormat)
89 {
90 ARMARX_ERROR << "image format does not match.";
91 }
92 maxDim.width = std::max(imageProviderInfo.imageFormat.dimension.width, maxDim.width);
93 maxDim.height = std::max(imageProviderInfo.imageFormat.dimension.height, maxDim.height);
94
95 data.srcCameraImages.clear();
96
97 for (auto& index : selection)
98 {
99 (void)index; // Unused.
100 data.srcCameraImages.push_back(visionx::CByteImageUPtr(
101 new CByteImage(imageProviderInfo.imageFormat.dimension.width,
102 imageProviderInfo.imageFormat.dimension.height,
103 CByteImage::eRGB24)));
104 }
105 }
106 ARMARX_INFO << "max image dimension: " << maxDim.width << "x" << maxDim.height;
107 totalNumberOfImages = 0;
108 for (auto& pair : imageProviderData)
109 {
110 auto name = pair.first;
111 auto& data = pair.second;
112 auto& selection = data.imageSelection;
113 data.targetCameraImages.clear();
114 data.resize = maxDim.width != data.providerInfo.imageFormat.dimension.width ||
115 maxDim.height != data.providerInfo.imageFormat.dimension.height;
116 ARMARX_INFO << name
117 << ": image dimension: " << data.providerInfo.imageFormat.dimension.width
118 << "x" << data.providerInfo.imageFormat.dimension.height
119 << " resizing: " << data.resize;
120
121 for (auto& index : selection)
122 {
123 (void)index; // Unused.
124 data.targetCameraImages.push_back(visionx::CByteImageUPtr(
125 new CByteImage(maxDim.width, maxDim.height, CByteImage::eRGB24)));
126 totalNumberOfImages++;
127 }
128 }
129 enableResultImages(totalNumberOfImages, maxDim, imageDisplayType);
130 }
131
132 void
134 {
135 // std::unique_lock lock(mutex);
136 // for (int i = 0; i < numImages; i++)
137 // {
138 // delete cameraImages[i];
139 // }
140
141 // delete [] cameraImages;
142
143 // if (scaleFactorX > 0.0)
144 // {
145 // for (int i = 0; i < numImages; i++)
146 // {
147 // delete scaledCameraImages[i];
148 // }
149 // }
150 // delete [] scaledCameraImages;
151 }
152
153 void
155 {
156 std::unique_lock lock(mutex);
157 std::vector<CByteImage*> images;
158 for (auto& pair : imageProviderData)
159 {
160 const auto name = pair.first;
161 auto& data = pair.second;
162 // const auto& selection = data.imageSelection;
163 if (!isNewImageAvailable(name))
164 {
165 if (!waitForImages(name))
166 {
167 ARMARX_WARNING << "Timeout while waiting for images";
168 return;
169 }
170 }
171 armarx::MetaInfoSizeBasePtr info;
172 std::vector<CByteImage*> tmpImageVec;
173 for (auto& image : data.srcCameraImages)
174 {
175 tmpImageVec.push_back(image.get());
176 }
177 auto numImg = getImages(name, tmpImageVec.data(), info);
178
179 if (numImg != data.numImages)
180 {
182 << "Unable to transfer or read images: returned number of images "
183 << numImg << " expected: " << data.numImages;
184 return;
185 }
186 size_t i = 0;
187 for (auto& image : data.srcCameraImages)
188 {
189 if (!data.resize)
190 {
191 images.push_back(image.get());
192 }
193 else
194 {
195 auto targetImg = data.targetCameraImages.at(i).get();
196 ::ImageProcessor::Resize(image.get(), targetImg);
197 images.push_back(targetImg);
198 }
199 i++;
200 }
201 }
202 ARMARX_CHECK_EQUAL(totalNumberOfImages, images.size());
203 provideResultImages(images.data());
204 }
205
212} // namespace armarx
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)
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
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