Images.cpp
Go to the documentation of this file.
00001 #include "pano_core/Images.h"
00002 
00003 #include <opencv2/core/core.hpp>
00004 #include <opencv2/highgui/highgui.hpp>
00005 #include <opencv2/imgproc/imgproc.hpp>
00006 
00007 #include <iostream>
00008 #include <iomanip>
00009 #include <map>
00010 
00011 using namespace cv;
00012 using std::map;
00013 
00014 namespace pano
00015 {
00016 Images::Images(const cv::Mat& src) :
00017   ondisk_(false), persist_img_(false)
00018 {
00019   load(src);
00020 }
00021 Images::Images(const std::string& fname, const std::string& path) :
00022   ondisk_(false), persist_img_(false)
00023 {
00024   load(fname, path);
00025 }
00026 
00027 void Images::clear()
00028 {
00029   src_ = Mat();
00030   grey_ = Mat();
00031 
00032 }
00033 void Images::load(const cv::Mat& src, bool dogray)
00034 {
00035   src.copyTo(src_);
00036   if (src_.type() == CV_8UC3 && dogray)
00037   {
00038     cvtColor(src_, grey_, CV_RGB2GRAY);
00039   }
00040   else if (src_.type() == CV_8UC1)
00041   {
00042     grey_ = src_;
00043   }
00044   else
00045   {
00046     CV_Error(CV_StsUnsupportedFormat,"only supports 3 channel 8 bit or 1 channel 8 bit images");
00047   }
00048   // integral(grey_, sum_, CV_32S);
00049 
00050 }
00051 void Images::load(const cv::Mat& src, const std::string& fname, const std::string& path,bool persist)
00052 {
00053   fname_ = fname;
00054   path_ = path;
00055   persist_img_ = persist;
00056   ondisk_ = !fname_.empty();
00057   load(src);
00058 }
00059 void Images::load(const std::string& fname, const std::string& path)
00060 {
00061 
00062   fname_ = fname;
00063   path_ = path;
00064   Mat img;
00065   if(!path.empty())
00066     img = imread(path + "/" + fname);
00067   else
00068     img = imread(fname);
00069   CV_Assert(!img.empty())
00070     ;
00071   ondisk_ = true;
00072   persist_img_ = false;
00073   load(img);
00074 
00075 }
00076 
00077 void Images::restore()
00078 {
00079   if (src_.empty() && ondisk_)
00080   {
00081     //std::cout << "attempted to load from " << fname_ << " at " << path_ << std::endl;
00082     load(fname_, path_);
00083   }
00084 }
00085 
00086 void Images::serialize(cv::FileStorage& fs) const
00087 {
00088   if (!ondisk_ && persist_img_)
00089   {
00090     imwrite(path_ + "/" + fname_, src_);
00091   }
00092   fs << "{";
00093   cvWriteComment(*fs, "Images class", 0);
00094   fs << "fname" << fname_;
00095   fs << "path" << path_;
00096   fs << "ondisk" << (int)(ondisk_ || persist_img_);
00097   fs << "persist" << (int)(persist_img_);
00098   fs << "}";
00099 }
00100 void Images::deserialize(const cv::FileNode& fn)
00101 {
00102   fname_ = (string)fn["fname"];
00103   path_ = (string)fn["path"];
00104   ondisk_ = (int)fn["ondisk"];
00105   persist_img_ = (int)fn["persist"];
00106   if (ondisk_)
00107   {
00108     //load(fname_, path_);
00109   }
00110 }
00111 // cv::Rect getEncompassingRect() const;
00112 cv::Mat HugeImage::loadAll() const
00113 {
00114   map<int, string>::const_iterator nit = names_.begin();
00115   map<int, Rect>::const_iterator rit = rois_.begin();
00116   Mat image = Mat::zeros(size_, CV_8UC3);
00117   for (; nit != names_.end(), rit != rois_.end(); ++nit, ++rit)
00118   {
00119     Mat roi = image(rit->second);
00120     Mat img = imread(nit->second);
00121     img.copyTo(roi);
00122   }
00123   return image;
00124 }
00125 void HugeImage::serialize(const std::string& name) const
00126 {
00127   FileStorage fs(name, FileStorage::WRITE);
00128   fs << "big_image" << "[";
00129   map<int, string>::const_iterator nit = names_.begin();
00130   map<int, Rect>::const_iterator rit = rois_.begin();
00131   for (; nit != names_.end(), rit != rois_.end(); ++nit, ++rit)
00132   {
00133     fs << "{" << "id" << rit->first << "roi" << rit->second << "name" << nit->second << "}";
00134   }
00135   fs << "]";
00136 
00137 }
00138 void HugeImage::deserialize(const std::string& name)
00139 {
00140 
00141 }
00142 std::string HugeImage::addName(int id, std::string&prefix)
00143 {
00144   std::stringstream ss;
00145   ss << prefix << "_" << std::setw(5) << std::setfill('0') << id << ".png";
00146 
00147   names_[id] = ss.str();
00148   return names_[id];
00149 }
00150 
00151 }


pano_core
Author(s): Ethan Rublee
autogenerated on Wed Aug 26 2015 16:34:01