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


ccny_rgbd
Author(s): Ivan Dryanovski
autogenerated on Fri Apr 12 2013 20:38:48