FlyCaptureImageProvider.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::FlyCaptureImageProvider
19 * @author Markus Grotz ( markus dot grotz 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
27#include <SimoxUtility/algorithm/string/string_tools.h>
28
30
32
33#include <Calibration/StereoCalibration.h>
34
35using namespace armarx;
36
37void
39{
40 std::string value = getProperty<std::string>("serialNumbers").getValue();
41 std::vector<std::string> serialNumbers = simox::alg::split(value, "\t ,");
42
43 visionx::ImageDimension dimensions =
44 getProperty<visionx::ImageDimension>("dimensions").getValue();
45
46 unsigned int numCameras;
47 if (busManager.GetNumOfCameras(&numCameras) != FlyCapture2::PGRERROR_OK && !numCameras)
48 {
49 ARMARX_FATAL << "no cameras found or unable to query bus";
50 return;
51 }
52
53 ARMARX_INFO << "found " << numCameras << " cameras";
54
55 for (std::string& serialNumber : serialNumbers)
56 {
57 FlyCapture2::PGRGuid pGuid;
58
59 if (busManager.GetCameraFromSerialNumber(std::stoi(serialNumber), &pGuid) !=
60 FlyCapture2::PGRERROR_OK)
61 {
62 ARMARX_FATAL << "invalid serial number or camera not found: " << serialNumber;
63 }
64
65 FlyCapture2::Camera* c = new FlyCapture2::Camera();
66
67
68 if (c->Connect(&pGuid) != FlyCapture2::PGRERROR_OK)
69 {
70 ARMARX_FATAL << "unable to connect to camera!";
71 }
72
73 FlyCapture2::CameraInfo cameraInfo;
74 if (c->GetCameraInfo(&cameraInfo) != FlyCapture2::PGRERROR_OK)
75 {
76 ARMARX_WARNING << "unable to get camera info";
77 }
78 else
79 {
80 ARMARX_INFO << " camera model: " << cameraInfo.modelName << " serial number: `"
81 << cameraInfo.serialNumber;
82 }
83
84 cameras.push_back(c);
85
86
87 colorImages.push_back(new FlyCapture2::Image(dimensions.height,
88 dimensions.width,
89 FlyCapture2::PIXEL_FORMAT_RGB,
90 FlyCapture2::BayerTileFormat::RGGB));
91 }
92
93
94 cameraImages = new CByteImage*[cameras.size()];
95 for (size_t i = 0; i < cameras.size(); i++)
96 {
97 cameraImages[i] = new CByteImage(dimensions.width, dimensions.height, CByteImage::eRGB24);
98 }
99
100 rectifyImages = false;
101
102 undistortion = new CUndistortion();
103 undistortImages = getProperty<bool>("UndistortImages").getValue();
104 undistortion->Init(calibrationFileName.c_str());
105
106 frameRate = getProperty<float>("FrameRate").getValue();
107
108 setNumberImages(cameras.size());
109 setImageFormat(dimensions, visionx::eRgb, visionx::eBayerPatternRg);
110 setImageSyncMode(visionx::eCaptureSynchronization);
111}
112
113void
115{
116 for (FlyCapture2::Camera* c : cameras)
117 {
118 if (c->IsConnected())
119 {
120 c->Disconnect();
121 }
122
123 delete c;
124 }
125
126 for (FlyCapture2::Image* i : colorImages)
127 {
128 delete i;
129 }
130
131
132 for (size_t i = 0; i < cameras.size(); i++)
133 {
134 delete cameraImages[i];
135 }
136
137 delete[] cameraImages;
138
139 delete undistortion;
140}
141
142void
144{
145 for (FlyCapture2::Camera* c : cameras)
146 {
147 FlyCapture2::Property p;
148 p.absControl = true;
149 p.onOff = true;
150
151 p.type = FlyCapture2::AUTO_EXPOSURE;
152 p.absValue = getProperty<float>("Exposure").getValue();
153 p.autoManualMode = getProperty<float>("Exposure").getValue() >= 0;
154 if (c->SetProperty(&p) != FlyCapture2::PGRERROR_OK)
155 {
156 ARMARX_WARNING << "unable to set auto exposure property";
157 }
158
159 p.type = FlyCapture2::SHUTTER;
160 p.absValue = getProperty<float>("Shutter").getValue();
161 p.autoManualMode = getProperty<float>("Shutter").getValue() >= 0;
162 if (c->SetProperty(&p) != FlyCapture2::PGRERROR_OK)
163 {
164 ARMARX_WARNING << "unable to set shutter property";
165 }
166
167 p.type = FlyCapture2::GAIN;
168 p.absValue = getProperty<float>("Gain").getValue();
169 p.autoManualMode = getProperty<float>("Gain").getValue() >= 0;
170 if (c->SetProperty(&p) != FlyCapture2::PGRERROR_OK)
171 {
172 ARMARX_WARNING << "unable to set gain property";
173 }
174
175
176 FlyCapture2::FrameRate frameRate = FlyCapture2::FRAMERATE_7_5;
177
178 if (framesPerSecond >= 60.0)
179 {
180 frameRate = FlyCapture2::FRAMERATE_60;
181 }
182 else if (framesPerSecond >= 30.0)
183 {
184 frameRate = FlyCapture2::FRAMERATE_30;
185 }
186 else if (framesPerSecond >= 15.0)
187 {
188 frameRate = FlyCapture2::FRAMERATE_15;
189 }
190 else if (framesPerSecond >= 7.5)
191 {
192 frameRate = FlyCapture2::FRAMERATE_7_5;
193 }
194 else
195 {
196 ARMARX_WARNING << "unsupported frame rate";
197 }
198
199 FlyCapture2::VideoMode mode = FlyCapture2::VideoMode::VIDEOMODE_640x480Y8;
200
201
202 if (getProperty<visionx::ImageDimension>("dimensions").getValue() ==
203 visionx::ImageDimension(640, 480))
204 {
205 mode = FlyCapture2::VideoMode::VIDEOMODE_640x480Y8;
206 }
207 else if (getProperty<visionx::ImageDimension>("dimensions").getValue() ==
208 visionx::ImageDimension(1600, 1200))
209 {
210 mode = FlyCapture2::VideoMode::VIDEOMODE_1600x1200Y8;
211 }
212 else
213 {
214 ARMARX_WARNING << "unsupported video mode";
215 }
216
217 FlyCapture2::Error error = c->SetVideoModeAndFrameRate(mode, frameRate);
218
219 if (error != FlyCapture2::PGRERROR_OK)
220 {
221 ARMARX_WARNING << "unable to set video mode";
222 }
223
224 if (c->StartCapture() != FlyCapture2::PGRERROR_OK)
225 {
226 ARMARX_WARNING << "unable to start capture!";
227 }
228 }
229}
230
231void
233{
234 for (FlyCapture2::Camera* c : cameras)
235 {
236 c->StopCapture();
237 }
238}
239
240bool
242{
243 visionx::ImageFormatInfo imageFormat = getImageFormat();
244 const int imageSize =
245 imageFormat.dimension.width * imageFormat.dimension.height * imageFormat.bytesPerPixel;
246
247 FlyCapture2::Image imageBuffer;
248 for (size_t i = 0; i < cameras.size(); i++)
249 {
250
251 if (cameras[i]->RetrieveBuffer(&imageBuffer) != FlyCapture2::PGRERROR_OK)
252 {
253 ARMARX_WARNING << "failed to get camera buffer";
254 continue;
255 //return false;
256 }
257
258 if (imageBuffer.Convert(FlyCapture2::PIXEL_FORMAT_RGB, colorImages[i]) !=
259 FlyCapture2::PGRERROR_OK)
260 {
261 ARMARX_WARNING << "failed to convert image";
262 return false;
263 }
264 }
265
267
268 for (size_t i = 0; i < cameras.size(); i++)
269 {
270 memcpy(cameraImages[i]->pixels, colorImages[i]->GetData(), imageSize);
271 }
272
273 if (rectifyImages)
274 {
275 rectification->Rectify(cameraImages, cameraImages);
276 }
277 else if (undistortImages)
278 {
279 undistortion->Undistort(cameraImages, cameraImages);
280 }
281
282 for (size_t i = 0; i < cameras.size(); i++)
283 {
284 memcpy(ppImageBuffers[i], cameraImages[i]->pixels, imageSize);
285 }
286
287
288 return true;
289}
290
297
298void
300{
302
303 calibrationFileName = getProperty<std::string>("CalibrationFile").getValue();
305 {
306 ARMARX_ERROR << "" << calibrationFileName.c_str();
307 }
308
309 CStereoCalibration ivtStereoCalibration;
310 if (!ivtStereoCalibration.LoadCameraParameters(calibrationFileName.c_str(), true))
311 {
312 ARMARX_ERROR << "" << calibrationFileName.c_str();
313 }
314
315 rectifyImages = getProperty<bool>("RectifyImages").getValue();
316 rectification = new CRectification(true, undistortImages);
317 rectification->Init(calibrationFileName.c_str());
318 stereoCalibration = visionx::tools::convert(ivtStereoCalibration);
319}
constexpr T c
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
void onStartCapture(float frameRate) override
This is called when the image provider capturing has been started.
void onStopCapture() override
This is called when the image provider capturing has been stopped.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onExitCapturingImageProvider() override
This is called when the Component::onExitComponent() setup is called.
void onInitCapturingImageProvider() override
This is called when the Component::onInitComponent() is called.
void onInitCapturingImageProvider() override
This is called when the Component::onInitComponent() is called.
void setImageSyncMode(ImageSyncMode imageSyncMode)
Sets the image synchronization mode.
armarx::IceSharedMemoryProvider< unsignedchar >::pointer_type sharedMemoryProvider
shared memory provider
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.
void setNumberImages(int numberImages)
Sets the number of images on each capture.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_FATAL
The logging level for unexpected behaviour, that will lead to a seriously malfunctioning program and ...
Definition Logging.h:199
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
CByteImage::ImageType convert(const ImageType visionxImageType)
Converts a VisionX image type into an image type of IVT's ByteImage.