ResultImageFuser.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package VisionX::ArmarXObjects::ResultImageFuser
19 * @author Markus Grotz ( markus dot grotz at kit dot edu )
20 * @date 2015
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#include "ResultImageFuser.h"
26
28
29
30using namespace armarx;
31using namespace visionx;
32
33void
35{
36 numImages = getProperty<int>("numImages").getValue();
37 height = getProperty<int>("height").getValue();
38 width = getProperty<int>("width").getValue();
39
40 colorMask = getProperty<Eigen::Vector3i>("colorMask").getValue();
41
42 ARMARX_INFO << "color mask is r:" << colorMask(0) << " g:" << colorMask(1)
43 << " b:" << colorMask(2);
44
45 setImageFormat(visionx::ImageDimension(width, height), visionx::eRgb);
46 setNumberImages(numImages);
47
48 bytesPerPixel = getImageFormat().bytesPerPixel;
49
50 pollImagesTask =
51 new PeriodicTask<ResultImageFuser>(this, &ResultImageFuser::pollImageProviders, 50);
52}
53
54void
58
59void
61{
62 pollImagesTask->start();
63
64 if (imageSources.size() == 0)
65 {
66 std::vector<std::string> imageProviders =
67 getProperty<std::vector<std::string>>("imageProviders").getValue();
68 setResultImageProviders(imageProviders);
69 }
70}
71
72void
74{
75 pollImagesTask->stop();
76}
77
78bool
80{
81 usleep(10000);
82
83 const size_t imageSize = width * height * bytesPerPixel;
84 CByteImage* resultImage = visionx::tools::createByteImage(getImageFormat(), visionx::eRgb);
85
86 bool imageCaptured = false;
87
88 for (auto& any : imageSources)
89 {
90
91 std::string proxyName = any.first;
92
93 if (!imageAvailable[proxyName])
94 {
95 continue;
96 }
97 else
98 {
99 imageAvailable[proxyName] = false;
100 }
101
102 ImageProviderInterfacePrx providerPrx;
103
104 try
105 {
106 providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
107 }
108 catch (...)
109 {
110 ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
111 continue;
112 }
113
114
115 CByteImage** providerImages = any.second;
116 for (int n = 0; n < numImages; n++)
117 {
118 CByteImage* image = providerImages[n];
119
120 for (int j = 0; j < height; j++)
121 {
122 for (int i = 0; i < width; i++)
123 {
124
125 int r = image->pixels[3 * (j * width + i) + 0];
126 int g = image->pixels[3 * (j * width + i) + 1];
127 int b = image->pixels[3 * (j * width + i) + 2];
128
129 if (!(r == colorMask(0) && g == colorMask(1) && b == colorMask(2)))
130 {
131 resultImage->pixels[3 * (j * width + i) + 0] = r;
132 resultImage->pixels[3 * (j * width + i) + 1] = g;
133 resultImage->pixels[3 * (j * width + i) + 2] = b;
134 }
135 }
136 }
137 }
138
139 imageCaptured = true;
140 }
141
142 {
144
145 for (int i = 0; i < numImages; i++)
146 {
147 memcpy(ppImages[i], resultImage->pixels, imageSize);
148 }
149 }
150
151 return imageCaptured;
152}
153
154void
155ResultImageFuser::setResultImageProviders(std::vector<std::string> imageProviders)
156{
157 imageSources.clear();
158 // removeProxyDependency()
159
160 for (std::string& proxyName : imageProviders)
161 {
162
163 ImageProviderInterfacePrx providerPrx;
164
165 try
166 {
167 providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
168 }
169 catch (...)
170 {
171 ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
172 continue;
173 }
174
175 const int providerNumImages = providerPrx->getNumberImages();
176 ImageFormatInfo imageFormat = providerPrx->getImageFormat();
177
178 assert(imageFormat.dimension.width == width);
179 assert(imageFormat.dimension.height == height);
180 assert(imageFormat.bytesPerPixel == bytesPerPixel);
181 assert(numImages <= providerNumImages);
182
183 CByteImage** images = new CByteImage*[providerNumImages];
184
185 for (int n = 0; n < numImages; n++)
186 {
187 // todo visionx::eRgb might be wrong
188 images[n] = visionx::tools::createByteImage(imageFormat, visionx::eRgb);
189 }
190
191 imageSources[proxyName] = images;
192 imageAvailable[proxyName] = false;
193 }
194}
195
196void
197ResultImageFuser::pollImageProviders()
198{
199 std::unique_lock lock(imageMutex);
200
201 for (auto& any : imageSources)
202 {
203 std::string proxyName = any.first;
204
205 ImageProviderInterfacePrx providerPrx;
206
207 try
208 {
209 providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
210 }
211 catch (...)
212 {
213 ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
214 continue;
215 }
216
217 CByteImage** bufferImages = any.second;
218 ImageFormatInfo imageFormat = providerPrx->getImageFormat();
219
220 armarx::Blob images = providerPrx->getImages();
221
222 size_t imageSize = width * height * imageFormat.bytesPerPixel;
223
224 for (int i = 0; i < numImages; i++)
225 {
226 memcpy(bufferImages[i]->pixels, &images[imageSize * i], imageSize);
227 }
228
229 imageAvailable[proxyName] = true;
230 }
231}
232
238
239std::string
241{
242 return "ResultImageFuser";
243}
244
245std::string
247{
248 return GetDefaultName();
249}
250
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
The periodic task executes one thread method repeatedly using the time period specified in the constr...
A brief description.
void onStartCapture(float frameRate) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onExitCapturingImageProvider() override
static std::string GetDefaultName()
void onInitCapturingImageProvider() override
std::string getDefaultName() const override
Retrieve default name of component.
armarx::SharedMemoryScopedWriteLockPtr getScopedWriteLock()
Retrieve scoped lock for writing to the memory.
ImageFormatInfo getImageFormat(const Ice::Current &c=Ice::emptyCurrent) override
Returns the entire image format info struct via Ice.
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
void setNumberImages(int numberImages)
Sets the number of images on each capture.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
constexpr auto n() noexcept
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
ArmarX headers.