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
27
28using namespace armarx;
29using namespace visionx;
30
31void
33{
34 numImages = getProperty<int>("numImages").getValue();
35 height = getProperty<int>("height").getValue();
36 width = getProperty<int>("width").getValue();
37
38 colorMask = getProperty<Eigen::Vector3i>("colorMask").getValue();
39
40 ARMARX_INFO << "color mask is r:" << colorMask(0) << " g:" << colorMask(1)
41 << " b:" << colorMask(2);
42
43 setImageFormat(visionx::ImageDimension(width, height), visionx::eRgb);
44 setNumberImages(numImages);
45
46 bytesPerPixel = getImageFormat().bytesPerPixel;
47
48 pollImagesTask =
49 new PeriodicTask<ResultImageFuser>(this, &ResultImageFuser::pollImageProviders, 50);
50}
51
52void
56
57void
59{
60 pollImagesTask->start();
61
62 if (imageSources.size() == 0)
63 {
64 std::vector<std::string> imageProviders =
65 getProperty<std::vector<std::string>>("imageProviders").getValue();
66 setResultImageProviders(imageProviders);
67 }
68}
69
70void
72{
73 pollImagesTask->stop();
74}
75
76bool
78{
79 usleep(10000);
80
81 const size_t imageSize = width * height * bytesPerPixel;
82 CByteImage* resultImage = visionx::tools::createByteImage(getImageFormat(), visionx::eRgb);
83
84 bool imageCaptured = false;
85
86 for (auto& any : imageSources)
87 {
88
89 std::string proxyName = any.first;
90
91 if (!imageAvailable[proxyName])
92 {
93 continue;
94 }
95 else
96 {
97 imageAvailable[proxyName] = false;
98 }
99
100 ImageProviderInterfacePrx providerPrx;
101
102 try
103 {
104 providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
105 }
106 catch (...)
107 {
108 ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
109 continue;
110 }
111
112
113 CByteImage** providerImages = any.second;
114 for (int n = 0; n < numImages; n++)
115 {
116 CByteImage* image = providerImages[n];
117
118 for (int j = 0; j < height; j++)
119 {
120 for (int i = 0; i < width; i++)
121 {
122
123 int r = image->pixels[3 * (j * width + i) + 0];
124 int g = image->pixels[3 * (j * width + i) + 1];
125 int b = image->pixels[3 * (j * width + i) + 2];
126
127 if (!(r == colorMask(0) && g == colorMask(1) && b == colorMask(2)))
128 {
129 resultImage->pixels[3 * (j * width + i) + 0] = r;
130 resultImage->pixels[3 * (j * width + i) + 1] = g;
131 resultImage->pixels[3 * (j * width + i) + 2] = b;
132 }
133 }
134 }
135 }
136
137 imageCaptured = true;
138 }
139
140 {
142
143 for (int i = 0; i < numImages; i++)
144 {
145 memcpy(ppImages[i], resultImage->pixels, imageSize);
146 }
147 }
148
149 return imageCaptured;
150}
151
152void
153ResultImageFuser::setResultImageProviders(std::vector<std::string> imageProviders)
154{
155 imageSources.clear();
156 // removeProxyDependency()
157
158 for (std::string& proxyName : imageProviders)
159 {
160
161 ImageProviderInterfacePrx providerPrx;
162
163 try
164 {
165 providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
166 }
167 catch (...)
168 {
169 ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
170 continue;
171 }
172
173 const int providerNumImages = providerPrx->getNumberImages();
174 ImageFormatInfo imageFormat = providerPrx->getImageFormat();
175
176 assert(imageFormat.dimension.width == width);
177 assert(imageFormat.dimension.height == height);
178 assert(imageFormat.bytesPerPixel == bytesPerPixel);
179 assert(numImages <= providerNumImages);
180
181 CByteImage** images = new CByteImage*[providerNumImages];
182
183 for (int n = 0; n < numImages; n++)
184 {
185 // todo visionx::eRgb might be wrong
186 images[n] = visionx::tools::createByteImage(imageFormat, visionx::eRgb);
187 }
188
189 imageSources[proxyName] = images;
190 imageAvailable[proxyName] = false;
191 }
192}
193
194void
195ResultImageFuser::pollImageProviders()
196{
197 std::unique_lock lock(imageMutex);
198
199 for (auto& any : imageSources)
200 {
201 std::string proxyName = any.first;
202
203 ImageProviderInterfacePrx providerPrx;
204
205 try
206 {
207 providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
208 }
209 catch (...)
210 {
211 ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
212 continue;
213 }
214
215 CByteImage** bufferImages = any.second;
216 ImageFormatInfo imageFormat = providerPrx->getImageFormat();
217
218 armarx::Blob images = providerPrx->getImages();
219
220 size_t imageSize = width * height * imageFormat.bytesPerPixel;
221
222 for (int i = 0; i < numImages; i++)
223 {
224 memcpy(bufferImages[i]->pixels, &images[imageSize * i], imageSize);
225 }
226
227 imageAvailable[proxyName] = true;
228 }
229}
230
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...
void onStartCapture(float frameRate) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onExitCapturingImageProvider() override
void onInitCapturingImageProvider() override
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.