2

我想执行一个非常简单的任务,但由于我无法弄清楚的错误而无法执行。我想使用以下代码将检测到的特征的内容保存到向量中到 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<_Ty>::at': 函数调用缺少参数列表;使用 '&std::vector<_Ty>::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 是一个由浮点数据类型组成的矩阵

4

3 回答 3

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 的重载运算符<<(..)。

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;
}
于 2012-11-07T01:59:16.447 回答
1

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

于 2012-11-07T01:07:04.023 回答
0

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

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

    fs << "meh" << keypoints;
于 2012-11-11T16:22:08.970 回答