Вопрос

I ve got a vector Mat file and I want to store it in txt file. Every mat file has size 1x4500 and I ve got in total 5000 vectors. I tried to store in txtfile with the above code:

void writeMatToFile(cv::Mat& m, const char* filename){

ofstream fout(filename);
for(int i=0; i<m.rows; i++){
    for(int j=0; j<m.cols; j++){
        fout<<m.at<float>(i,j)<<"\t";
    }
    fout<<endl;
}
fout.close();

}

In main:

    string file = "output.txt";
writeMatToFile(image,file.c_str());

However, I got unhandled exception errors.

Это было полезно?

Решение

Write your data in binary mode using your own format. Something like : mat_count|shape1|data1(row_first)|shape2...

std::fstream will do the harder job for you in text mode or binary mode. You should try both to see the final size.

If your Mat data is something like uint8, uint16, the binary mode will be much better. The problem with text-mode is that you need a separator for each single data which add additional bytes to your file. However text-mode can compress data : "1.5" 3 vs 8 bytes (double).

Last thing, if you want absolutely a file in text-mode, you can zip it at the end of your process and see the ratio.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top