VirtualColorCam.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include "../include/cob_camera_sensors/StdAfx.h"
19 
20 #ifdef __LINUX__
22 #include "tinyxml.h"
23 #else
25 #include "cob_vision/windows/src/extern/TinyXml/tinyxml.h"
26 #endif
27 
28 #include <opencv/highgui.h>
29 #include <iostream>
30 
31 namespace fs = boost::filesystem;
32 using namespace ipa_CameraSensors;
33 
35 {
37 }
38 
40 {
41  m_initialized = false;
42  m_open = false;
43 
44  m_BufferSize = 1;
45 
46  m_ImageWidth = 0;
47  m_ImageHeight = 0;
48 
49  m_ImageCounter = 0;
50 }
51 
53 {
54  if (isOpen())
55  {
56  Close();
57  }
58 }
59 
60 
61 unsigned long VirtualColorCam::Init(std::string directory, int cameraIndex)
62 {
63  if (isInitialized())
64  {
66  }
67 
69 
70  // It is important to put this before LoadParameters
71  m_CameraDataDirectory = directory;
72  if (LoadParameters((directory + "cameraSensorsIni.xml").c_str(), cameraIndex) & RET_FAILED)
73  {
74  return (RET_FAILED | RET_INIT_CAMERA_FAILED);
75  }
76 
77  m_CameraIndex = cameraIndex;
78 
79  m_initialized = true;
80  return RET_OK;
81 
82 }
83 
84 
85 unsigned long VirtualColorCam::Open()
86 {
87  if (!isInitialized())
88  {
89  std::cerr << "ERROR - VirtualColorCam::Open:" << std::endl;
90  std::cerr << "\t ... Camera not initialized." << std::endl;
91  return (RET_FAILED);
92  }
93  m_open = false;
94 
95  if (isOpen())
96  {
98  }
99 
100  // Convert camera ID to string
101  std::stringstream ss;
102  std::string sCameraIndex;
103  ss << m_CameraIndex;
104  ss >> sCameraIndex;
105 
106  m_ImageWidth = -1;
107  m_ImageHeight = -1;
108 
109  // Create absolute filename and check if directory exists
110  fs::path absoluteDirectoryName( m_CameraDataDirectory );
111  if ( !fs::exists( absoluteDirectoryName ) )
112  {
113  std::cerr << "ERROR - VirtualColorCam::Open:" << std::endl;
114  std::cerr << "\t ... Path '" << absoluteDirectoryName.file_string() << "' not found" << std::endl;
116  }
117 
118  int colorImageCounter = 0;
119  // Extract all image filenames from the directory
120  if ( fs::is_directory( absoluteDirectoryName ) )
121  {
122  std::cout << "INFO - VirtualColorCam::Open:" << std::endl;
123  std::cout << "\t ... Parsing directory '" << absoluteDirectoryName.directory_string() << "'" << std::endl;;
124  fs::directory_iterator end_iter;
125  for ( fs::directory_iterator dir_itr( absoluteDirectoryName ); dir_itr != end_iter; ++dir_itr )
126  {
127  try
128  {
129  if (fs::is_regular_file(dir_itr->status()))
130  {
131  std::string filename = dir_itr->path().string();
132  if ((dir_itr->path().extension() == ".jpg" || dir_itr->path().extension() == ".jpe" ||
133  dir_itr->path().extension() == ".jpeg" || dir_itr->path().extension() == ".bmp" ||
134  dir_itr->path().extension() == ".bmp" || dir_itr->path().extension() == ".dib" ||
135  dir_itr->path().extension() == ".png" || dir_itr->path().extension() == ".pgm" ||
136  dir_itr->path().extension() == ".ppm" || dir_itr->path().extension() == ".sr" ||
137  dir_itr->path().extension() == ".ras" || dir_itr->path().extension() == ".tiff" ||
138  dir_itr->path().extension() == ".exr" || dir_itr->path().extension() == ".jp2") &&
139  filename.find( "ColorCamRGB_8U3_" + sCameraIndex, 0 ) != std::string::npos)
140  {
141  ++colorImageCounter;
142  //std::cout << "VirtualColorCam::Open(): Reading '" << dir_itr->path().string() << "\n";
143  m_ColorImageFileNames.push_back(dir_itr->path().string());
144  // Get image size
145  if (m_ImageWidth == -1 || m_ImageHeight == -1)
146  {
147  cv::Mat image = cv::imread(m_ColorImageFileNames.back());
148  m_ImageWidth = image.cols;
149  m_ImageHeight = image.rows;
150  }
151  }
152  }
153  }
154  catch ( const std::exception &ex )
155  {
156  std::cerr << "ERROR - VirtualColorCam::Open:" << std::endl;
157  std::cerr << "\t ... Exception catch of '" << ex.what() << "'" << std::endl;
158  }
159  }
160  std::sort(m_ColorImageFileNames.begin(),m_ColorImageFileNames.end());
161  std::cout << "INFO - VirtualColorCam::Open:" << std::endl;
162  std::cerr << "\t ... Extracted '" << colorImageCounter << "' color images (8*3 bit/color)\n";
163  }
164  else
165  {
166  std::cerr << "ERROR - VirtualColorCam::Open:" << std::endl;
167  std::cerr << "\t .... Path '" << absoluteDirectoryName.file_string() << "' is not a directory." << std::endl;
169  }
170 
171  if (colorImageCounter == 0)
172  {
173  std::cerr << "ERROR - VirtualColorCam::Open:" << std::endl;
174  std::cerr << "\t ... Could not detect any color images" << std::endl;
175  std::cerr << "\t ... from the specified directory. Check directory" << std::endl;
176  std::cerr << "\t ... and filenames (i.e. ColorCamRGB_8U3_*_*.jpg)." << std::endl;
178  }
179 
180  std::cout << "*******************************************************" << std::endl;
181  std::cout << "VirtualColorCam::Open: Virtual color camera device OPEN" << std::endl;
182  std::cout << "*******************************************************" << std::endl << std::endl;
183 
184  m_open = true;
185  return RET_OK;
186 
187 }
188 
190 {
191  return (int)std::min(0.0f, (float)m_ColorImageFileNames.size());
192 }
193 
194 unsigned long VirtualColorCam::SaveParameters(const char* filename)
195 {
196  return RET_FAILED;
197 }
198 
199 unsigned long VirtualColorCam::SetPathToImages(std::string path)
200 {
201  m_CameraDataDirectory = path;
202  return RET_OK;
203 }
204 
205 
206 unsigned long VirtualColorCam::Close()
207 {
208  if (!isOpen())
209  {
210  return RET_OK;
211  }
212 
213  m_open = false;
214  return RET_OK;
215 }
216 
217 
218 unsigned long VirtualColorCam::SetProperty(t_cameraProperty* cameraProperty)
219 {
220 #ifdef __LINUX__
221  std::cerr << "VirtualColorCam::SetProperty: Function not implemented.";
222  return RET_FAILED;
223 #endif
224 #ifndef __LINUX__
225 
226  switch (cameraProperty->propertyID)
227  {
229  m_ImageWidth = cameraProperty->cameraResolution.xResolution;
230  m_ImageHeight = cameraProperty->cameraResolution.yResolution;
231  break;
232  default:
233  std::cerr << "ERROR - VirtualColorCam::SetProperty:" << std::endl;
234  std::cerr << "\t ... Property " << cameraProperty->propertyID << " unspecified.";
235  return RET_FAILED;
236  break;
237  }
238 
239  return RET_OK;
240 #endif
241 }
242 
244 
245 unsigned long VirtualColorCam::GetProperty(t_cameraProperty* cameraProperty)
246 {
247  switch (cameraProperty->propertyID)
248  {
250  cameraProperty->cameraResolution.xResolution = m_ImageWidth;
251  cameraProperty->cameraResolution.yResolution = m_ImageHeight;
252  cameraProperty->propertyType = TYPE_CAMERA_RESOLUTION;
253  return RET_OK;
254  break;
255 
257  cameraProperty->u_integerData = m_BufferSize;
258  return RET_OK;
259  break;
260 
261  default:
262  std::cerr << "ERROR - VirtualColorCam::SetProperty:" << std::endl;
263  std::cerr << "\t ... Property " << cameraProperty->propertyID << " unspecified.";
264  return RET_FAILED;
265  break;
266 
267  }
268 
269  return RET_OK;
270 }
271 
272 
273 unsigned long VirtualColorCam::GetColorImage(char* colorImageData, bool getLatestFrame)
274 {
275  if (!isOpen())
276  {
277  std::cerr << "ERROR - VirtualColorCam::GetColorImage:" << std::endl;
278  std::cerr << "\t ... Color camera not open." << std::endl;
279  return (RET_FAILED | RET_CAMERA_NOT_OPEN);
280  }
281 
282  IplImage* colorImage = (IplImage*) cvLoadImage(m_ColorImageFileNames[m_ImageCounter].c_str(), CV_LOAD_IMAGE_COLOR);
283 
284  for(int row=0; row<m_ImageHeight; row++)
285  {
286  for (int col=0; col<m_ImageWidth; col++)
287  {
288  char* f_color_ptr = &((char*) (colorImage->imageData + row*colorImage->widthStep))[col*3];
289  ((char*) (colorImageData + row*colorImage->widthStep))[col*3 + 0] = f_color_ptr[0];
290  ((char*) (colorImageData + row*colorImage->widthStep))[col*3 + 1] = f_color_ptr[1];
291  ((char*) (colorImageData + row*colorImage->widthStep))[col*3 + 2] = f_color_ptr[2];
292  }
293  }
294 
295  cvReleaseImage(&colorImage);
296 
297  m_ImageCounter++;
299  {
300  // Reset image counter
301  m_ImageCounter = 0;
302  }
303 
304  return RET_OK;
305 }
306 
307 
308 unsigned long VirtualColorCam::GetColorImage(cv::Mat* colorImage, bool getLatestFrame)
309 {
310  if (!isOpen())
311  {
312  std::cerr << "ERROR - VirtualColorCam::GetColorImage:" << std::endl;
313  std::cerr << "\t ... Color camera not open." << std::endl;
314  return (RET_FAILED | RET_CAMERA_NOT_OPEN);
315  }
316 
317  CV_Assert(colorImage != 0);
318 
319  colorImage->create(m_ImageHeight, m_ImageWidth, CV_8UC3);
320 
321  return GetColorImage((char*)(colorImage->ptr<unsigned char>(0)), getLatestFrame);
322 
323  return RET_FAILED;
324 }
325 
327 {
329 }
330 
331 unsigned long VirtualColorCam::TestCamera(const char* filename)
332 {
334  {
335  return RET_FAILED;
336  }
337 
338  return RET_OK;
339 }
340 
341 
342 unsigned long VirtualColorCam::LoadParameters(const char* filename, int cameraIndex)
343 { //m_intrinsicMatrix; m_distortionParameters
344  boost::shared_ptr<TiXmlDocument> p_configXmlDocument (new TiXmlDocument( filename ));
345  if (!p_configXmlDocument->LoadFile())
346  {
347  std::cerr << "ERROR - VirtualColorCam::LoadParameters:" << std::endl;
348  std::cerr << "\t ... Error while loading xml configuration file (Check filename and syntax of the file):\n" << filename << std::endl;
349  return (RET_FAILED | RET_FAILED_OPEN_FILE);
350  }
351  std::cout << "INFO - VirtualColorCam::LoadParameters:" << std::endl;
352  std::cout << "\t ... Parsing xml configuration file:" << std::endl;
353  std::cout << "\t ... '" << filename << "'" << std::endl;
354 
355  if ( p_configXmlDocument )
356  {
357 
358 //************************************************************************************
359 // BEGIN LibCameraSensors
360 //************************************************************************************
361  // Tag element "LibCameraSensors" of Xml Inifile
362  TiXmlElement *p_xmlElement_Root = NULL;
363  p_xmlElement_Root = p_configXmlDocument->FirstChildElement( "LibCameraSensors" );
364  if ( p_xmlElement_Root )
365  {
366 
367 //************************************************************************************
368 // BEGIN LibCameraSensors->VirtualColorCam
369 //************************************************************************************
370  // Tag element "VirtualColorCam" of Xml Inifile
371  TiXmlElement *p_xmlElement_Root_VirtualColorCam = NULL;
372  std::stringstream ss;
373  ss << "VirtualColorCam_" << cameraIndex;
374  p_xmlElement_Root_VirtualColorCam = p_xmlElement_Root->FirstChildElement( ss.str() );
375  if ( p_xmlElement_Root_VirtualColorCam )
376  {
377 
378 //************************************************************************************
379 // BEGIN LibCameraSensors->VirtualColorCam->CameraDataDirectory
380 //************************************************************************************
381  // Subtag element "CameraDataDirectory" of Xml Inifile
382  TiXmlElement *p_xmlElement_Child = NULL;
383  p_xmlElement_Child = p_xmlElement_Root_VirtualColorCam->FirstChildElement( "CameraDataDirectory" );
384  if ( p_xmlElement_Child )
385  {
386  // read and save value of attribute
387  std::string tempString;
388  if ( p_xmlElement_Child->QueryValueAttribute( "relativePath", &tempString ) != TIXML_SUCCESS)
389  {
390  std::cerr << "VirtualColorCam::LoadParameters: Can't find attribute 'relativePath' of tag 'CameraDataDirectory'." << std::endl;
392  }
393 
394  m_CameraDataDirectory = m_CameraDataDirectory + tempString + "/";
395  }
396  else
397  {
398  std::cerr << "ERROR - VirtualColorCam::LoadParameters:" << std::endl;
399  std::cerr << "\t ... Can't find tag 'CameraDataDirectory'." << std::endl;
401  }
402  }
403 //************************************************************************************
404 // END LibCameraSensors->VirtualColorCam
405 //************************************************************************************
406  else
407  {
408  std::cerr << "ERROR - VirtualColorCam::LoadParameters:" << std::endl;
409  std::cerr << "\t ... Can't find tag '" << ss.str() << "'" << std::endl;
411  }
412  }
413 
414 //************************************************************************************
415 // END LibCameraSensors
416 //************************************************************************************
417  else
418  {
419  std::cerr << "ERROR - VirtualColorCam::LoadParameters:" << std::endl;
420  std::cerr << "\t ... Can't find tag 'LibCameraSensors'." << std::endl;
422  }
423  }
424 
425  std::cout << "\t [OK] Parsing xml configuration file " << std::endl;
426 
427  return RET_OK;
428 }
int m_CameraIndex
Index of the specified camera. Important, when several cameras of the same type are present...
unsigned long TestCamera(const char *filename)
f
boost::shared_ptr< AbstractColorCamera > AbstractColorCameraPtr
virtual unsigned long TestCamera(const char *filename)
unsigned long SetProperty(t_cameraProperty *cameraProperty)
unsigned long LoadParameters(const char *filename, int cameraIndex)
unsigned long GetProperty(t_cameraProperty *cameraProperty)
unsigned long Init(std::string directory, int cameraIndex=0)
bool m_initialized
True, when the camera has sucessfully been initialized.
__DLL_LIBCAMERASENSORS__ AbstractColorCameraPtr CreateColorCamera_VirtualCam()
unsigned long SetPathToImages(std::string path)
unsigned long GetColorImage(char *colorImageData, bool getLatestFrame)
std::vector< std::string > m_ColorImageFileNames
bool m_open
True, when the camera has sucessfully been opend.
unsigned int m_BufferSize
Number of images, the camera buffers internally.
unsigned long SaveParameters(const char *filename)
unsigned int m_ImageCounter
Holds the index of the image that is extracted during the next call of AcquireImages ...
#define __DLL_LIBCAMERASENSORS__
std::string m_CameraDataDirectory
Directory where the image data resides.


cob_camera_sensors
Author(s): Jan Fischer , Richard Bormann
autogenerated on Thu Mar 19 2020 03:23:05