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
28
29#include <Ice/Properties.h>
30
33
34// VisionXCore
37
38// VisionXTools
42
44#include <Calibration/Calibration.h>
45
46using namespace armarx;
47
48namespace visionx
49{
50 void
52 {
53 // REQUIRED properties
54 filePathLeft = getProperty<std::string>("PathLeft").getValue();
55
56 // OPTIONAL properties
57 filePathRight = getProperty<std::string>("PathRight").getValue();
58
59 // image format
60 setImageFormat(visionx::ImageDimension(getProperty<ImageDimension>("ImageSize").getValue()),
61 visionx::eRgb);
62 setImageSyncMode(eFpsSynchronization);
63 frameRate = getProperty<float>("FrameRate").getValue();
64
65 // open the image files
67 images = NULL;
70
71 // stereo calibration
72 std::string calibrationFileName = getProperty<std::string>("CalibrationFile").getValue();
73 if (!calibrationFileName.empty())
74 {
75 std::string fullCalibrationFileName;
76
77 if (!ArmarXDataPath::getAbsolutePath(calibrationFileName, fullCalibrationFileName))
78 {
79 ARMARX_ERROR << "Could not find camera calibration file in ArmarXDataPath: "
80 << calibrationFileName;
81 }
82
83 if (!ivtStereoCalibration.LoadCameraParameters(fullCalibrationFileName.c_str(), true))
84 {
85 ARMARX_ERROR << "Error loading camera calibration file: "
86 << fullCalibrationFileName;
87 }
88 }
89
90 stereoCalibration = visionx::tools::convert(ivtStereoCalibration);
91
92 repeatImageCount = getProperty<int>("RepeatImageCount");
93 }
94
95 void
97 {
98 if (images != NULL)
99 {
100 for (int i = 0; i < getNumberImages(); i++)
101 {
102 delete images[i];
103 delete resizedImages[i];
104 }
105
106 delete[] images;
107 delete[] resizedImages;
108
109 images = NULL;
110 }
111 }
112
113 void
115 {
116 if (!bitmapSequenceCapture->OpenCamera())
117 {
119 "Opening bitmap sequence \"" + filePathLeft + "\" failed.");
120 }
121 else
122 {
124 currentRepeatCount = repeatImageCount - 1;
125 }
126 }
127
128 void
133
134 bool
135 ImageSequenceProvider::capture(void** ppImageBuffers)
136 {
137 bool succeeded = true;
138
140 {
141 std::unique_lock lock(captureMutex);
142
143 ARMARX_VERBOSE << "Loading next image";
144
145 loadNewImageAtNextCapture = getProperty<bool>("LoadNextImageAutomatically").getValue();
146
147 ++currentRepeatCount;
148 if (currentRepeatCount == repeatImageCount)
149 {
150 succeeded = bitmapSequenceCapture->CaptureImage(images);
151 currentRepeatCount = 0;
152
153 if (!succeeded)
154 {
155 bitmapSequenceCapture->Rewind();
156 succeeded = bitmapSequenceCapture->CaptureImage(images);
157 }
158 }
159 }
160
161 if (succeeded)
162 {
164
165 for (int i = 0; i < getNumberImages(); i++)
166 {
167 ::ImageProcessor::Resize(images[i], resizedImages[i]);
168
169 memcpy(ppImageBuffers[i],
170 resizedImages[i]->pixels,
171 getImageFormat().dimension.width * getImageFormat().dimension.height *
172 getImageFormat().bytesPerPixel);
173 }
174 }
175
177
178 return succeeded;
179 }
180
181 StereoCalibration
183 {
184 return stereoCalibration;
185 }
186
187 bool
189 {
190 return getProperty<bool>("ImagesAreUndistorted").getValue();
191 }
192
193 void
194 ImageSequenceProvider::setImageFilePath(const std::string& imageFilePathLeft,
195 const std::string& imageFilePathRight,
196 const Ice::Current& c)
197 {
198 filePathLeft = imageFilePathLeft;
199 filePathRight = imageFilePathRight;
200
202 }
203
204 void
206 {
207 std::unique_lock lock(captureMutex);
208
209 bool isStereoMode = filePathRight.length() > 0;
210
211 std::string fullFilePathLeft, fullFilePathRight;
212
213 if (!ArmarXDataPath::getAbsolutePath(filePathLeft, fullFilePathLeft))
214 {
215 ARMARX_ERROR << "Could not find left image sequence file in ArmarXDataPath: "
216 << filePathLeft;
217 }
218
219 if (isStereoMode && !ArmarXDataPath::getAbsolutePath(filePathRight, fullFilePathRight))
220 {
221 ARMARX_ERROR << "Could not find right image sequence file in ArmarXDataPath: "
222 << filePathRight;
223 }
224
226 {
228 }
229
230 bitmapSequenceCapture = new CBitmapSequenceCapture(
231 fullFilePathLeft.c_str(), (isStereoMode ? fullFilePathRight.c_str() : NULL));
232
233 // create images
234 int width = 0;
235 int height = 0;
236
237 if (bitmapSequenceCapture->OpenCamera())
238 {
239 width = bitmapSequenceCapture->GetWidth();
240 height = bitmapSequenceCapture->GetHeight();
241
243 //bitmapSequenceCapture->CloseCamera();
244 }
245 else
246 {
247 ARMARX_ERROR << "Could not open bitmap sequence";
248 }
249
250 if (images)
251 {
252 for (int i = 0; i < getNumberImages(); i++)
253 {
254 delete images[i];
255 delete resizedImages[i];
256 }
257
258 delete[] images;
259 delete[] resizedImages;
260 }
261
262
263 setNumberImages((isStereoMode ? 2 : 1));
264
265
266 images = new CByteImage*[getNumberImages()];
267
268 for (int i = 0; i < getNumberImages(); i++)
269 {
270 images[i] = new CByteImage(width, height, CByteImage::eRGB24);
271 }
272
273 resizedImages = new CByteImage*[getNumberImages()];
274
275 for (int i = 0; i < getNumberImages(); i++)
276 {
277 resizedImages[i] =
279 }
280 }
281
282 void
284 {
285 std::unique_lock lock(captureMutex);
286
288 }
289
290
291std::string
293{
294 return "ImageSequenceProvider";
295}
296
297std::string
302
303
305} // namespace visionx
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
constexpr T c
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
Property< PropertyType > getProperty(const std::string &name)
static DateTime Now()
Definition DateTime.cpp:51
void setImageSyncMode(ImageSyncMode imageSyncMode)
Sets the image synchronization mode.
armarx::SharedMemoryScopedWriteLockPtr getScopedWriteLock()
Retrieve scoped lock for writing to the memory.
void updateTimestamp(Ice::Long timestamp, bool threadSafe=true)
Updates the timestamp of the currently captured image.
ImageFormatInfo getImageFormat(const Ice::Current &c=Ice::emptyCurrent) override
Returns the entire image format info struct via Ice.
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
int getNumberImages(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve number of images handled by this provider.
void setNumberImages(int numberImages)
Sets the number of images on each capture.
Image sequence provider loads images from a device and broadcasts notifications after loading at a sp...
void loadNextImage(const Ice::Current &c=Ice::emptyCurrent) override
CBitmapSequenceCapture * bitmapSequenceCapture
void setImageFilePath(const std::string &imageFilePathLeft, const std::string &imageFilePathRight="", const Ice::Current &c=Ice::emptyCurrent) override
Set the paths for the image files.
void onStartCapture(float frameRate) override
bool getImagesAreUndistorted(const Ice::Current &c=Ice::emptyCurrent) override
Returns whether images are undistorted.
visionx::StereoCalibration getStereoCalibration(const Ice::Current &c=Ice::emptyCurrent) override
Returns the StereoCalibration as provided in configuration.
std::string getDefaultName() const override
Retrieve default name of component.
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
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.