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 
35 namespace 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  }
62  usingImageProvider(name);
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 
208  {
211  }
212 } // namespace armarx
armarx::MultiImageProvider::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: MultiImageProvider.cpp:207
index
uint8_t index
Definition: EtherCATFrame.h:59
ArmarXManager.h
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:36
visionx::ImageProcessor::getImageProvider
ImageProviderInfo getImageProvider(std::string name, ImageType destinationImageType=eRgb, bool waitForProxy=false)
Select an ImageProvider.
Definition: ImageProcessor.cpp:167
armarx::MultiImageProviderPropertyDefinitions
Definition: MultiImageProvider.h:38
visionx::CByteImageUPtr
std::unique_ptr< CByteImage > CByteImageUPtr
Definition: ImageProvider.h:56
armarx::MultiImageProvider::onConnectImageProcessor
void onConnectImageProcessor() override
Definition: MultiImageProvider.cpp:68
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
visionx::ImageProcessor::isNewImageAvailable
bool isNewImageAvailable()
Definition: ImageProcessor.cpp:372
visionx::ImageProcessor::usingImageProvider
void usingImageProvider(std::string name)
Registers a delayed topic subscription and a delayed provider proxy retrieval which all will be avail...
Definition: ImageProcessor.cpp:128
armarx::MultiImageProvider::onExitImageProcessor
void onExitImageProcessor() override
Definition: MultiImageProvider.cpp:133
ArmarXObjectScheduler.h
max
T max(T t1, T t2)
Definition: gdiam.h:51
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
visionx::ImageProcessor::getImages
int getImages(CByteImage **ppImages)
Poll images from provider.
Definition: ImageProcessor.cpp:395
ARMARX_CHECK_POSITIVE
#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...
Definition: ExpressionException.h:145
ARMARX_LOG
#define ARMARX_LOG
Definition: Logging.h:165
ExpressionException.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::ImageProcessor::enableResultImages
void enableResultImages(int numberImages, ImageDimension imageDimension, ImageType imageType, const std::string &name="")
Enables visualization.
Definition: ImageProcessor.cpp:251
visionx::tools::typeNameToImageType
ImageType typeNameToImageType(const std::string &imageTypeName)
Converts an image type name as string into an ImageType integer.
Definition: TypeMapping.cpp:42
ImageUtil.h
visionx::ImageProcessor::provideResultImages
void provideResultImages(CByteImage **images, armarx::MetaInfoSizeBasePtr info=nullptr)
sends result images for visualization
Definition: ImageProcessor.cpp:274
StringHelperTemplates.h
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:99
TypeMapping.h
armarx::MultiImageProvider::onInitImageProcessor
void onInitImageProcessor() override
Definition: MultiImageProvider.cpp:40
ARMARX_CHECK_EQUAL
#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...
Definition: ExpressionException.h:130
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
MultiImageProvider.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38
visionx::ImageProcessor::waitForImages
bool waitForImages(int milliseconds=1000)
Wait for new images.
Definition: ImageProcessor.cpp:309
armarx::MultiImageProvider::process
void process() override
Definition: MultiImageProvider.cpp:154