2012-11-07 106 views
2

我想執行一個非常簡單的任務,但不能由於我無法弄清的錯誤。我想用下面這段代碼打印矢量的內容

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

保存的檢測到的向量到一個txt文件特徵的內容,但我看到下面的錯誤:

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是由浮點數據類型的矩陣

+2

'keypoints.at (ⅰ)'應該簡單地成爲'keypoints.at(i)'。 – jrok

+0

謝謝,但我試過你的版本,仍然得到相同的錯誤。此外,我使用.at運算符閱讀該文件,建議指定正在使用的數據類型,這就是爲什麼我這樣寫的。還基於我剛添加的編輯。 – ipunished

+1

你是否重載了'operator <<(std :: ostream&,const KeyPoint&)'?沒有這些,你將無法使用'outf << ...'語法。 – jogojapan

回答

0

使用內置FileStorage類,這是..我印刷使用下列行的關鍵點的矢量一樣容易添加兩行:

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

    fs << "meh" << keypoints; 
1

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

+0

有什麼區別? – shengy

+1

@shengy:OP說'keypoints.at (i)'。那不一樣。並且完全不正確。 –

+0

謝謝,但我試過沒有,但仍然得到相同的錯誤。此外,我使用.at運算符閱讀該文件,建議指定正在使用的數據類型,這就是爲什麼我這樣寫的。還基於我剛添加的編輯。 – ipunished

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(); 

as @jogojapan said,overload operator < <(..)for 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; 
} 
+0

感謝這是一個好點,我應該把它放在循環之外。但是這並不能解決我得到的錯誤,它仍然存在。我使用的數據類型是OpenCV特有的KeyPoint,是否會導致錯誤?因爲當我按照你的建議使用它時,我得到以下錯誤:'''<<':找不到操作符找到類型爲'std :: basic_ostream <_Elem,_Traits>'的左側操作數(或者沒有可接受的轉換)'' – ipunished

+1

你定義了'std :: ostream&operator <<(std :: ostream&out,const KeyPoint&);'? – billz

+0

謝謝..查看運算符重載..不同的數據類型以及內置的Point2f會使這個變得困難,還有一個內置的函數來編寫這個函數,我也在研究這個函數。 – ipunished