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
29
30#include "DepthFromStereo.h"
31#include <Calibration/StereoCalibration.h>
32#include <Image/ImageProcessor.h>
33
34
35using namespace armarx;
36using namespace pcl;
37
38namespace visionx
39{
40
41
42 void
44 {
48 downsamplingRate = getProperty<int>("DownsamplingRate").getValue();
49 smoothDisparity = getProperty<bool>("SmoothDisparity").getValue();
50 }
51
52 void
59
60 void
67
68 void
75
76 void
80
81 void
85
86 void
90
91 void
95
96 MetaPointCloudFormatPtr
98 {
99 MetaPointCloudFormatPtr info = new MetaPointCloudFormat();
100 //info->frameId = getProperty<std::string>("frameId").getValue();
101 info->type = PointContentType::eColoredPoints;
102 info->capacity = 1600 * 1200 * sizeof(ColoredPoint3D); // + info->frameId.size();
103 info->size = info->capacity;
104 return info;
105 }
106
107 void
109 {
110 // set desired image provider
111 providerName = getProperty<std::string>("ImageProviderAdapterName").getValue();
112 usingImageProvider(providerName);
113 }
114
115 void
117 {
118 // connect to image provider
119 ARMARX_INFO << getName() << " connecting to " << providerName;
120 visionx::ImageProviderInfo imageProviderInfo = getImageProvider(providerName);
121 imageProviderPrx = getProxy<ImageProviderInterfacePrx>(providerName);
122 setImageFormat(imageProviderInfo.imageFormat.dimension,
123 imageProviderInfo.imageFormat.type,
124 imageProviderInfo.imageFormat.bpType);
125
126 StereoCalibrationProviderInterfacePrx calibrationProviderPrx1 =
127 StereoCalibrationProviderInterfacePrx::checkedCast(imageProviderPrx);
128 CapturingPointCloudAndImageAndStereoCalibrationProviderInterfacePrx
129 calibrationProviderPrx2 =
130 CapturingPointCloudAndImageAndStereoCalibrationProviderInterfacePrx::checkedCast(
131 imageProviderPrx);
132
133 if (calibrationProviderPrx1)
134 {
135 stereoCalibration =
136 visionx::tools::convert(calibrationProviderPrx1->getStereoCalibration());
137 imagesAreUndistorted = calibrationProviderPrx1->getImagesAreUndistorted();
138 }
139 else if (calibrationProviderPrx2)
140 {
141 stereoCalibration =
142 visionx::tools::convert(calibrationProviderPrx2->getStereoCalibration());
143 imagesAreUndistorted = calibrationProviderPrx2->getImagesAreUndistorted();
144 }
145 else
146 {
147 ARMARX_WARNING << "Image provider with name " << providerName
148 << " is not a StereoCalibrationProvider";
149 imagesAreUndistorted = true;
150 }
151
152 cameraImages = new CByteImage*[2];
153 cameraImages[0] = tools::createByteImage(imageProviderInfo);
154 cameraImages[1] = tools::createByteImage(imageProviderInfo);
155 width = imageProviderInfo.imageFormat.dimension.width;
156 height = imageProviderInfo.imageFormat.dimension.height;
157 cameraImagesGrey = new CByteImage*[2];
158 cameraImagesGrey[0] = new CByteImage(width, height, CByteImage::eGrayScale);
159 cameraImagesGrey[1] = new CByteImage(width, height, CByteImage::eGrayScale);
160 disparityImage = new CByteImage(width, height, CByteImage::eGrayScale);
161 disparityImageRGB = new CByteImage(cameraImages[0]);
162 resultImages = new CByteImage*[2];
163 resultImages[0] = cameraImages[0];
164 resultImages[1] = disparityImageRGB;
165 }
166
167 void
169 {
170 delete cameraImages[0];
171 delete cameraImages[1];
172 delete[] cameraImages;
173 delete cameraImagesGrey[0];
174 delete cameraImagesGrey[1];
175 delete[] cameraImagesGrey;
176 delete disparityImage;
177 delete disparityImageRGB;
178 delete[] resultImages;
179 }
180
181 void
183 {
184 setImageFormat(visionx::ImageDimension(width, height), visionx::eRgb);
186 }
187
188 MonocularCalibration
190 {
191 return visionx::tools::convert(*stereoCalibration->GetLeftCalibration());
192 }
193
194 bool
196 {
197 std::unique_lock lock(captureLock);
198
199 if (!waitForImages(8000))
200 {
201 ARMARX_IMPORTANT << "Timeout or error in wait for images";
202 return false;
203 }
204 else
205 {
206 // get images
207 int nNumberImages = ImageProcessor::getImages(cameraImages);
208 ARMARX_DEBUG << getName() << " got " << nNumberImages << " images";
209
210 pcl::PointCloud<PointXYZRGBA>::Ptr pointcloud(new pcl::PointCloud<PointXYZRGBA>());
211 ::ImageProcessor::ConvertImage(cameraImages[0], cameraImagesGrey[0]);
212 ::ImageProcessor::ConvertImage(cameraImages[1], cameraImagesGrey[1]);
213
214 ARMARX_DEBUG << getName() << "depthfromstereo::GetPointsFromDisparity";
216 cameraImages[1],
217 cameraImagesGrey[0],
218 cameraImagesGrey[1],
219 downsamplingRate,
220 *pointcloud,
221 disparityImage,
222 width,
223 height,
224 stereoCalibration,
225 imagesAreUndistorted,
226 smoothDisparity);
227
228 // provide point cloud
229 ARMARX_DEBUG << "providing pointcloud";
230 providePointCloud(pointcloud);
231
232 for (size_t j = 0; j < pointcloud->height; j++)
233 {
234 for (size_t i = 0; i < pointcloud->width; i++)
235 {
236 int idx = (j * pointcloud->width + i);
237 int value = pointcloud->points[idx].z;
238 // if(value <= max ...)
239
240 disparityImageRGB->pixels[3 * idx + 0] = value & 0xFF;
241 disparityImageRGB->pixels[3 * idx + 1] = (value >> 8) & 0xFF;
242 }
243 }
244
245 provideImages(resultImages);
246
247 ARMARX_DEBUG << "done";
248
249 return true;
250 }
251 }
252
259
260} // namespace visionx
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
#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.