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 
25 
30 
33 
34 #include <Image/ImageProcessor.h>
35 
36 namespace armarx
37 {
38 
39 
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 = std::set<size_t>(imageSelectionVec.begin(), imageSelectionVec.end());
55  }
56  else
57 
58  {
59  ARMARX_LOG << "name" << name;
60  }
61  usingImageProvider(name);
62  imageProviderData[name].imageSelection = imageSelection;
63  }
64  }
65 
67  {
68  std::unique_lock lock(mutex);
69  visionx::ImageType imageDisplayType = visionx::tools::typeNameToImageType("rgb");
70  ::visionx::ImageDimension maxDim;
71  for (auto& pair : imageProviderData)
72  {
73  auto name = pair.first;
74  auto& data = pair.second;
75  auto& selection = data.imageSelection;
76  data.providerInfo = getImageProvider(name, imageDisplayType);
77  const auto& imageProviderInfo = data.providerInfo;
78  if (selection.empty())
79  {
80  for (int i = 0 ; i < imageProviderInfo.numberImages; i++)
81  {
82  selection.insert(i);
83  }
84  }
85  data.numImages = imageProviderInfo.numberImages;
86  if (imageProviderInfo.imageFormat != imageProviderInfo.imageFormat)
87  {
88  ARMARX_ERROR << "image format does not match.";
89  }
90  maxDim.width = std::max(imageProviderInfo.imageFormat.dimension.width, maxDim.width);
91  maxDim.height = std::max(imageProviderInfo.imageFormat.dimension.height, maxDim.height);
92 
93  data.srcCameraImages.clear();
94 
95  for (auto& index : selection)
96  {
97  (void) index; // Unused.
98  data.srcCameraImages.push_back(visionx::CByteImageUPtr(new CByteImage(imageProviderInfo.imageFormat.dimension.width,
99  imageProviderInfo.imageFormat.dimension.height,
100  CByteImage::eRGB24)));
101  }
102 
103  }
104  ARMARX_INFO << "max image dimension: " << maxDim.width << "x" << maxDim.height;
105  totalNumberOfImages = 0;
106  for (auto& pair : imageProviderData)
107  {
108  auto name = pair.first;
109  auto& data = pair.second;
110  auto& selection = data.imageSelection;
111  data.targetCameraImages.clear();
112  data.resize = maxDim.width != data.providerInfo.imageFormat.dimension.width
113  || maxDim.height != data.providerInfo.imageFormat.dimension.height;
114  ARMARX_INFO << name << ": image dimension: " << data.providerInfo.imageFormat.dimension.width << "x" << data.providerInfo.imageFormat.dimension.height << " resizing: " << data.resize;
115 
116  for (auto& index : selection)
117  {
118  (void) index; // Unused.
119  data.targetCameraImages.push_back(visionx::CByteImageUPtr(new CByteImage(maxDim.width, maxDim.height, CByteImage::eRGB24)));
120  totalNumberOfImages++;
121  }
122  }
123  enableResultImages(totalNumberOfImages, maxDim, imageDisplayType);
124  }
125 
127  {
128  // std::unique_lock lock(mutex);
129  // for (int i = 0; i < numImages; i++)
130  // {
131  // delete cameraImages[i];
132  // }
133 
134  // delete [] cameraImages;
135 
136  // if (scaleFactorX > 0.0)
137  // {
138  // for (int i = 0; i < numImages; i++)
139  // {
140  // delete scaledCameraImages[i];
141  // }
142  // }
143  // delete [] scaledCameraImages;
144  }
145 
147  {
148  std::unique_lock lock(mutex);
149  std::vector<CByteImage*> images;
150  for (auto& pair : imageProviderData)
151  {
152  const auto name = pair.first;
153  auto& data = pair.second;
154  // const auto& selection = data.imageSelection;
155  if (!isNewImageAvailable(name))
156  {
157  if (!waitForImages(name))
158  {
159  ARMARX_WARNING << "Timeout while waiting for images";
160  return;
161  }
162  }
163  armarx::MetaInfoSizeBasePtr info;
164  std::vector<CByteImage*> tmpImageVec;
165  for (auto& image : data.srcCameraImages)
166  {
167  tmpImageVec.push_back(image.get());
168  }
169  auto numImg = getImages(name, tmpImageVec.data(), info);
170 
171  if (numImg != data.numImages)
172  {
173  ARMARX_WARNING << deactivateSpam(5) << "Unable to transfer or read images: returned number of images " << numImg << " expected: " << data.numImages;
174  return;
175  }
176  size_t i = 0;
177  for (auto& image : data.srcCameraImages)
178  {
179  if (!data.resize)
180  {
181  images.push_back(image.get());
182  }
183  else
184  {
185  auto targetImg = data.targetCameraImages.at(i).get();
186  ::ImageProcessor::Resize(image.get(), targetImg);
187  images.push_back(targetImg);
188  }
189  i++;
190  }
191  }
192  ARMARX_CHECK_EQUAL(totalNumberOfImages, images.size());
193  provideResultImages(images.data());
194  }
195 
196 
197 
198 
200  {
203  }
204 }
armarx::MultiImageProvider::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: MultiImageProvider.cpp:199
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:35
visionx::ImageProcessor::getImageProvider
ImageProviderInfo getImageProvider(std::string name, ImageType destinationImageType=eRgb, bool waitForProxy=false)
Select an ImageProvider.
Definition: ImageProcessor.cpp:152
armarx::MultiImageProviderPropertyDefinitions
Definition: MultiImageProvider.h:42
visionx::CByteImageUPtr
std::unique_ptr< CByteImage > CByteImageUPtr
Definition: ImageProvider.h:57
armarx::MultiImageProvider::onConnectImageProcessor
void onConnectImageProcessor() override
Definition: MultiImageProvider.cpp:66
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
visionx::ImageProcessor::isNewImageAvailable
bool isNewImageAvailable()
Definition: ImageProcessor.cpp:331
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:117
armarx::MultiImageProvider::onExitImageProcessor
void onExitImageProcessor() override
Definition: MultiImageProvider.cpp:126
ArmarXObjectScheduler.h
max
T max(T t1, T t2)
Definition: gdiam.h:48
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
visionx::ImageProcessor::getImages
int getImages(CByteImage **ppImages)
Poll images from provider.
Definition: ImageProcessor.cpp:351
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:163
ExpressionException.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::ImageProcessor::enableResultImages
void enableResultImages(int numberImages, ImageDimension imageDimension, ImageType imageType, const std::string &name="")
Enables visualization.
Definition: ImageProcessor.cpp:227
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:245
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:92
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:186
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
MultiImageProvider.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36
visionx::ImageProcessor::waitForImages
bool waitForImages(int milliseconds=1000)
Wait for new images.
Definition: ImageProcessor.cpp:275
armarx::MultiImageProvider::process
void process() override
Definition: MultiImageProvider.cpp:146