C++程序调试过程输出数据的三种基本方法std::cout filestream image

最简单方法就是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,&current_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,仅供参考

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值