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 
35 using namespace armarx;
36 using namespace pcl;
37 
38 namespace visionx
39 {
40 
41 
42  void
43  StereoImagePointCloudProvider::onInitComponent()
44  {
45  ImageProcessor::onInitComponent();
46  CapturingPointCloudProvider::onInitComponent();
47  ImageProvider::onInitComponent();
48  downsamplingRate = getProperty<int>("DownsamplingRate").getValue();
49  smoothDisparity = getProperty<bool>("SmoothDisparity").getValue();
50  }
51 
52  void
53  StereoImagePointCloudProvider::onConnectComponent()
54  {
55  ImageProcessor::onConnectComponent();
56  CapturingPointCloudProvider::onConnectComponent();
57  ImageProvider::onConnectComponent();
58  }
59 
60  void
61  StereoImagePointCloudProvider::onDisconnectComponent()
62  {
63  ImageProcessor::onDisconnectComponent();
64  CapturingPointCloudProvider::onDisconnectComponent();
65  ImageProvider::onDisconnectComponent();
66  }
67 
68  void
69  StereoImagePointCloudProvider::onExitComponent()
70  {
71  ImageProvider::onExitComponent();
72  CapturingPointCloudProvider::onExitComponent();
73  ImageProcessor::onExitComponent();
74  }
75 
76  void
77  StereoImagePointCloudProvider::onInitCapturingPointCloudProvider()
78  {
79  }
80 
81  void
82  StereoImagePointCloudProvider::onStartCapture(float frameRate)
83  {
84  }
85 
86  void
87  StereoImagePointCloudProvider::onStopCapture()
88  {
89  }
90 
91  void
92  StereoImagePointCloudProvider::onExitCapturingPointCloudProvider()
93  {
94  }
95 
96  MetaPointCloudFormatPtr
97  StereoImagePointCloudProvider::getDefaultPointCloudFormat()
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
108  StereoImagePointCloudProvider::onInitImageProcessor()
109  {
110  // set desired image provider
111  providerName = getProperty<std::string>("ImageProviderAdapterName").getValue();
112  usingImageProvider(providerName);
113  }
114 
115  void
116  StereoImagePointCloudProvider::onConnectImageProcessor()
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
168  StereoImagePointCloudProvider::onExitImageProcessor()
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
182  StereoImagePointCloudProvider::onInitImageProvider()
183  {
184  setImageFormat(visionx::ImageDimension(width, height), visionx::eRgb);
185  setNumberImages(2);
186  }
187 
188  MonocularCalibration
189  StereoImagePointCloudProvider::getMonocularCalibration(const Ice::Current& c)
190  {
191  return visionx::tools::convert(*stereoCalibration->GetLeftCalibration());
192  }
193 
194  bool
195  StereoImagePointCloudProvider::doCapture()
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 
254  StereoImagePointCloudProvider::createPropertyDefinitions()
255  {
256  return PropertyDefinitionsPtr(
257  new StereoImagePointCloudProviderPropertyDefinitions(getConfigIdentifier()));
258  }
259 
260 } // namespace visionx
pcl
Definition: pcl_point_operators.cpp:3
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
visionx::ImageProviderInfo::imageFormat
ImageFormatInfo imageFormat
Image format struct that contains all necessary image information.
Definition: ImageProcessor.h:509
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
visionx::ImageProviderInfo
Definition: ImageProcessor.h:479
visionx::tools::convert
CByteImage::ImageType convert(const ImageType visionxImageType)
Converts a VisionX image type into an image type of IVT's ByteImage.
Definition: TypeMapping.cpp:97
visionx::StereoImagePointCloudProviderPropertyDefinitions
Definition: StereoImagePointCloudProvider.h:52
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
visionx::depthfromstereo::GetPointsFromDisparity
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)
Definition: DepthFromStereo.cpp:95
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
DepthFromStereo.h
IceUtil::Handle< class PropertyDefinitionContainer >
ImageUtil.h
TypeMapping.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
StereoImagePointCloudProvider.h