ImageSequenceProvider.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::Component
19  * @author Jan Issac (jan dot issac at gmx dot net)
20  * @date 2011
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include <exception>
26 
27 #include <Ice/Properties.h>
28 
31 
32 // VisionXCore
35 
36 // VisionXTools
40 
41 #include "ImageSequenceProvider.h"
42 #include <Calibration/Calibration.h>
43 
44 using namespace armarx;
45 
46 namespace visionx
47 {
48  void
49  ImageSequenceProvider::onInitCapturingImageProvider()
50  {
51  // REQUIRED properties
52  filePathLeft = getProperty<std::string>("PathLeft").getValue();
53 
54  // OPTIONAL properties
55  filePathRight = getProperty<std::string>("PathRight").getValue();
56 
57  // image format
58  setImageFormat(visionx::ImageDimension(getProperty<ImageDimension>("ImageSize").getValue()),
59  visionx::eRgb);
60  setImageSyncMode(eFpsSynchronization);
61  frameRate = getProperty<float>("FrameRate").getValue();
62 
63  // open the image files
64  bitmapSequenceCapture = NULL;
65  images = NULL;
66  OpenImageFiles();
67  loadNewImageAtNextCapture = true;
68 
69  // stereo calibration
70  std::string calibrationFileName = getProperty<std::string>("CalibrationFile").getValue();
71  if (!calibrationFileName.empty())
72  {
73  std::string fullCalibrationFileName;
74 
75  if (!ArmarXDataPath::getAbsolutePath(calibrationFileName, fullCalibrationFileName))
76  {
77  ARMARX_ERROR << "Could not find camera calibration file in ArmarXDataPath: "
78  << calibrationFileName;
79  }
80 
81  if (!ivtStereoCalibration.LoadCameraParameters(fullCalibrationFileName.c_str(), true))
82  {
83  ARMARX_ERROR << "Error loading camera calibration file: "
84  << fullCalibrationFileName;
85  }
86  }
87 
88  stereoCalibration = visionx::tools::convert(ivtStereoCalibration);
89 
90  repeatImageCount = getProperty<int>("RepeatImageCount");
91  }
92 
93  void
94  ImageSequenceProvider::onExitCapturingImageProvider()
95  {
96  if (images != NULL)
97  {
98  for (int i = 0; i < getNumberImages(); i++)
99  {
100  delete images[i];
101  delete resizedImages[i];
102  }
103 
104  delete[] images;
105  delete[] resizedImages;
106 
107  images = NULL;
108  }
109  }
110 
111  void
112  ImageSequenceProvider::onStartCapture(float frameRate)
113  {
114  if (!bitmapSequenceCapture->OpenCamera())
115  {
117  "Opening bitmap sequence \"" + filePathLeft + "\" failed.");
118  }
119  else
120  {
121  loadNewImageAtNextCapture = true;
122  currentRepeatCount = repeatImageCount - 1;
123  }
124  }
125 
126  void
127  ImageSequenceProvider::onStopCapture()
128  {
129  bitmapSequenceCapture->CloseCamera();
130  }
131 
132  bool
133  ImageSequenceProvider::capture(void** ppImageBuffers)
134  {
135  bool succeeded = true;
136 
137  if (loadNewImageAtNextCapture)
138  {
139  std::unique_lock lock(captureMutex);
140 
141  ARMARX_VERBOSE << "Loading next image";
142 
143  loadNewImageAtNextCapture = getProperty<bool>("LoadNextImageAutomatically").getValue();
144 
145  ++currentRepeatCount;
146  if (currentRepeatCount == repeatImageCount)
147  {
148  succeeded = bitmapSequenceCapture->CaptureImage(images);
149  currentRepeatCount = 0;
150 
151  if (!succeeded)
152  {
153  bitmapSequenceCapture->Rewind();
154  succeeded = bitmapSequenceCapture->CaptureImage(images);
155  }
156  }
157  }
158 
159  if (succeeded)
160  {
161  armarx::SharedMemoryScopedWriteLockPtr lock = getScopedWriteLock();
162 
163  for (int i = 0; i < getNumberImages(); i++)
164  {
165  ::ImageProcessor::Resize(images[i], resizedImages[i]);
166 
167  memcpy(ppImageBuffers[i],
168  resizedImages[i]->pixels,
169  getImageFormat().dimension.width * getImageFormat().dimension.height *
170  getImageFormat().bytesPerPixel);
171  }
172  }
173 
174  updateTimestamp(DateTime::Now());
175 
176  return succeeded;
177  }
178 
179  StereoCalibration
180  ImageSequenceProvider::getStereoCalibration(const Ice::Current& c)
181  {
182  return stereoCalibration;
183  }
184 
185  bool
186  ImageSequenceProvider::getImagesAreUndistorted(const Ice::Current& c)
187  {
188  return getProperty<bool>("ImagesAreUndistorted").getValue();
189  }
190 
191  void
192  ImageSequenceProvider::setImageFilePath(const std::string& imageFilePathLeft,
193  const std::string& imageFilePathRight,
194  const Ice::Current& c)
195  {
196  filePathLeft = imageFilePathLeft;
197  filePathRight = imageFilePathRight;
198 
199  OpenImageFiles();
200  }
201 
202  void
203  ImageSequenceProvider::OpenImageFiles()
204  {
205  std::unique_lock lock(captureMutex);
206 
207  bool isStereoMode = filePathRight.length() > 0;
208 
209  std::string fullFilePathLeft, fullFilePathRight;
210 
211  if (!ArmarXDataPath::getAbsolutePath(filePathLeft, fullFilePathLeft))
212  {
213  ARMARX_ERROR << "Could not find left image sequence file in ArmarXDataPath: "
214  << filePathLeft;
215  }
216 
217  if (isStereoMode && !ArmarXDataPath::getAbsolutePath(filePathRight, fullFilePathRight))
218  {
219  ARMARX_ERROR << "Could not find right image sequence file in ArmarXDataPath: "
220  << filePathRight;
221  }
222 
223  if (bitmapSequenceCapture)
224  {
225  delete bitmapSequenceCapture;
226  }
227 
228  bitmapSequenceCapture = new CBitmapSequenceCapture(
229  fullFilePathLeft.c_str(), (isStereoMode ? fullFilePathRight.c_str() : NULL));
230 
231  // create images
232  int width = 0;
233  int height = 0;
234 
235  if (bitmapSequenceCapture->OpenCamera())
236  {
237  width = bitmapSequenceCapture->GetWidth();
238  height = bitmapSequenceCapture->GetHeight();
239 
240  loadNewImageAtNextCapture = true;
241  //bitmapSequenceCapture->CloseCamera();
242  }
243  else
244  {
245  ARMARX_ERROR << "Could not open bitmap sequence";
246  }
247 
248  if (images)
249  {
250  for (int i = 0; i < getNumberImages(); i++)
251  {
252  delete images[i];
253  delete resizedImages[i];
254  }
255 
256  delete[] images;
257  delete[] resizedImages;
258  }
259 
260 
261  setNumberImages((isStereoMode ? 2 : 1));
262 
263 
264  images = new CByteImage*[getNumberImages()];
265 
266  for (int i = 0; i < getNumberImages(); i++)
267  {
268  images[i] = new CByteImage(width, height, CByteImage::eRGB24);
269  }
270 
271  resizedImages = new CByteImage*[getNumberImages()];
272 
273  for (int i = 0; i < getNumberImages(); i++)
274  {
275  resizedImages[i] =
276  visionx::tools::createByteImage(getImageFormat(), getImageFormat().type);
277  }
278  }
279 
280  void
281  ImageSequenceProvider::loadNextImage(const Ice::Current& c)
282  {
283  std::unique_lock lock(captureMutex);
284 
285  loadNewImageAtNextCapture = true;
286  }
287 
288 } // namespace visionx
StartingCaptureFailedException.h
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:51
DateTime.h
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::SharedMemoryScopedWriteLockPtr
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
Definition: SharedMemoryProvider.h:47
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
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::exceptions::user::StartingCaptureFailedException
Definition: StartingCaptureFailedException.h:31
FrameRateNotSupportedException.h
InvalidFrameRateException.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
ImageSequenceProvider.h
ImageUtil.h
TypeMapping.h
armarx::ArmarXDataPath::getAbsolutePath
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
Definition: ArmarXDataPath.cpp:109
ArmarXDataPath.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27