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