1 二进制文件读

 1 void get_map_from_bin(ifstream &fin, cv ::Mat &undistor_map)
 2 {
 3     float temp_buff = 0.0;
 4     if (fin.is_open()) {
 5         for (int i=0; i < undistor_map.rows;i++) {
 6             for (int j=0; j < undistor_map.cols;j++) {
 7                 fin.read((char *)&temp_buff,sizeof(float)); //注意
 8                 undistor_map.ptr<float>(i)[j] = temp_buff;
 9             }
10         }
11         fin.close();
12     }
13     else {
14         std::cout << "map read file open failed" << std::endl;
15     }
16 }
17 
18 
19 int map_file_exit_ = 1;
20 ifstream front_map1_infile("./front_map1", ios::binary);
21 
22 if (!front_map1_infile) {
23     map_file_exit_ = 0;
24 }
25 else {
26     front_map1 = cv::Mat::zeros(cma_image_height, cma_image_width, CV_32FC1);
27     get_map_from_bin(front_map1_infile,front_map1);
28 }

读取速度更快:

 1 void get_map_from_bin(ifstream &fin, cv ::Mat &undistor_map)
 2 {
 3 //    float temp_buff = 0.0;
 4     if (fin.is_open()) {
 5         for (int i=0; i < undistor_map.rows;i++) {
 6 //            for (int j=0; j < undistor_map.cols;j++) {
 7                 fin.read((char *)(undistor_map.ptr<float>(i)),sizeof(float)*undistor_map.cols);
 8 //                fin.read((char *)&temp_buff,sizeof(float));
 9 //                undistor_map.ptr<float>(i)[j] = temp_buff;
10 //            }
11         }
12         fin.close();
13     }
14     else {
15         std::cout << "map read file open failed" << std::endl;
16     }
17 }

 

 

  1. 二进制写
 1 ofstream front_map1_outfile("./front_map1", ios::binary);
 2 
 3 void generate_map_bin(ofstream &fout, cv ::Mat &undistor_map)
 4 {
 5     float temp_buff = 0.0f;
 6     if (fout.is_open()) {
 7         for (int i=0; i < undistor_map.rows;i++) {
 8             for (int j=0; j < undistor_map.cols;j++) {
 9                temp_buff = undistor_map.ptr<float>(i)[j];
10                 fout.write((char *)&temp_buff, sizeof(float));
11             }
12         }
13         fout.close();
14     }
15     else {
16         std::cout << "map write file open failed" << std::endl;
17     }
18 }
View Code
  1. 文本写
 1 ofstream front_map1_outfile("./front_map1.txt");
 2 void generate_map_char(ofstream &fout, cv ::Mat &undistor_map)
 3 {
 4     if (fout.is_open()) {
 5         fout << "{\n";
 6         std::ostringstream ss;
 7         for (int i=0; i < undistor_map.rows;i++) {
 8             for (int j=0; j < undistor_map.cols;j++) {
 9                 ss.clear();
10                 ss << undistor_map.ptr<float>(i)[j];
11                 fout << ss.str() << ", ";
12                 ss.str("");   //注意
13             }
14             fout << "\n";
15         }
16         fout << "};\n";
17         fout.close();
18     }
19     else {
20         std::cout << "file open failed" << std::endl;
21     }
22 }