StereoImagePointCloudProvider.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::StereoImagePointCloudProvider
19 * @author David Schiebener (schiebener 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
26
28
31
32#include "DepthFromStereo.h"
33#include <Calibration/StereoCalibration.h>
34#include <Image/ImageProcessor.h>
35
36
37using namespace armarx;
38using namespace pcl;
39
40namespace visionx
41{
42
43
44 void
46 {
50 downsamplingRate = getProperty<int>("DownsamplingRate").getValue();
51 smoothDisparity = getProperty<bool>("SmoothDisparity").getValue();
52 }
53
54 void
61
62 void
69
70 void
77
78 void
82
83 void
87
88 void
92
93 void
97
98 MetaPointCloudFormatPtr
100 {
101 MetaPointCloudFormatPtr info = new MetaPointCloudFormat();
102 //info->frameId = getProperty<std::string>("frameId").getValue();
103 info->type = PointContentType::eColoredPoints;
104 info->capacity = 1600 * 1200 * sizeof(ColoredPoint3D); // + info->frameId.size();
105 info->size = info->capacity;
106 return info;
107 }
108
109 void
111 {
112 // set desired image provider
113 providerName = getProperty<std::string>("ImageProviderAdapterName").getValue();
114 usingImageProvider(providerName);
115 }
116
117 void
119 {
120 // connect to image provider
121 ARMARX_INFO << getName() << " connecting to " << providerName;
122 visionx::ImageProviderInfo imageProviderInfo = getImageProvider(providerName);
123 imageProviderPrx = getProxy<ImageProviderInterfacePrx>(providerName);
124 setImageFormat(imageProviderInfo.imageFormat.dimension,
125 imageProviderInfo.imageFormat.type,
126 imageProviderInfo.imageFormat.bpType);
127
128 StereoCalibrationProviderInterfacePrx calibrationProviderPrx1 =
129 StereoCalibrationProviderInterfacePrx::checkedCast(imageProviderPrx);
130 CapturingPointCloudAndImageAndStereoCalibrationProviderInterfacePrx
131 calibrationProviderPrx2 =
132 CapturingPointCloudAndImageAndStereoCalibrationProviderInterfacePrx::checkedCast(
133 imageProviderPrx);
134
135 if (calibrationProviderPrx1)
136 {
137 stereoCalibration =
138 visionx::tools::convert(calibrationProviderPrx1->getStereoCalibration());
139 imagesAreUndistorted = calibrationProviderPrx1->getImagesAreUndistorted();
140 }
141 else if (calibrationProviderPrx2)
142 {
143 stereoCalibration =
144 visionx::tools::convert(calibrationProviderPrx2->getStereoCalibration());
145 imagesAreUndistorted = calibrationProviderPrx2->getImagesAreUndistorted();
146 }
147 else
148 {
149 ARMARX_WARNING << "Image provider with name " << providerName
150 << " is not a StereoCalibrationProvider";
151 imagesAreUndistorted = true;
152 }
153
154 cameraImages = new CByteImage*[2];
155 cameraImages[0] = tools::createByteImage(imageProviderInfo);
156 cameraImages[1] = tools::createByteImage(imageProviderInfo);
157 width = imageProviderInfo.imageFormat.dimension.width;
158 height = imageProviderInfo.imageFormat.dimension.height;
159 cameraImagesGrey = new CByteImage*[2];
160 cameraImagesGrey[0] = new CByteImage(width, height, CByteImage::eGrayScale);
161 cameraImagesGrey[1] = new CByteImage(width, height, CByteImage::eGrayScale);
162 disparityImage = new CByteImage(width, height, CByteImage::eGrayScale);
163 disparityImageRGB = new CByteImage(cameraImages[0]);
164 resultImages = new CByteImage*[2];
165 resultImages[0] = cameraImages[0];
166 resultImages[1] = disparityImageRGB;
167 }
168
169 void
171 {
172 delete cameraImages[0];
173 delete cameraImages[1];
174 delete[] cameraImages;
175 delete cameraImagesGrey[0];
176 delete cameraImagesGrey[1];
177 delete[] cameraImagesGrey;
178 delete disparityImage;
179 delete disparityImageRGB;
180 delete[] resultImages;
181 }
182
183 void
185 {
186 setImageFormat(visionx::ImageDimension(width, height), visionx::eRgb);
188 }
189
190 MonocularCalibration
192 {
193 return visionx::tools::convert(*stereoCalibration->GetLeftCalibration());
194 }
195
196 bool
198 {
199 std::unique_lock lock(captureLock);
200
201 if (!waitForImages(8000))
202 {
203 ARMARX_IMPORTANT << "Timeout or error in wait for images";
204 return false;
205 }
206 else
207 {
208 // get images
209 int nNumberImages = ImageProcessor::getImages(cameraImages);
210 ARMARX_DEBUG << getName() << " got " << nNumberImages << " images";
211
212 pcl::PointCloud<PointXYZRGBA>::Ptr pointcloud(new pcl::PointCloud<PointXYZRGBA>());
213 ::ImageProcessor::ConvertImage(cameraImages[0], cameraImagesGrey[0]);
214 ::ImageProcessor::ConvertImage(cameraImages[1], cameraImagesGrey[1]);
215
216 ARMARX_DEBUG << getName() << "depthfromstereo::GetPointsFromDisparity";
218 cameraImages[1],
219 cameraImagesGrey[0],
220 cameraImagesGrey[1],
221 downsamplingRate,
222 *pointcloud,
223 disparityImage,
224 width,
225 height,
226 stereoCalibration,
227 imagesAreUndistorted,
228 smoothDisparity);
229
230 // provide point cloud
231 ARMARX_DEBUG << "providing pointcloud";
232 providePointCloud(pointcloud);
233
234 for (size_t j = 0; j < pointcloud->height; j++)
235 {
236 for (size_t i = 0; i < pointcloud->width; i++)
237 {
238 int idx = (j * pointcloud->width + i);
239 int value = pointcloud->points[idx].z;
240 // if(value <= max ...)
241
242 disparityImageRGB->pixels[3 * idx + 0] = value & 0xFF;
243 disparityImageRGB->pixels[3 * idx + 1] = (value >> 8) & 0xFF;
244 }
245 }
246
247 provideImages(resultImages);
248
249 ARMARX_DEBUG << "done";
250
251 return true;
252 }
253 }
254
261
262
263std::string
265{
266 return "StereoImagePointCloudProvider";
267}
268
269std::string
274
275
277} // namespace visionx
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
constexpr T c
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
virtual void onExitComponent()
Hook for subclass.
virtual void onDisconnectComponent()
Hook for subclass.
virtual void onConnectComponent()=0
Pure virtual hook for the subclass.
std::string getName() const
Retrieve name of object.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
virtual void onInitComponent()=0
Pure virtual hook for the subclass.
void onInitComponent() override
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.
void onDisconnectComponent() override
int getImages(CByteImage **ppImages)
Poll images from provider.
void onConnectComponent() override
void onExitComponent() override
ImageFormatInfo imageFormat
Image format struct that contains all necessary image information.
void onInitComponent() override
void onDisconnectComponent() override
Hook for subclass.
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
void provideImages(void **inputBuffers, const IceUtil::Time &imageTimestamp=IceUtil::Time())
send images raw.
void onConnectComponent() override
void setNumberImages(int numberImages)
Sets the number of images on each capture.
void onExitComponent() override
void providePointCloud(PointCloudPtrT pointCloudPtr)
offer the new point cloud.
void onConnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component is fully initia...
void onDisconnectComponent() override
Hook for subclass.
void onExitImageProcessor() override
Exit the ImapeProcessor component.
MetaPointCloudFormatPtr getDefaultPointCloudFormat() override
default point cloud format used to initialize shared memory
void onInitImageProcessor() override
Setup the vision component.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
visionx::MonocularCalibration getMonocularCalibration(const ::Ice::Current &c=Ice::emptyCurrent) override
std::string getDefaultName() const override
Retrieve default name of component.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#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.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
void GetPointsFromDisparity(const CByteImage *pImageLeftColor, const CByteImage *pImageRightColor, const CByteImage *pImageLeftGrey, const CByteImage *pImageRightGrey, const int nDisparityPointDistance, pcl::PointCloud< pcl::PointXYZRGBA > &aPointsFromDisparity, CByteImage *pDisparityImage, const int imageWidth, const int imageHeight, CStereoCalibration *pStereoCalibration, bool imagesAreUndistorted, const bool smoothDisparities)
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
CByteImage::ImageType convert(const ImageType visionxImageType)
Converts a VisionX image type into an image type of IVT's ByteImage.
ArmarX headers.