rgbd_keyframe.cpp
Go to the documentation of this file.
00001 
00024 #include "rgbdtools/rgbd_keyframe.h"
00025 
00026 namespace rgbdtools {
00027 
00028 RGBDKeyframe::RGBDKeyframe():
00029   manually_added(false)
00030 {
00031  
00032 }
00033 
00034 RGBDKeyframe::RGBDKeyframe(const RGBDFrame& frame):
00035   RGBDFrame(),
00036   manually_added(false)
00037 {
00038   // copies the data
00039   rgb_img   = frame.rgb_img.clone();
00040   depth_img = frame.depth_img.clone();
00041   intr      = frame.intr.clone();
00042   dist      = frame.dist.clone();
00043   header    = frame.header;
00044   
00045   // this preserves the index number of the original frame
00046   index = frame.index;
00047 }
00048 
00049 bool RGBDKeyframe::save(
00050   const RGBDKeyframe& keyframe, 
00051   const std::string& path)
00052 {  
00053   std::string pose_filename  = path + "/pose.yaml";
00054   std::string prop_filename  = path + "/properties.yaml"; 
00055 
00056   // save frame  
00057   bool save_frame_result = RGBDFrame::save(keyframe, path);
00058   if (!save_frame_result) return false;
00059   
00060   // save pose as OpenCV rmat and tvec
00061   cv::Mat rmat, tvec;
00062   eigenAffineToOpenCVRt(keyframe.pose, rmat, tvec);
00063 
00064   cv::FileStorage fs_m(pose_filename, cv::FileStorage::WRITE);
00065   fs_m << "rmat" << rmat;
00066   fs_m << "tvec" << tvec;
00067  
00068   // save other class members 
00069   cv::FileStorage fs_p(prop_filename, cv::FileStorage::WRITE);
00070   fs_p << "manually_added"      << keyframe.manually_added;
00071   fs_p << "path_length_linear"  << keyframe.path_length_linear;
00072   fs_p << "path_length_angular" << keyframe.path_length_angular;
00073 
00074   return true;
00075 }
00076 
00077 bool RGBDKeyframe::load(RGBDKeyframe& keyframe, const std::string& path)
00078 {
00079   // load frame
00080   bool load_frame_result = RGBDFrame::load(keyframe, path);
00081   if (!load_frame_result) return false;
00082 
00083   // set up filenames
00084   std::string pose_filename  = path + "/pose.yaml";
00085   std::string prop_filename  = path + "/properties.yaml"; 
00086 
00087   // check if files exist
00088   if (!boost::filesystem::exists(pose_filename)  ||
00089       !boost::filesystem::exists(prop_filename)  )
00090   {
00091     std::cerr << "files for loading keyframe not found" <<std::endl;
00092     return false;
00093   }
00094 
00095   // load pose
00096   cv::FileStorage fs_m(pose_filename, cv::FileStorage::READ);
00097 
00098   cv::Mat rmat, tvec;
00099   fs_m["rmat"] >> rmat;
00100   fs_m["tvec"] >> tvec;
00101 
00102   openCVRtToEigenAffine(rmat, tvec, keyframe.pose);
00103 
00104   // load other class members
00105   cv::FileStorage fs_p(prop_filename, cv::FileStorage::READ);
00106   fs_p["manually_added"]      >> keyframe.manually_added;
00107   fs_p["path_length_linear"]  >> keyframe.path_length_linear;
00108   fs_p["path_length_angular"] >> keyframe.path_length_angular;
00109   return true;
00110 }
00111 
00112 bool saveKeyframes(
00113   const KeyframeVector& keyframes, 
00114   const std::string& path)
00115 {
00116   for (unsigned int kf_idx = 0; kf_idx < keyframes.size(); ++kf_idx)
00117   {
00118     std::stringstream ss_idx;
00119     ss_idx << std::setw(4) << std::setfill('0') << kf_idx;
00120     
00121     std::string kf_path = path + "/" + ss_idx.str();
00122 
00123     bool save_result = RGBDKeyframe::save(keyframes[kf_idx], kf_path); 
00124     if (!save_result) return false;
00125   }
00126 
00127   return true;
00128 }
00129 
00130 bool loadKeyframes(
00131   KeyframeVector& keyframes, 
00132   const std::string& path)
00133 {
00134   keyframes.clear();
00135 
00136   int kf_idx = 0;
00137 
00138   while(true)
00139   {
00140     std::stringstream ss_idx;
00141     ss_idx << std::setw(4) << std::setfill('0') << kf_idx;
00142 
00143     std::string path_kf = path + "/" + ss_idx.str();
00144 
00145     if (boost::filesystem::exists(path_kf))
00146     {
00147       std::cout << "Loading " << path_kf << std::endl;
00148       RGBDKeyframe keyframe;
00149       bool result_load = RGBDKeyframe::load(keyframe, path_kf);
00150       if (result_load) keyframes.push_back(keyframe);
00151       else
00152       {
00153         std::cerr << "Error loading" << std::endl; 
00154         return false;
00155       }
00156     } 
00157     else break;
00158 
00159     kf_idx++;
00160   }
00161   for(u_int i = 0; i< keyframes.size(); i++)
00162   {
00163       keyframes[i].used = true;
00164   }
00165   return true;
00166 }
00167 
00168 } // namespace rgbdtools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends


lib_rgbdtools
Author(s): Ivan Dryanovski
autogenerated on Tue Aug 27 2013 10:33:54