7/07/2021

opencv Vector CameraParams save(yaml file store) and load(yaml file restore ) using FileNode, FileNodeIterator


Refer to load & save function.

..

std::vector<cv::detail::CameraParams> params;

bool loadCameraParams(std::string fileName){

cv::FileStorage fs(fileName+".yaml", cv::FileStorage::READ);

if(!fs.isOpened())
return false;

/////
int nObjects= 0;
fs["NC"] >> nObjects;
params.clear();
params.resize(nObjects);
/////
cv::FileNode fn = fs["cameras"];
int id=0;
for (cv::FileNodeIterator it = fn.begin(); it != fn.end(); it++,id++)
{
cv::FileNode item = *it;

cv::Mat K, R, t;
double ppx, ppy, focal, aspect;
item["K"] >> K;
item["R"] >> R;
item["t"] >> t;
item["ppx"] >> ppx;
item["ppy"] >> ppy;
item["focal"] >> focal;
item["aspect"] >> aspect;

mg_params[id].K() = (cv::Mat)K;
mg_params[id].R = R;
mg_params[id].t = t;
mg_params[id].ppx = (double)ppx;
mg_params[id].ppy = (double)ppy;
mg_params[id].focal = (double)focal;
mg_params[id].aspect = (double)aspect;
}

return true;
}

bool saveCameraParams(std::string fileName){

cv::FileStorage fs(fileName+".yaml", cv::FileStorage::WRITE);
cv::detail::CameraParams cameras = mg_params[0];
fs << "NC" << int(params.size());

fs << "cameras" << "[";
for (const auto& param : mg_params)
{
fs << "{";
fs << "K" << param.K();
fs << "R" << param.R;
fs << "t" << param.t;
fs << "ppx" << param.ppx;
fs << "ppy" << param.ppy;
fs << "focal" << param.focal;
fs << "aspect" << param.aspect;
fs << "}";
}
fs << "]";
fs.release();
return true;
}

..


Thank you!!

☕️