bin 文件读取储存



typedef struct Frame_Data{
std::vector<cv::KeyPoint> vkeypoints;
cv::Mat vdescriptors;
std::vector<cv::Point3d> vkpts_3d;
cv::Point3d gps;
}Frame_Data;



void SaveBin(std::vector<Frame_Data> &vframe_data, std::string &save_path){ ofstream outfile(save_path, ios::out|ios::binary); int num = vframe_data.size(); outfile.write((char*)& num,sizeof(int)); for(int i = 0; i < vframe_data.size();++i){ outfile.write((char*) &vframe_data[i],sizeof(vframe_data[i])); } outfile.close(); } /// 加载bin文件 void LoadBin(const std::string &bin_path, std::vector<Frame_Data> &vframe_data) { int num; ifstream inFile(bin_path ,ios::in|ios::binary); //二进制读方式打开 if(!inFile){ std::cout << "Open file error!" << std::endl; exit(-1); } inFile.read((char *)&num,sizeof (int)); vframe_data.reserve(num); int i=0; while(inFile.read((char *)&vframe_data[i], sizeof(vframe_data[i]))){ std::cout << vframe_data[i].vdescriptors <<" "<< vframe_data[i].vdescriptors.type() <<std::endl; std::cout << vframe_data[i].gps <<std::endl; std::cout << vframe_data[i].vkeypoints[0].pt <<std::endl; std::cout << vframe_data[i].vkpts_3d << std::endl; std::cout<<i<<std::endl; i++; } inFile.close(); }

int main(){
  
std::cout << "1---------------------------------------"<<std::endl; std::string save_path = "1.bin"; std::cout << "2---------------------------------------"<<std::endl; vector< Frame_Data> s; vector< Frame_Data> s3; Frame_Data s1; Frame_Data s2; cv::Mat vdescriptors; cv::Point3d vkpts_3d; cv::Point3d gps; cv::KeyPoint vkeypoints;   vkeypoints.pt.x = 178; vkeypoints.pt.y = 234; std::cout << "3---------------------------------------"<<std::endl; vkpts_3d.x = 122; vkpts_3d.y = 34; vkpts_3d.z = 100; std::cout << "4---------------------------------------"<<std::endl; gps.x = 122; gps.y = 12; gps.z = 100; std::cout << "5---------------------------------------"<<std::endl; vdescriptors = (cv::Mat_<double>(3,3) << 1,0,0,0,1,0,0,0,1); std::cout << "5---------------------------------------"<<std::endl; // vdescriptors = 0; s1.vkpts_3d.push_back(vkpts_3d); s1.vkpts_3d.push_back(vkpts_3d); s1.vkpts_3d.push_back(vkpts_3d); // s[0].vkpts_3d.push_back(vkpts_3d); std::cout << "6---------------------------------------"<<std::endl; s1.gps = gps;   s1.vdescriptors = vdescriptors; std::cout << "7---------------------------------------"<<std::endl; s1.vkeypoints.push_back(vkeypoints); s1.vkeypoints.push_back(vkeypoints); s.push_back(s1);     s2.vkpts_3d.push_back(vkpts_3d); s2.vkpts_3d.push_back(vkpts_3d); s2.vkeypoints.push_back(vkeypoints); s2.vdescriptors = vdescriptors; s2.gps = gps;   s.push_back(s2); // s[0].vkeypoints.push_back(vkeypoints); std::cout << "---------------------------------------"<<std::endl; SaveBin(s,save_path); std::cout << "---------------------------------------"<<std::endl; LoadBin(save_path,s3); std::cout << "s3:" <<s3.size() <<std::endl; std::cout << "---------------------------------------"<<std::endl; std::cout << s3[1].gps << std::endl; std::cout << "---------------------------------------"<<std::endl; std::cout << s3[1].vkpts_3d[0] << std::endl; std::cout << s3[1].vkpts_3d[1] << std::endl;   return 0;


}

 

上一篇:【深度学习 十】swin transformer:屠榜各大cv比赛


下一篇:javascript中alert()与console.log()的区别