在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.

本文介绍了在C++中如何将矩阵数据存储到.txt文件的两种方式。第一种方法使用标准库中的`FILE`,通过`fopen`, `fprintf` 和 `fclose` 函数实现写操作。第二种方法利用`ofstream`对象,适用于处理图像数据,如`img_gray`的灰度值。每种方法都详细说明了文件打开模式及其作用。"
121176852,964760,Java集合框架大管家Collections详解,"['Java', '后端开发', '集合框架']
最低0.47元/天 解锁文章
7220

被折叠的 条评论
为什么被折叠?



