gpt4 book ai didi

c++ - 打印 vector 的内容

转载 作者:搜寻专家 更新时间:2023-10-31 01:52:02 26 4
gpt4 key购买 nike

我想执行一个非常简单的任务,但由于我无法弄清楚的错误而无法执行。我想使用以下代码将检测到的特征的内容保存到 vector 到 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': function call missing argument list; use '&std::vector<_Ty>::at' to create a pointer to member

我检查了我的语法,没有发现任何错误。

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

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是由float数据类型组成的矩阵

最佳答案

尝试将代码更改为以下:

 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;
}

关于c++ - 打印 vector 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13261839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com