00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #ifdef __LINUX__
00055 #include "cob_camera_sensors/VirtualRangeCam.h"
00056 #else
00057 #include "cob_driver/cob_camera_sensors/common/include/cob_camera_sensors/VirtualRangeCam.h"
00058 #endif
00059
00060 using namespace ipa_CameraSensors;
00061
00062 __DLL_LIBCAMERASENSORS__ AbstractRangeImagingSensorPtr ipa_CameraSensors::CreateRangeImagingSensor_VirtualCam()
00063 {
00064 return AbstractRangeImagingSensorPtr(new VirtualRangeCam());
00065 }
00066
00067 VirtualRangeCam::VirtualRangeCam()
00068 {
00069 m_initialized = false;
00070 m_open = false;
00071
00072 m_BufferSize = 1;
00073
00074 m_ImageCounter = 0;
00075 }
00076
00077
00078 VirtualRangeCam::~VirtualRangeCam()
00079 {
00080 if (isOpen())
00081 {
00082 Close();
00083 }
00084 }
00085
00086 unsigned long VirtualRangeCam::Init(std::string directory, int cameraIndex)
00087 {
00088 if (isInitialized())
00089 {
00090 return (RET_OK | RET_CAMERA_ALREADY_INITIALIZED);
00091 }
00092
00093 m_CameraType = ipa_CameraSensors::CAM_VIRTUALRANGE;
00094
00095
00096 m_CameraDataDirectory = directory;
00097
00098
00099 if (LoadParameters((directory + "cameraSensorsIni.xml").c_str(), cameraIndex) & RET_FAILED)
00100 {
00101 std::cerr << "ERROR - VirtualRangeCam::Init:" << std::endl;
00102 std::cerr << "\t ... Parsing xml configuration file failed" << std::endl;
00103 return (RET_FAILED | RET_INIT_CAMERA_FAILED);
00104 }
00105
00106 m_CoeffsInitialized = true;
00107 if (m_CalibrationMethod == MATLAB)
00108 {
00109
00110 std::string filename = directory + "MatlabCalibrationData/PMD/ZCoeffsA0.xml";
00111 CvMat* c_mat = (CvMat*)cvLoad(filename.c_str());
00112 if (! c_mat)
00113 {
00114 std::cerr << "ERROR - PMDCamCube::LoadParameters:" << std::endl;
00115 std::cerr << "\t ... Error while loading " << directory + "MatlabCalibrationData/ZcoeffsA0.txt" << "." << std::endl;
00116 std::cerr << "\t ... Data is necessary for z-calibration of swissranger camera" << std::endl;
00117 m_CoeffsInitialized = false;
00118
00119 }
00120 else
00121 {
00122 m_CoeffsA0 = c_mat;
00123 cvReleaseMat(&c_mat);
00124 }
00125
00126 filename = directory + "MatlabCalibrationData/PMD/ZCoeffsA1.xml";
00127 c_mat = (CvMat*)cvLoad(filename.c_str());
00128 if (! c_mat)
00129 {
00130 std::cerr << "ERROR - PMDCamCube::LoadParameters:" << std::endl;
00131 std::cerr << "\t ... Error while loading " << directory + "MatlabCalibrationData/ZcoeffsA1.txt" << "." << std::endl;
00132 std::cerr << "\t ... Data is necessary for z-calibration of swissranger camera" << std::endl;
00133 m_CoeffsInitialized = false;
00134
00135 }
00136 else
00137 {
00138 m_CoeffsA1 = c_mat;
00139 cvReleaseMat(&c_mat);
00140 }
00141
00142 filename = directory + "MatlabCalibrationData/PMD/ZCoeffsA2.xml";
00143 c_mat = (CvMat*)cvLoad(filename.c_str());
00144 if (! c_mat)
00145 {
00146 std::cerr << "ERROR - PMDCamCube::LoadParameters:" << std::endl;
00147 std::cerr << "\t ... Error while loading " << directory + "MatlabCalibrationData/ZcoeffsA2.txt" << "." << std::endl;
00148 std::cerr << "\t ... Data is necessary for z-calibration of swissranger camera" << std::endl;
00149 m_CoeffsInitialized = false;
00150
00151 }
00152 else
00153 {
00154 m_CoeffsA2 = c_mat;
00155 cvReleaseMat(&c_mat);
00156 }
00157
00158 filename = directory + "MatlabCalibrationData/PMD/ZCoeffsA3.xml";
00159 c_mat = (CvMat*)cvLoad(filename.c_str());
00160 if (! c_mat)
00161 {
00162 std::cerr << "ERROR - PMDCamCube::LoadParameters:" << std::endl;
00163 std::cerr << "\t ... Error while loading " << directory + "MatlabCalibrationData/ZcoeffsA3.txt" << "." << std::endl;
00164 std::cerr << "\t ... Data is necessary for z-calibration of swissranger camera" << std::endl;
00165 m_CoeffsInitialized = false;
00166
00167 }
00168 else
00169 {
00170 m_CoeffsA3 = c_mat;
00171 cvReleaseMat(&c_mat);
00172 }
00173
00174 filename = directory + "MatlabCalibrationData/PMD/ZCoeffsA4.xml";
00175 c_mat = (CvMat*)cvLoad(filename.c_str());
00176 if (! c_mat)
00177 {
00178 std::cerr << "ERROR - PMDCamCube::LoadParameters:" << std::endl;
00179 std::cerr << "\t ... Error while loading " << directory + "MatlabCalibrationData/ZcoeffsA4.txt" << "." << std::endl;
00180 std::cerr << "\t ... Data is necessary for z-calibration of swissranger camera" << std::endl;
00181 m_CoeffsInitialized = false;
00182
00183 }
00184 else
00185 {
00186 m_CoeffsA4 = c_mat;
00187 cvReleaseMat(&c_mat);
00188 }
00189
00190 filename = directory + "MatlabCalibrationData/PMD/ZCoeffsA5.xml";
00191 c_mat = (CvMat*)cvLoad(filename.c_str());
00192 if (! c_mat)
00193 {
00194 std::cerr << "ERROR - PMDCamCube::LoadParameters:" << std::endl;
00195 std::cerr << "\t ... Error while loading " << directory + "MatlabCalibrationData/ZcoeffsA5.txt" << "." << std::endl;
00196 std::cerr << "\t ... Data is necessary for z-calibration of swissranger camera" << std::endl;
00197 m_CoeffsInitialized = false;
00198
00199 }
00200 else
00201 {
00202 m_CoeffsA5 = c_mat;
00203 cvReleaseMat(&c_mat);
00204 }
00205
00206 filename = directory + "MatlabCalibrationData/PMD/ZCoeffsA6.xml";
00207 c_mat = (CvMat*)cvLoad(filename.c_str());
00208 if (! c_mat)
00209 {
00210 std::cerr << "ERROR - PMDCamCube::LoadParameters:" << std::endl;
00211 std::cerr << "\t ... Error while loading " << directory + "MatlabCalibrationData/ZcoeffsA6.txt" << "." << std::endl;
00212 std::cerr << "\t ... Data is necessary for z-calibration of swissranger camera" << std::endl;
00213 m_CoeffsInitialized = false;
00214
00215 }
00216 else
00217 {
00218 m_CoeffsA6 = c_mat;
00219 cvReleaseMat(&c_mat);
00220 }
00221 }
00222
00223 m_CameraIndex = cameraIndex;
00224
00225
00226 m_initialized = true;
00227
00228 return RET_OK;
00229 }
00230
00231
00232 inline void VirtualRangeCam::UpdateImageDimensionsOnFirstImage(std::string filename, std::string ext)
00233 {
00234 if (m_ImageWidth == -1 || m_ImageHeight == -1)
00235 {
00236 if (ext != ".bin")
00237 {
00238 IplImage* image = (IplImage*) cvLoad(filename.c_str(), 0);
00239 m_ImageWidth = image->width;
00240 m_ImageHeight = image->height;
00241 cvReleaseImage(&image);
00242 }
00243 else
00244 {
00245 cv::Mat mat;
00246 ipa_Utils::LoadMat(mat, filename);
00247 m_ImageHeight = mat.rows;
00248 m_ImageWidth = mat.cols;
00249 }
00250 }
00251 }
00252
00253
00254 inline void VirtualRangeCam::FindSourceImageFormat(std::map<std::string, int>::iterator& itCounter, std::string& ext)
00255 {
00256 if (itCounter->second > 0)
00257 {
00258 if (ext == "") ext = itCounter->first;
00259 else
00260 {
00261 std::cerr << "ERROR - VirtualRangeCam::Open:\n";
00262 std::cerr << "\t ... The provided path contains data in mixed formats (e.g. .xml and .bin).\n";
00263 assert(false);
00264 }
00265 }
00266 }
00267
00268
00269 unsigned long VirtualRangeCam::Open()
00270 {
00271 if (!isInitialized())
00272 {
00273 return (RET_FAILED | RET_CAMERA_NOT_INITIALIZED);
00274 }
00275
00276 if (isOpen())
00277 {
00278 return (RET_OK | RET_CAMERA_ALREADY_OPEN);
00279 }
00280
00281
00282 std::stringstream ss;
00283 std::string sCameraIndex;
00284 ss << m_CameraIndex;
00285 ss >> sCameraIndex;
00286
00287 m_ImageWidth = -1;
00288 m_ImageHeight = -1;
00289
00290
00291 fs::path absoluteDirectoryName( m_CameraDataDirectory );
00292 if ( !fs::exists( absoluteDirectoryName ) )
00293 {
00294 std::cerr << "ERROR - VirtualRangeCam::Open:" << std::endl;
00295 std::cerr << "\t ... Path '" << absoluteDirectoryName.file_string() << "' not found" << std::endl;
00296 return (ipa_CameraSensors::RET_FAILED | ipa_CameraSensors::RET_FAILED_OPEN_FILE);
00297 }
00298
00299 std::vector<std::string> extensionList;
00300 extensionList.push_back(".xml"); extensionList.push_back(".bin"); extensionList.push_back(".png"); extensionList.push_back(".jpg"); extensionList.push_back(".bmp");
00301 std::map<std::string, int> amplitudeImageCounter;
00302 std::map<std::string, int> intensityImageCounter;
00303 std::map<std::string, int> coordinateImageCounter;
00304 std::map<std::string, int> rangeImageCounter;
00305 std::map<std::string, std::vector<std::string> > amplitudeImageFileNames;
00306 std::map<std::string, std::vector<std::string> > intensityImageFileNames;
00307 std::map<std::string, std::vector<std::string> > coordinateImageFileNames;
00308 std::map<std::string, std::vector<std::string> > rangeImageFileNames;
00309
00310 if ( fs::exists( absoluteDirectoryName ) )
00311 {
00312 std::cout << "INFO - VirtualRangeCam::Open :" << std::endl;
00313 std::cout << "\t ... Parsing directory '" << absoluteDirectoryName.directory_string() << "'" << std::endl;
00314
00315 fs::directory_iterator end_iter;
00316 for ( fs::directory_iterator dir_itr( absoluteDirectoryName ); dir_itr != end_iter; ++dir_itr )
00317 {
00318 try
00319 {
00320 if (fs::is_regular_file(dir_itr->status()))
00321 {
00322 std::string filename = dir_itr->path().string();
00323
00324
00325 for (unsigned int extIndex=0; extIndex<extensionList.size(); extIndex++)
00326 {
00327 std::string ext = extensionList[extIndex];
00328 std::string format = "32F1_";
00329 if (extIndex>=2) format = "8U3_";
00330 if ((dir_itr->path().extension() == ext) && filename.find( "RangeCamIntensity_" + format + sCameraIndex, 0 ) != std::string::npos)
00331 {
00332 (intensityImageCounter.find(ext) == intensityImageCounter.end()) ? intensityImageCounter[ext] = 1 : intensityImageCounter[ext]++;
00333
00334 intensityImageFileNames[ext].push_back(filename);
00335 UpdateImageDimensionsOnFirstImage(filename, ext);
00336 }
00337 }
00338
00339
00340 for (unsigned int extIndex=0; extIndex<2; extIndex++)
00341 {
00342 std::string ext = extensionList[extIndex];
00343 if ((dir_itr->path().extension() == ext) && filename.find( "RangeCamAmplitude_32F1_" + sCameraIndex, 0 ) != std::string::npos)
00344 {
00345 (amplitudeImageCounter.find(ext) == amplitudeImageCounter.end()) ? amplitudeImageCounter[ext] = 1 : amplitudeImageCounter[ext]++;
00346
00347 amplitudeImageFileNames[ext].push_back(filename);
00348 UpdateImageDimensionsOnFirstImage(filename, ext);
00349 }
00350 }
00351
00352
00353 for (unsigned int extIndex=0; extIndex<2; extIndex++)
00354 {
00355 std::string ext = extensionList[extIndex];
00356 if ((dir_itr->path().extension() == ext) && filename.find( "RangeCamCoordinate_32F3_" + sCameraIndex, 0 ) != std::string::npos)
00357 {
00358 (coordinateImageCounter.find(ext) == coordinateImageCounter.end()) ? coordinateImageCounter[ext] = 1 : coordinateImageCounter[ext]++;
00359 coordinateImageFileNames[ext].push_back(filename);
00360 UpdateImageDimensionsOnFirstImage(filename, ext);
00361
00362 }
00363 }
00364
00365
00366 for (unsigned int extIndex=0; extIndex<2; extIndex++)
00367 {
00368 std::string ext = extensionList[extIndex];
00369 if ((dir_itr->path().extension() == ext) && filename.find( "RangeCamRange_32F1_" + sCameraIndex, 0 ) != std::string::npos)
00370 {
00371 (rangeImageCounter.find(ext) == rangeImageCounter.end()) ? rangeImageCounter[ext] = 1 : rangeImageCounter[ext]++;
00372
00373 rangeImageFileNames[ext].push_back(filename);
00374 UpdateImageDimensionsOnFirstImage(filename, ext);
00375 }
00376 }
00377 }
00378 }
00379 catch ( const std::exception &ex )
00380 {
00381 std::cout << "WARNING - VirtualRangeCam::Open:" << std::endl;
00382 std::cout << "\t ... Exception catch of '" << ex.what() << "'" << std::endl;
00383 }
00384 }
00385
00386 std::map<std::string, int>::iterator itCounter;
00387 std::string extInt = "";
00388 for (itCounter = intensityImageCounter.begin(); itCounter != intensityImageCounter.end(); itCounter++) FindSourceImageFormat(itCounter, extInt);
00389 if (extInt != "") m_IntensityImageFileNames = intensityImageFileNames[extInt];
00390
00391
00392 std::string extAmp = "";
00393 for (itCounter = amplitudeImageCounter.begin(); itCounter != amplitudeImageCounter.end(); itCounter++) FindSourceImageFormat(itCounter, extAmp);
00394 if (extAmp != "") m_AmplitudeImageFileNames = amplitudeImageFileNames[extAmp];
00395
00396
00397 std::string extCoord = "";
00398 for (itCounter = coordinateImageCounter.begin(); itCounter != coordinateImageCounter.end(); itCounter++) FindSourceImageFormat(itCounter, extCoord);
00399 if (extCoord != "") m_CoordinateImageFileNames = coordinateImageFileNames[extCoord];
00400
00401
00402 std::string extRange = "";
00403 for (itCounter = rangeImageCounter.begin(); itCounter != rangeImageCounter.end(); itCounter++) FindSourceImageFormat(itCounter, extRange);
00404 if (extRange != "") m_RangeImageFileNames = rangeImageFileNames[extRange];
00405
00406 std::sort(m_IntensityImageFileNames.begin(),m_IntensityImageFileNames.end());
00407 std::sort(m_AmplitudeImageFileNames.begin(),m_AmplitudeImageFileNames.end());
00408 std::sort(m_CoordinateImageFileNames.begin(),m_CoordinateImageFileNames.end());
00409 std::sort(m_RangeImageFileNames.begin(),m_RangeImageFileNames.end());
00410 std::cout << "INFO - VirtualRangeCam::Open:" << std::endl;
00411 std::cout << "\t ... Extracted '" << intensityImageCounter[extInt] << "' intensity images (3*8 or 16 bit/value)\n";
00412 std::cout << "\t ... Extracted '" << amplitudeImageCounter[extAmp] << "' amplitude images (16 bit/value)\n";
00413 std::cout << "\t ... Extracted '" << coordinateImageCounter[extCoord] << "' coordinate images (3*16 bit/value)\n";
00414 std::cout << "\t ... Extracted '" << rangeImageCounter[extRange] << "' range images (16 bit/value)\n";
00415
00416 if (intensityImageCounter[extInt] == 0 && amplitudeImageCounter[extAmp] == 0)
00417 {
00418 std::cerr << "ERROR - VirtualRangeCam::Open:" << std::endl;
00419 std::cerr << "\t ... Could not detect any intensity or amplitude images" << std::endl;
00420 std::cerr << "\t ... from the specified directory." << std::endl;
00421 return ipa_CameraSensors::RET_FAILED;
00422 }
00423
00424 if (coordinateImageCounter[extCoord] != 0 &&
00425 ((intensityImageCounter[extInt] != coordinateImageCounter[extCoord] &&
00426 amplitudeImageCounter[extAmp] != coordinateImageCounter[extCoord]) ||
00427 (coordinateImageCounter[extCoord] != rangeImageCounter[extRange])))
00428 {
00429 std::cerr << "ERROR - VirtualRangeCam::Open:" << std::endl;
00430 std::cerr << "\t ... Number of intensity, range and coordinate images must agree." << std::endl;
00431 return ipa_CameraSensors::RET_FAILED;
00432 }
00433
00434 if((m_CalibrationMethod == NATIVE || m_CalibrationMethod == MATLAB_NO_Z) && coordinateImageCounter[extCoord] == 0 )
00435 {
00436 std::cerr << "ERROR - VirtualRangeCam::Open:" << std::endl;
00437 std::cerr << "\t ... Coordinate images must be available for calibration mode NATIVE or MATLAB_NO_Z." << std::endl;
00438 return ipa_CameraSensors::RET_FAILED;
00439 }
00440 }
00441 else
00442 {
00443 std::cerr << "ERROR - VirtualRangeCam::Open():" << std::endl;
00444 std::cerr << "\t ... Path '" << absoluteDirectoryName.file_string() << "' is not a directory." << std::endl;
00445 return ipa_CameraSensors::RET_FAILED;
00446 }
00447
00448
00449 std::cout << "**************************************************" << std::endl;
00450 std::cout << "VirtualRangeCam::Open(): Virtual range camera OPEN" << std::endl;
00451 std::cout << "**************************************************" << std::endl<< std::endl;
00452 m_open = true;
00453
00454 return RET_OK;
00455 }
00456
00457
00458 unsigned long VirtualRangeCam::Close()
00459 {
00460 if (!isOpen())
00461 {
00462 return (RET_OK);
00463 }
00464
00465 m_open = false;
00466 return RET_OK;
00467
00468 }
00469
00470
00471 unsigned long VirtualRangeCam::SetProperty(t_cameraProperty* cameraProperty)
00472 {
00473 switch (cameraProperty->propertyID)
00474 {
00475 case PROP_CAMERA_RESOLUTION:
00476
00477 default:
00478 std::cerr << "ERROR - VirtualRangeCam::SetProperty:" << std::endl;
00479 std::cerr << "\t ... Property " << cameraProperty->propertyID << " unspecified.\n";
00480 return RET_FAILED;
00481 break;
00482 }
00483
00484 return RET_OK;
00485 }
00486
00487
00488 unsigned long VirtualRangeCam::SetPropertyDefaults()
00489 {
00490 return RET_FUNCTION_NOT_IMPLEMENTED;
00491 }
00492
00493
00494 unsigned long VirtualRangeCam::GetProperty(t_cameraProperty* cameraProperty)
00495 {
00496 switch (cameraProperty->propertyID)
00497 {
00498 case PROP_CAMERA_RESOLUTION:
00499 cameraProperty->cameraResolution.xResolution = m_ImageWidth;
00500 cameraProperty->cameraResolution.yResolution = m_ImageHeight;
00501 cameraProperty->propertyType = TYPE_CAMERA_RESOLUTION;
00502 break;
00503
00504 case PROP_DMA_BUFFER_SIZE:
00505 cameraProperty->u_integerData = m_BufferSize;
00506 return RET_OK;
00507 break;
00508
00509 default:
00510 std::cerr << "VirtualRangeCam::GetProperty:" << std::endl;
00511 std::cerr << "\t ... Property " << cameraProperty->propertyID << " unspecified.";
00512 return RET_FAILED;
00513 break;
00514
00515 }
00516
00517 return RET_OK;
00518 }
00519
00520
00521
00522
00523 unsigned long VirtualRangeCam::AcquireImages(cv::Mat* rangeImage, cv::Mat* grayImage, cv::Mat* cartesianImage,
00524 bool getLatestFrame, bool undistort, ipa_CameraSensors::t_ToFGrayImageType grayImageType)
00525 {
00526
00527
00528 char* rangeImageData = 0;
00529 char* grayImageData = 0;
00530 char* cartesianImageData = 0;
00531 int widthStepRange = -1;
00532 int widthStepGray = -1;
00533 int widthStepCartesian = -1;
00534
00535 if(rangeImage)
00536 {
00537 rangeImage->create(m_ImageHeight, m_ImageWidth, CV_32FC1);
00538 rangeImageData = (char*) rangeImage->data;
00539 widthStepRange = rangeImage->step;
00540 }
00541
00542 if(grayImage)
00543 {
00544 if (grayImageType == ipa_CameraSensors::INTENSITY_8U3) grayImage->create(m_ImageHeight, m_ImageWidth, CV_8UC3);
00545 else grayImage->create(m_ImageHeight, m_ImageWidth, CV_32FC1);
00546 grayImageData = (char*) grayImage->data;
00547 widthStepGray = grayImage->step;
00548 }
00549
00550 if(cartesianImage)
00551 {
00552 cartesianImage->create(m_ImageHeight, m_ImageWidth, CV_32FC3);
00553 cartesianImageData = (char*) cartesianImage->data;
00554 widthStepCartesian = cartesianImage->step;
00555 }
00556
00557 if (widthStepRange+widthStepGray+widthStepCartesian == -3)
00558 {
00559 return RET_OK;
00560 }
00561
00562 return AcquireImages(widthStepRange, widthStepGray, widthStepCartesian, rangeImageData, grayImageData, cartesianImageData, getLatestFrame, undistort, grayImageType);
00563
00564 }
00565
00566 unsigned long VirtualRangeCam::AcquireImages(int widthStepRange, int widthStepGray, int widthStepCartesian, char* rangeImageData, char* grayImageData, char* cartesianImageData,
00567 bool getLatestFrame, bool undistort, ipa_CameraSensors::t_ToFGrayImageType grayImageType)
00568 {
00569 if (!m_open)
00570 {
00571 std::cerr << "ERROR - VirtualRangeCam::AcquireImages:" << std::endl;
00572 std::cerr << "\t ... Camera not open." << std::endl;
00573 return (RET_FAILED | RET_CAMERA_NOT_OPEN);
00574 }
00575
00577
00579 if (rangeImageData)
00580 {
00581 float* f_ptr = 0;
00582 float* f_ptr_dst = 0;
00583 bool releaseNecessary = false;
00584
00585 cv::Mat rangeMat;
00586 IplImage rangeIpl;
00587 IplImage* rangeImage = &rangeIpl;
00588 if (m_RangeImageFileNames[m_ImageCounter].find(".bin") != std::string::npos)
00589 {
00590 ipa_Utils::LoadMat(rangeMat, m_RangeImageFileNames[m_ImageCounter]);
00591 rangeIpl = (IplImage)rangeMat;
00592 }
00593 else if (m_RangeImageFileNames[m_ImageCounter].find(".xml") != std::string::npos)
00594 {
00595 rangeImage = (IplImage*) cvLoad(m_RangeImageFileNames[m_ImageCounter].c_str(), 0);
00596 releaseNecessary = true;
00597 }
00598 else
00599 {
00600 std::cerr << "ERROR - VirtualRangeCam::AcquireImages:\n";
00601 std::cerr << "\t ... Wrong file format for file " << m_RangeImageFileNames[m_ImageCounter] << ".\n";
00602 CV_Assert(false);
00603 }
00604
00605 if (!undistort)
00606 {
00607
00608 for(unsigned int row=0; row<(unsigned int)m_ImageHeight; row++)
00609 {
00610 f_ptr = (float*) (rangeImage->imageData + row*rangeImage->widthStep);
00611 f_ptr_dst = (float*) (rangeImageData + row*widthStepRange);
00612
00613 for (unsigned int col=0; col<(unsigned int)m_ImageWidth; col++)
00614 {
00615 f_ptr_dst[col] = f_ptr[col];
00616 }
00617 }
00618 }
00619 else
00620 {
00621 cv::Mat undistortedData (m_ImageHeight, m_ImageWidth, CV_32FC1, (float*) rangeImageData);
00622 cv::Mat cpp_rangeImage = rangeImage;
00623
00624 assert (!m_undistortMapX.empty() && !m_undistortMapY.empty());
00625 cv::remap(cpp_rangeImage, undistortedData, m_undistortMapX, m_undistortMapY, cv::INTER_LINEAR);
00626 }
00627
00628 if (releaseNecessary) cvReleaseImage(&rangeImage);
00629 }
00631
00633 if(grayImageData)
00634 {
00635 float* f_ptr = 0;
00636 float* f_ptr_dst = 0;
00637 unsigned char* uc_ptr = 0;
00638 unsigned char* uc_ptr_dst = 0;
00639 cv::Mat grayMat;
00640 IplImage grayIpl;
00641 IplImage* grayImage = &grayIpl;
00642 bool releaseNecessary = false;
00643
00644
00645 if ((grayImageType == ipa_CameraSensors::INTENSITY_32F1) || (grayImageType == ipa_CameraSensors::INTENSITY_8U3))
00646 {
00647
00648 if (m_IntensityImageFileNames[m_ImageCounter].find(".bin") != std::string::npos)
00649 {
00650 ipa_Utils::LoadMat(grayMat, m_IntensityImageFileNames[m_ImageCounter]);
00651 grayIpl = (IplImage)grayMat;
00652 }
00653 else if (m_IntensityImageFileNames[m_ImageCounter].find(".xml") != std::string::npos)
00654 {
00655 grayImage = (IplImage*) cvLoad(m_IntensityImageFileNames[m_ImageCounter].c_str());
00656 releaseNecessary = true;
00657 }
00658 else if ((m_IntensityImageFileNames[m_ImageCounter].find(".png") != std::string::npos) || (m_IntensityImageFileNames[m_ImageCounter].find(".bmp") != std::string::npos) ||
00659 (m_IntensityImageFileNames[m_ImageCounter].find(".jpg") != std::string::npos))
00660 {
00661 grayMat = cv::imread(m_IntensityImageFileNames[m_ImageCounter], -1);
00662 grayIpl = (IplImage)grayMat;
00663 }
00664 else
00665 {
00666 std::cerr << "ERROR - VirtualRangeCam::AcquireImages:\n";
00667 std::cerr << "\t ... Wrong file format for file " << m_IntensityImageFileNames[m_ImageCounter] << ".\n";
00668 CV_Assert(false);
00669 }
00670 }
00671 else
00672 {
00673
00674 if (m_AmplitudeImageFileNames[m_ImageCounter].find(".bin") != std::string::npos)
00675 {
00676 ipa_Utils::LoadMat(grayMat, m_AmplitudeImageFileNames[m_ImageCounter]);
00677 grayIpl = (IplImage)grayMat;
00678 }
00679 else if (m_AmplitudeImageFileNames[m_ImageCounter].find(".xml") != std::string::npos)
00680 {
00681 grayImage = (IplImage*) cvLoad(m_AmplitudeImageFileNames[m_ImageCounter].c_str());
00682 releaseNecessary = true;
00683 }
00684 else
00685 {
00686 std::cerr << "ERROR - VirtualRangeCam::AcquireImages:\n";
00687 std::cerr << "\t ... Wrong file format for file " << m_AmplitudeImageFileNames[m_ImageCounter] << ".\n";
00688 CV_Assert(false);
00689 }
00690 }
00691
00692
00693 if (!undistort)
00694 {
00695 if (grayImageType == ipa_CameraSensors::INTENSITY_8U3)
00696 {
00697 for(unsigned int row=0; row<(unsigned int)m_ImageHeight; row++)
00698 {
00699 f_ptr = (float*) (grayImage->imageData + row*grayImage->widthStep);
00700 f_ptr_dst = (float*) (grayImageData + row*widthStepGray);
00701
00702 for (unsigned int col=0; col<(unsigned int)m_ImageWidth; col++)
00703 {
00704 f_ptr_dst[col] = f_ptr[col];
00705 }
00706 }
00707 }
00708 else
00709 {
00710 for(unsigned int row=0; row<(unsigned int)m_ImageHeight; row++)
00711 {
00712 uc_ptr = (unsigned char*) (grayImage->imageData + row*grayImage->widthStep);
00713 uc_ptr_dst = (unsigned char*) (grayImageData + row*widthStepGray);
00714
00715 for (unsigned int col=0; col<(unsigned int)m_ImageWidth; col++)
00716 {
00717 uc_ptr_dst[col] = uc_ptr[col];
00718 }
00719 }
00720 }
00721 }
00722 else
00723 {
00724 cv::Mat undistortedData;
00725 if (grayImageType == ipa_CameraSensors::INTENSITY_8U3)
00726 {
00727 undistortedData = cv::Mat(m_ImageHeight, m_ImageWidth, CV_8UC3, (unsigned char*) grayImageData);
00728 }
00729 else
00730 {
00731 undistortedData = cv::Mat(m_ImageHeight, m_ImageWidth, CV_32FC1, (float*) grayImageData);
00732 }
00733 cv::Mat cpp_grayImage = grayImage;
00734
00735 assert (!m_undistortMapX.empty() && !m_undistortMapY.empty());
00736 cv::remap(cpp_grayImage, undistortedData, m_undistortMapX, m_undistortMapY, cv::INTER_LINEAR);
00737 }
00738
00739 if (releaseNecessary) cvReleaseImage(&grayImage);
00740 }
00742
00744 if(cartesianImageData)
00745 {
00746 float x = -1;
00747 float y = -1;
00748 float zCalibrated = -1;
00749 float* f_ptr = 0;
00750 float* f_ptr_dst = 0;
00751 bool releaseNecessary = false;
00752
00753 if(m_CalibrationMethod==MATLAB)
00754 {
00755 CV_Assert(false);
00756 }
00757 else if(m_CalibrationMethod==MATLAB_NO_Z)
00758 {
00759
00760
00761
00762 cv::Mat coordinateMat;
00763 IplImage coordinateIpl;
00764 IplImage* coordinateImage = &coordinateIpl;
00765 if (m_CoordinateImageFileNames[m_ImageCounter].find(".bin") != std::string::npos)
00766 {
00767 ipa_Utils::LoadMat(coordinateMat, m_CoordinateImageFileNames[m_ImageCounter]);
00768 coordinateIpl = (IplImage)coordinateMat;
00769 }
00770 else if (m_CoordinateImageFileNames[m_ImageCounter].find(".xml") != std::string::npos)
00771 {
00772 coordinateImage = (IplImage*) cvLoad(m_CoordinateImageFileNames[m_ImageCounter].c_str(), 0);
00773 releaseNecessary = true;
00774 }
00775 else
00776 {
00777 std::cerr << "ERROR - VirtualRangeCam::AcquireImages:\n";
00778 std::cerr << "\t ... Wrong file format for file " << m_CoordinateImageFileNames[m_ImageCounter] << ".\n";
00779 CV_Assert(false);
00780 }
00781
00782 for(unsigned int row=0; row<(unsigned int)m_ImageHeight; row++)
00783 {
00784 f_ptr = (float*) (coordinateImage->imageData + row*coordinateImage->widthStep);
00785 f_ptr_dst = (float*) (cartesianImageData + row*widthStepCartesian);
00786
00787 for (unsigned int col=0; col<(unsigned int)m_ImageWidth; col++)
00788 {
00789 int colTimes3 = 3*col;
00790
00791 zCalibrated = f_ptr[colTimes3+2];
00792 GetCalibratedXYMatlab(col, row, zCalibrated, x, y);
00793
00794 f_ptr_dst[colTimes3] = x;
00795 f_ptr_dst[colTimes3 + 1] = y;
00796 f_ptr_dst[colTimes3 + 2] = zCalibrated;
00797
00798 if (f_ptr_dst[colTimes3 + 2] < 0)
00799 {
00800 std::cout << "<0: " << row << " " << col << "\n";
00801 }
00802 }
00803 }
00804 if (releaseNecessary) cvReleaseImage(&coordinateImage);
00805 }
00806 else if(m_CalibrationMethod==NATIVE)
00807 {
00808 cv::Mat coordinateMat;
00809 IplImage coordinateIpl;
00810 IplImage* coordinateImage = &coordinateIpl;
00811 if (m_CoordinateImageFileNames[m_ImageCounter].find(".bin") != std::string::npos)
00812 {
00813 ipa_Utils::LoadMat(coordinateMat, m_CoordinateImageFileNames[m_ImageCounter]);
00814 coordinateIpl = (IplImage)coordinateMat;
00815 }
00816 else if (m_CoordinateImageFileNames[m_ImageCounter].find(".xml") != std::string::npos)
00817 {
00818 coordinateImage = (IplImage*) cvLoad(m_CoordinateImageFileNames[m_ImageCounter].c_str(), 0);
00819 releaseNecessary = true;
00820 }
00821 else
00822 {
00823 std::cerr << "ERROR - VirtualRangeCam::AcquireImages:\n";
00824 std::cerr << "\t ... Wrong file format for file " << m_CoordinateImageFileNames[m_ImageCounter] << ".\n";
00825 CV_Assert(false);
00826 }
00827
00828 for(unsigned int row=0; row<(unsigned int)m_ImageHeight; row++)
00829 {
00830 f_ptr = (float*) (coordinateImage->imageData + row*coordinateImage->widthStep);
00831 f_ptr_dst = (float*) (cartesianImageData + row*widthStepCartesian);
00832
00833 for (unsigned int col=0; col<(unsigned int)m_ImageWidth; col++)
00834 {
00835 int colTimes3 = 3*col;
00836
00837 f_ptr_dst[colTimes3] =f_ptr[colTimes3+0];;
00838 f_ptr_dst[colTimes3 + 1] = f_ptr[colTimes3+1];
00839 f_ptr_dst[colTimes3 + 2] = f_ptr[colTimes3+2];
00840 }
00841 }
00842 if (releaseNecessary) cvReleaseImage(&coordinateImage);
00843 }
00844 else
00845 {
00846 std::cerr << "ERROR - VirtualRangeCam::AcquireImages:" << std::endl;
00847 std::cerr << "\t ... Calibration method unknown.\n";
00848 return RET_FAILED;
00849 }
00850 }
00851
00852 std::cout << m_ImageCounter << " \r" << std::endl;
00853 m_ImageCounter++;
00854 if ((m_IntensityImageFileNames.size() != 0 && m_ImageCounter >= m_IntensityImageFileNames.size()) ||
00855 (m_AmplitudeImageFileNames.size() != 0 && m_ImageCounter >= m_AmplitudeImageFileNames.size()) ||
00856 (m_RangeImageFileNames.size() != 0 && m_ImageCounter >= m_RangeImageFileNames.size()) ||
00857 (m_CoordinateImageFileNames.size() != 0 && m_ImageCounter >= m_CoordinateImageFileNames.size()))
00858 {
00859
00860 m_ImageCounter = 0;
00861 }
00862
00863 return RET_OK;
00864 }
00865
00866 int VirtualRangeCam::GetNumberOfImages()
00867 {
00868 if (m_IntensityImageFileNames.size() == 0 &&
00869 m_AmplitudeImageFileNames.size() == 0 &&
00870 m_RangeImageFileNames.size() == 0 &&
00871 m_CoordinateImageFileNames.size() == 0)
00872 {
00873 return 0;
00874 }
00875
00876 int min=std::numeric_limits<int>::max();
00877
00878 if (m_IntensityImageFileNames.size() != 0) min = (int)std::min((float)min, (float)m_IntensityImageFileNames.size());
00879 if (m_AmplitudeImageFileNames.size() != 0) min = (int)std::min((float)min, (float)m_AmplitudeImageFileNames.size());
00880 if (m_RangeImageFileNames.size() != 0) min = (int)std::min((float)min, (float)m_RangeImageFileNames.size());
00881 if (m_CoordinateImageFileNames.size() != 0) min = (int)std::min((float)min, (float)m_CoordinateImageFileNames.size());
00882
00883 return min;
00884 }
00885
00886 unsigned long VirtualRangeCam::SetPathToImages(std::string path)
00887 {
00888 m_CameraDataDirectory = path;
00889 return ipa_Utils::RET_OK;
00890 }
00891
00892 unsigned long VirtualRangeCam::SaveParameters(const char* filename)
00893 {
00894 return RET_FUNCTION_NOT_IMPLEMENTED;
00895 }
00896
00897
00898
00899
00900 unsigned long VirtualRangeCam::GetCalibratedZMatlab(int u, int v, float zRaw, float& zCalibrated)
00901 {
00902 double c[7] = {m_CoeffsA0.at<double>(v,u), m_CoeffsA1.at<double>(v,u), m_CoeffsA2.at<double>(v,u),
00903 m_CoeffsA3.at<double>(v,u), m_CoeffsA4.at<double>(v,u), m_CoeffsA5.at<double>(v,u), m_CoeffsA6.at<double>(v,u)};
00904 double y = 0;
00905
00906 ipa_Utils::EvaluatePolynomial((double) zRaw, 6, &c[0], &y);
00907
00908 zCalibrated = (float) y;
00909 return RET_OK;
00910 }
00911
00912 unsigned long VirtualRangeCam::GetCalibratedXYMatlab(int u, int v, float z, float& x, float& y)
00913 {
00914
00915 z *= 1000;
00916
00917
00918 double fx, fy, cx, cy;
00919
00920 fx = m_intrinsicMatrix.at<double>(0, 0);
00921 fy = m_intrinsicMatrix.at<double>(1, 1);
00922
00923 cx = m_intrinsicMatrix.at<double>(0, 2);
00924 cy = m_intrinsicMatrix.at<double>(1, 2);
00925
00926
00927 if (fx == 0)
00928 {
00929 std::cerr << "VirtualRangeCam::GetCalibratedXYMatlab:" << std::endl;
00930 std::cerr << "\t ... fx is 0.\n";
00931 return RET_FAILED;
00932 }
00933 x = (float) (z*(u-cx)/fx) ;
00934
00935
00936 if (fy == 0)
00937 {
00938 std::cerr << "VirtualRangeCam::GetCalibratedXYMatlab:" << std::endl;
00939 std::cerr << "\t ... fy is 0.\n";
00940 return RET_FAILED;
00941 }
00942 y = (float) (z*(v-cy)/fy);
00943
00944
00945 x /= 1000;
00946 y /= 1000;
00947
00948 return RET_OK;
00949 }
00950
00951 unsigned long VirtualRangeCam::GetCalibratedUV(double x, double y, double z, double& u, double& v)
00952 {
00953 if(m_CalibrationMethod==MATLAB || m_CalibrationMethod==MATLAB_NO_Z)
00954 {
00955 double fx, fy, cx, cy;
00956 fx = m_intrinsicMatrix.at<double>(0, 0);
00957 fy = m_intrinsicMatrix.at<double>(1, 1);
00958
00959 cx = m_intrinsicMatrix.at<double>(0, 2);
00960 cy = m_intrinsicMatrix.at<double>(1, 2);
00961
00962
00963 x *= 1000;
00964 y *= 1000;
00965 z *= 1000;
00966
00967
00968 if (z == 0)
00969 {
00970 std::cerr << "ERROR - VirtualRangeCam::GetCalibratedUV:" << std::endl;
00971 std::cerr << "\t ... z is 0.\n";
00972 return RET_FAILED;
00973 }
00974
00975 u = (fx*x)/z + cx;
00976 v = (fy*y)/z + cy;
00977 }
00978 else if(m_CalibrationMethod==NATIVE)
00979 {
00980
00981 std::cerr << "ERROR - VirtualRangeCam::GetCalibratedUV:" << std::endl;
00982 std::cerr << "\t ... Function not implemented.\n";
00983 return RET_FAILED;
00984 }
00985
00986
00987
00988 if(u<0) u=0;
00989 if(u>=m_ImageWidth) u=m_ImageWidth-1;
00990 if(v<0) v=0;
00991 if(v>=m_ImageHeight) v=m_ImageHeight-1;
00992
00993 return RET_OK;
00994 }
00995
00996 unsigned long VirtualRangeCam::LoadParameters(const char* filename, int cameraIndex)
00997 {
00998
00999 boost::shared_ptr<TiXmlDocument> p_configXmlDocument (new TiXmlDocument( filename ));
01000
01001 if (!p_configXmlDocument->LoadFile())
01002 {
01003 std::cerr << "ERROR - VirtualRangeCam::LoadParameters:" << std::endl;
01004 std::cerr << "\t ... Error while loading xml configuration file" << std::endl;
01005 std::cerr << "\t ...(Check filename and syntax):" << std::endl;
01006 std::cerr << "\t ... " << filename << std::endl;
01007 return (RET_FAILED | RET_FAILED_OPEN_FILE);
01008 }
01009 std::cout << "INFO - VirtualRangeCam::LoadParameters:" << std::endl;
01010 std::cout << "\t ... Parsing xml configuration file" << std::endl;
01011 std::cout << "\t ... '" << filename << "'" << std::endl;
01012
01013 if ( p_configXmlDocument )
01014 {
01015
01016
01017
01018
01019
01020 TiXmlElement *p_xmlElement_Root = NULL;
01021 p_xmlElement_Root = p_configXmlDocument->FirstChildElement( "LibCameraSensors" );
01022
01023 if ( p_xmlElement_Root )
01024 {
01025
01026
01027
01028
01029
01030 TiXmlElement *p_xmlElement_Root_VirtualRangeCam = NULL;
01031 std::stringstream ss;
01032 ss << "VirtualRangeCam_" << cameraIndex;
01033 p_xmlElement_Root_VirtualRangeCam = p_xmlElement_Root->FirstChildElement( ss.str() );
01034 if ( p_xmlElement_Root_VirtualRangeCam )
01035 {
01036
01037
01038
01039
01040
01041 TiXmlElement *p_xmlElement_Child = NULL;
01042 p_xmlElement_Child = p_xmlElement_Root_VirtualRangeCam->FirstChildElement( "CameraDataDirectory" );
01043 if ( p_xmlElement_Child )
01044 {
01045
01046 std::string tempString;
01047 if ( p_xmlElement_Child->QueryValueAttribute( "relativePath", &tempString ) != TIXML_SUCCESS)
01048 {
01049 std::cerr << "ERROR - VirtualRangeCam::LoadParameters:" << std::endl;
01050 std::cerr << "\t ... Can't find attribute 'relativePath' of tag 'CameraDataDirectory'." << std::endl;
01051 return (RET_FAILED | RET_XML_ATTR_NOT_FOUND);
01052 }
01053
01054 m_CameraDataDirectory = m_CameraDataDirectory + tempString + "/";
01055 }
01056 else
01057 {
01058 std::cerr << "ERROR - VirtualColorCam::LoadParameters:" << std::endl;
01059 std::cerr << "\t ... Can't find tag 'CameraDataDirectory'." << std::endl;
01060 return (RET_FAILED | RET_XML_TAG_NOT_FOUND);
01061 }
01062
01063
01064
01065
01066
01067 p_xmlElement_Child = NULL;
01068 p_xmlElement_Child = p_xmlElement_Root_VirtualRangeCam->FirstChildElement( "CalibrationMethod" );
01069 std::string tempString;
01070 if ( p_xmlElement_Child )
01071 {
01072
01073 if ( p_xmlElement_Child->QueryValueAttribute( "name", &tempString ) != TIXML_SUCCESS)
01074 {
01075 std::cerr << "ERROR - VirtualRangeCam::LoadParameters:" << std::endl;
01076 std::cerr << "\t ... Can't find attribute 'name' of tag 'CalibrationMethod'." << std::endl;
01077 return (RET_FAILED | RET_XML_ATTR_NOT_FOUND);
01078 }
01079 if (tempString == "MATLAB") m_CalibrationMethod = MATLAB;
01080 else if (tempString == "MATLAB_NO_Z") m_CalibrationMethod = MATLAB_NO_Z;
01081 else if (tempString == "NATIVE") m_CalibrationMethod = NATIVE;
01082 else
01083 {
01084 std::cerr << "ERROR - VirtualRangeCam::LoadParameters:" << std::endl;
01085 std::cerr << "\t ... Calibration mode " << tempString << " unspecified." << std::endl;
01086 return (RET_FAILED);
01087 }
01088 }
01089 else
01090 {
01091 std::cerr << "ERROR - VirtualRangeCam::LoadParameters:" << std::endl;
01092 std::cerr << "\t ... Can't find tag 'CalibrationMethod'." << std::endl;
01093 return (RET_FAILED | RET_XML_TAG_NOT_FOUND);
01094 }
01095 }
01096
01097
01098
01099 else
01100 {
01101 std::cerr << "ERROR - VirtualRangeCam::LoadParameters:" << std::endl;
01102 std::cerr << "\t ... Can't find tag '" << ss.str() << "'" << std::endl;
01103 return (RET_FAILED | RET_XML_TAG_NOT_FOUND);
01104 }
01105 }
01106
01107
01108
01109
01110 else
01111 {
01112 std::cerr << "ERROR - VirtualRangeCam::LoadParameters:" << std::endl;
01113 std::cerr << "\t ... Can't find tag 'LibCameraSensors'." << std::endl;
01114 return (RET_FAILED | RET_XML_TAG_NOT_FOUND);
01115 }
01116 }
01117
01118 std::cout << "\t ... Parsing xml calibration file ... [OK] \n";
01119
01120 return RET_OK;
01121 }