C++将矩阵存到.txt文件, 使用 FILE 或 ofstream
2016-05-02 09:15
405 查看
在C/C++中常常需要查看中间结果,比如:某一个矩阵中的数据的数值情况,在这种情况下常将该数值矩阵存成 .txt文件再查看。比如:
#include<iostream>
#include<stdio.h>
#include"opencv2/highgui/highgui.hpp"
#include"opencv2/imgproc/imgproc.hpp"
#include"opencv2/core/core.hpp"
int main(){
int i, j ;
cv::Mat my_mat( 20, 20, CV_32FC2, cv::Scalar::all(0) ) ;
FILE *fp ;
fp = fopen( "myfile.txt", "w" ) ;
for(i=0; i<my_mat.rows; i++){
for(j=0; j<my_mat.cols; j++){
fprintf(fp, "%3.0f", my_mat.at<float>(i,j) ) ; // the data type should be matched.
// the same as that of my_mat
}
fprintf( fp, "\n" ) ;
}
fclose( fp ) ;
return 1 ;
}
-----------------------------------------------------------------------
fp = fopen( name, mode ) ;
mode : "r" read ,
"w" write, the old contents to be discarded.
"a" append, the old contents to be preserved.
------------------------------------------------------------------------
方法2. 使用 ofstream 对象来保存文件
// 这里假设需要保存图像 img_gray 的灰度值 到文件夹
std::string fileName = "val_img.txt" ;
std::ofstream outfile( fileName.c_str() ) ; // file name and the operation type.
int i, j ;
for( i=0; i<img_gray.rows; i++ ){
for( j=0; j<img_gray.cols; j++ )
outfile << (int) img_gray.at<uchar>(i,j) << " " ; // note the uchar, be changed to int type.
outfile << std::endl ; // a newline after storing all the values of a line of the img
}
outfile.close() ;
#include<iostream>
#include<stdio.h>
#include"opencv2/highgui/highgui.hpp"
#include"opencv2/imgproc/imgproc.hpp"
#include"opencv2/core/core.hpp"
int main(){
int i, j ;
cv::Mat my_mat( 20, 20, CV_32FC2, cv::Scalar::all(0) ) ;
FILE *fp ;
fp = fopen( "myfile.txt", "w" ) ;
for(i=0; i<my_mat.rows; i++){
for(j=0; j<my_mat.cols; j++){
fprintf(fp, "%3.0f", my_mat.at<float>(i,j) ) ; // the data type should be matched.
// the same as that of my_mat
}
fprintf( fp, "\n" ) ;
}
fclose( fp ) ;
return 1 ;
}
-----------------------------------------------------------------------
fp = fopen( name, mode ) ;
mode : "r" read ,
"w" write, the old contents to be discarded.
"a" append, the old contents to be preserved.
------------------------------------------------------------------------
方法2. 使用 ofstream 对象来保存文件
// 这里假设需要保存图像 img_gray 的灰度值 到文件夹
std::string fileName = "val_img.txt" ;
std::ofstream outfile( fileName.c_str() ) ; // file name and the operation type.
int i, j ;
for( i=0; i<img_gray.rows; i++ ){
for( j=0; j<img_gray.cols; j++ )
outfile << (int) img_gray.at<uchar>(i,j) << " " ; // note the uchar, be changed to int type.
outfile << std::endl ; // a newline after storing all the values of a line of the img
}
outfile.close() ;
相关文章推荐
- MOOC北京理工《C语言程序设计(上)》第4周第1题:计算时钟的夹角
- C语言位字段实例
- 一起talk C栗子吧(第一百四十三回:C语言实例--文件操作:基于文件指针三)
- C语言动态存储分配空间作为数组
- c/c++里的 堆区 栈区 静态区 文字常量区 程序代码区
- leetcode笔记:Reverse Vowels of a String
- 【C语言】动态链表和静态链表的创建
- 从txt文件中逐行读取字符串,并且用读取到的字符串在Mat型的图片上画矩形
- leetcode笔记:Reverse String
- C++基础实例(1)
- deepdetect 用c++11写的机器学习caffe和XGBoost API 接口
- 深度分析C++默认构造函数、拷贝构造函数
- C++中操作符重载的使用
- C++中操作符重载的使用
- 【AKOJ】1100-连续和
- C++中构造函数创建对象
- C语言 str2bin 和 bin2str 实现
- c++11新增特性
- C语言输入比较
- C语言——输入输出