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
42#include <Calibration/Calibration.h>
43
44using namespace armarx;
45
46namespace visionx
47{
48 void
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
65 images = NULL;
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
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
113 {
114 if (!bitmapSequenceCapture->OpenCamera())
115 {
117 "Opening bitmap sequence \"" + filePathLeft + "\" failed.");
118 }
119 else
120 {
122 currentRepeatCount = repeatImageCount - 1;
123 }
124 }
125
126 void
131
132 bool
133 ImageSequenceProvider::capture(void** ppImageBuffers)
134 {
135 bool succeeded = true;
136
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 {
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
175
176 return succeeded;
177 }
178
179 StereoCalibration
181 {
182 return stereoCalibration;
183 }
184
185 bool
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
200 }
201
202 void
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
224 {
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
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] =
277 }
278 }
279
280 void
282 {
283 std::unique_lock lock(captureMutex);
284
286 }
287
288} // namespace visionx
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.
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.
#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.