将数组数据写入opencv中的文本文件

时间:2014-07-14 17:40:44

标签: c++ opencv

我想将其中一个频道的数组数据写入文本文件。我该怎么办?

我试过这个

Mat img = imread("dog.jpg", 1);

std::ofstream fout("myfile.txt");

fout << img.at<Vec3b>(i, j); 

但它正在复制所有3个频道的数据。我想存储一个频道的数据,让我们说B [] []。假设我已经将所有通道的数据分别存储在R [] [],G [] [],B [] []中,如图所示。

Vec3b intensity = img.at<Vec3b>(i, j);

B[j][i]=intensity[0];
G[j][i] = intensity[1];
R[j][i] = intensity[2];

fout << ??????????????????????;  //want to store data of Blue channel

1 个答案:

答案 0 :(得分:2)

试试这个:

Mat img = imread("d:\\ImagesForTest\\cat.jpg", 1);
std::ofstream fout("myfile.txt");
vector<Mat> ch;
cv::split(img,ch);

  //  Possible formatters
  // --------------------------
  //  (matlab)      Formatter::FMT_MATLAB
  //  (python)      Formatter::FMT_PYTHON
  //  (numpy)       Formatter::FMT_NUMPY
  //  (csv)         Formatter::FMT_CSV
  //  (c)           Formatter::FMT_C 

fout << format(ch[0], Formatter::FMT_CSV); // Blue
fout << format(ch[1], Formatter::FMT_CSV); // Green
fout << format(ch[2], Formatter::FMT_CSV); // Red