Go to the documentation of this file.00001
00024 #include <vector>
00025 #include <string>
00026 #include <fstream>
00027 #include <sstream>
00028 #include <iomanip>
00029 #include <opencv/cv.h>
00030 #include "BundleCamera.h"
00031
00032 using namespace DVision::Bundle;
00033 using namespace std;
00034
00035
00036
00037 void CameraFile::Camera::save(const std::string &filename,
00038 const std::string &comment) const
00039 {
00040 fstream f(filename.c_str(), ios::out);
00041 if(!f.is_open()) throw string("BundleCamera: cannot open ") + filename;
00042
00043 if(comment.empty())
00044 f << "# Single camera (opencv reference)" << endl;
00045 else
00046 {
00047 string cm = comment;
00048 replace(cm.begin(), cm.end(), '\n', ' ');
00049 f << "# " << cm << endl;
00050 }
00051 f << "1 0" << endl;
00052
00053 this->save(f);
00054
00055 f.close();
00056 }
00057
00058
00059
00060 void CameraFile::Camera::load(const std::string &filename)
00061 {
00062 fstream f(filename.c_str(), ios::in);
00063 if(!f.is_open()) throw string("BundleCamera: cannot open ") + filename;
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 int ncameras = -1;
00077 string line;
00078 while(!f.eof() && ncameras < 0){
00079 getline(f, line);
00080
00081 if(!f.eof())
00082 {
00083 if(line[0] != '#')
00084 {
00085 stringstream ss(line);
00086 ss >> ncameras;
00087 }
00088 }
00089 }
00090
00091 load(f);
00092
00093 f.close();
00094 }
00095
00096
00097
00098 void CameraFile::Camera::save(std::fstream &f) const
00099 {
00100 f.setf(ios::fixed, ios::floatfield);
00101 f.precision(6);
00102 f << this->f << " " << this->k1 << " " << this->k2 << endl;
00103
00104 for(int r = 0; r < 3; ++r)
00105 {
00106 for(int c = 0; c < 3; ++c)
00107 {
00108 f << setprecision(12) << this->R.at<double>(r,c) << " ";
00109 }
00110 f << endl;
00111 }
00112
00113 for(int r = 0; r < 3; ++r)
00114 f << this->t.at<double>(r,0) << " ";
00115 f << endl;
00116 }
00117
00118
00119
00120 void CameraFile::Camera::load(std::fstream &f)
00121 {
00122 R.create(3, 3, CV_64F);
00123 t.create(3, 1, CV_64F);
00124
00125 string line;
00126 {
00127 getline(f, line);
00128 stringstream ss(line);
00129 ss >> this->f >> k1 >> k2;
00130 }
00131
00132 {
00133 getline(f, line);
00134 stringstream ss(line);
00135
00136 ss >> R.at<double>(0,0)
00137 >> R.at<double>(0,1)
00138 >> R.at<double>(0,2);
00139 }
00140
00141 {
00142 getline(f, line);
00143 stringstream ss(line);
00144
00145 ss >> R.at<double>(1,0)
00146 >> R.at<double>(1,1)
00147 >> R.at<double>(1,2);
00148 }
00149
00150 {
00151 getline(f, line);
00152 stringstream ss(line);
00153
00154 ss >> R.at<double>(2,0)
00155 >> R.at<double>(2,1)
00156 >> R.at<double>(2,2);
00157 }
00158
00159 {
00160 getline(f, line);
00161 stringstream ss(line);
00162
00163 ss >> t.at<double>(0,0)
00164 >> t.at<double>(1,0)
00165 >> t.at<double>(2,0);
00166 }
00167 }
00168
00169
00170
00171 void CameraFile::readFile(const std::string &filename,
00172 std::vector<Camera> &cameras)
00173 {
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 fstream f(filename.c_str(), ios::in);
00186 if(!f.is_open()) throw string("BundleCamera: cannot open ") + filename;
00187
00188 string line;
00189
00190 int ncameras = -1;
00191
00192 while(!f.eof() && ncameras < 0){
00193 getline(f, line);
00194
00195 if(!f.eof())
00196 {
00197 if(line[0] != '#')
00198 {
00199 stringstream ss(line);
00200 ss >> ncameras;
00201 }
00202 }
00203 }
00204
00205 readFromStream(f, ncameras, cameras);
00206
00207 f.close();
00208 }
00209
00210
00211
00212 void CameraFile::readFromStream(std::fstream &f, int N,
00213 std::vector<Camera> &cameras)
00214 {
00215 cameras.resize(N);
00216 for(int i = 0; i < N; ++i)
00217 {
00218 cameras[i].load(f);
00219 }
00220 }
00221
00222
00223
00224 void CameraFile::saveFile(const std::string &filename,
00225 const std::vector<Camera> &cameras)
00226 {
00227 fstream f(filename.c_str(), ios::out);
00228 if(!f.is_open()) throw string("BundleCamera: cannot open ") + filename;
00229
00230 f << "# A contraption file with the bundle.out format in opencv reference system" << endl;
00231 f << cameras.size() << " 0" << endl;
00232
00233 saveToStream(f, cameras);
00234
00235 f.close();
00236 }
00237
00238
00239
00240 void CameraFile::saveToStream(std::fstream &f,
00241 const std::vector<Camera> &cameras)
00242 {
00243 vector<Camera>::const_iterator cit;
00244 for(cit = cameras.begin(); cit != cameras.end(); ++cit)
00245 {
00246 cit->save(f);
00247 }
00248 }
00249
00250
00251