簡體   English   中英

訪問向量中的數組值<Point2f>並分開 x 和 y 坐標

[英]Access array values in a vector<Point2f> and separate x- and y coordinates

我正在編寫使用迭代 Lucas-Kanade 方法計算光流的代碼: calcOpticalFlowPyrLK() 我有一個可以容納兩個元素的數組向量,請參見下面的示例:

vector <Point2f> points[2];

x 和 y 坐標存儲在數組中,數組存儲在向量中。 輸出數組時,例如cout << points[0] ,當前在屏幕上顯示的坐標如下:

Output example: [261.837, 65.093]

現在我想提取 x 和 y 坐標,將它們分開並將它們存儲在不同的變量中。 已經用迭代器嘗試了幾種方法,但沒有結果。 如果有人可以幫助我,我將不勝感激,謝謝。

以下示例將 PLK 應用於常規網格並顯示如何讀取 x 和 y 坐標。 這些點存儲在 Point2f 類中,使用向量類將它們存儲在數組中。 該類具有可以直接使用的公共 x 和 y 成員。 此示例不使用迭代器。

std::vector<cv::Point2f> prevPoints, currPoints;
std::vector<float> error; // stores the SSD error. 
std::vector<uchar> status;// stores a flag of successful tracking / I recomend to ignore it.
cv::Mat prevGrayImg,currGrayImg;
// <- insert code for read the images
// initalize grid or the features you want to track
for( int r = 0; r < prevGrayImg.rows;r+=5){
for( int c = 0; c < prevGrayImg.cols;c+=5){
  prevPoints.push_back(cv::Point2f(c,r));
}}
// apply pyramidal lucas kanade
cv::calcOpticalFlowPyrLK(prevGrayImg, currGrayImg, prevPoints, currPoints, status, error);
for( unsigned int i = 0; i < prevPoints.size(); i++){
  float x0 = prevPoints[i].x;
  float y0 = prevPoints[i].y;
  float x1 = currPoints[i].x;
  float y1 = currPoints[i].y;
}

使用迭代器,它將是:

for( auto i = prevPoints.begin(); i != prevPoints.end(); ++i){
  float x0 = i->x; ... a.s.o

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM