最简单方法就是std::cout将数据显示到终端
#include <iostream>
std::cout << "pos_x="<< pos_x << " pos_x1=" << pos_x1<<" pos_x2=" << pos_x2 << " pos_y="<< pos_y<< " pos_y1="<< pos_y1<< " pos_y2="<< pos_y2 <<endl;
std::cout.flush();
第二种方法:filestream,将数据通过ASCII输出到txt文件
#include <fstream>std::ofstream outf;
outf.open("/home/xxx/test.txt");
outf<<ros::Time::now()<<" i="<<i<<"\n";
for(x = 0; x < TEMPLATE_X_SIZE; x++){for(y=0; y < TEMPLATE_Y_SIZE; y++){sum_all += abs(view_temp[x+y*TEMPLATE_X_SIZE] - vt->data[x+y*TEMPLATE_X_SIZE]);outf<<view_temp[x+y*TEMPLATE_X_SIZE]<<" ";}outf<<"\n";if(sum_all>best_comp) break; }outf<<flush;
outf.close();
第三种方法:用于将矩阵或者数组数据输出到图像中保存
#include <opencv2/opencv.hpp>
void main()
{
//current_view存放的是double类型的一维向量vector数据
string strpath1="/home/xxx/"+std::to_string(ros::Time::now().toSec())+"curr_view.jpg";
savaImage(strpath1,¤t_view[0]);
}
void LocalViewMatch::savaImage(std::string filepath, const double * ptr)
{cv::Mat img;//TEMPLATE_Y_SIZE数组高度,TEMPLATE_X_SIZE宽度cv::resize(convertImage(TEMPLATE_Y_SIZE,TEMPLATE_X_SIZE,true,ptr),img,cv::Size(IMAGE_WIDTH,IMAGE_HEIGHT));cv::imwrite(filepath, img);
}
cv::Mat LocalViewMatch::convertImage(size_t rows, size_t cols, bool isGray, const double* data){cv::Mat res;if(isGray){res=cv::Mat(rows,cols,CV_8UC3);//这里之所以还是作为彩色图像处理,主要是为了混合显示的方便for(int row=0;row<rows;row++) //行的个数和 IMAGE_HEIGHT 相等for(int col=0;col<(cols);col++) //列数和 IMAGE_WIDTH 相等{//res.at<unsigned char>(row,col)=(unsigned char)((data)[row*cols+col]*255.0);res.at<unsigned char>(row,3*col )=(unsigned char)((data)[row*cols+col]*255.0);res.at<unsigned char>(row,3*col+1)=(unsigned char)((data)[row*cols+col]*255.0);res.at<unsigned char>(row,3*col+2)=(unsigned char)((data)[row*cols+col]*255.0);}}else{res=cv::Mat(rows,cols,CV_8UC3);for(int row=0;row<rows;row++) //行的个数和 IMAGE_HEIGHT 相等for(int col=0;col<cols;col++) //列数和 IMAGE_WIDTH 相等{res.at<unsigned char>(row,3*col )=(unsigned char)((data)[3*(row*(cols)+col) ]*255.0);res.at<unsigned char>(row,3*col+1)=(unsigned char)((data)[3*(row*(cols)+col)+1]*255.0);res.at<unsigned char>(row,3*col+2)=(unsigned char)((data)[3*(row*(cols)+col)+2]*255.0); }}return res;}
其中convertImage函数来源于ratslam,仅供参考