打印矢量的内容

时间:2012-11-07 01:04:33

标签: c++ opencv feature-detection

我想执行一项非常简单的任务,但由于错误我无法弄清楚。我想使用以下代码

将检测到的功能的内容保存到txt文件中
Ptr<FeatureDetector> feature_detector = FeatureDetector::create("SIFT");
vector<KeyPoint> keypoints;

feature_detector->detect(img, keypoints);

for(unsigned int i = 0; i < keypoints.size(); i++)
{
    ofstream outf("vector.txt", ios::app);
    outf<<"value at "<< i << " = " << keypoints.at<KeyPoint>(i)<<endl;
}

但我收到以下错误:

  

std :: vector&lt; _Ty&gt; :: at':函数调用缺少参数列表;使用   '&amp; std :: vector&lt; _Ty&gt; :: at'创建指向成员的指针

我检查了我的语法,找不到任何错误。

编辑:在此之前,我想要打印出矩阵的内容,这种格式非常适合它,这是我用来打印矩阵内容的代码:

for(int x = 0;x < dst.rows ; x++)
{
    for( int y = 0; y < dst.cols; y++)
    {
        ofstream outf("Sample.txt", ios::app);
        outf<<"value at "<< x << "  " << y << " = " << dst.at<float>(x,y)<<endl;
    }
}

其中dst是由浮点数据类型

组成的矩阵

3 个答案:

答案 0 :(得分:2)

尝试将代码更改为:

 ofstream outf("vector.txt", ios::app);  // you don't want to open file again and again
 for(unsigned int i = 0; i < keypoints.size(); i++)
 {
     outf<<"value at "<< i << " = " << keypoints.at(i)<<endl;
 }
 outf.close(); 

正如@jogojapan所提到的,为OpenCV KeyPoint重载运算符&lt;&lt;(..)。

std::ostream& operator<<(std::ostream& out,const KeyPoint& keypoint)
{
  // add stream keypoint member by yourself here
  out << keypoint.size;
  out << keypoint.angle;
  out << keypoint.response;
  out << keypoint.octave;
  out << keypoint.class_id;
  return out;
}

答案 1 :(得分:1)

说:outf << "value at "<< i << " = " << keypoints[i] <<endl;

答案 2 :(得分:0)

使用内置的FileStorage类就像添加两行一样简单。我使用以下行打印了KeyPoints的向量:

FileStorage fs("test.xml", FileStorage::WRITE);

    fs << "meh" << keypoints;