2014-03-27 82 views
0

我想計算所有活動的blobs的平均位置。爲此,首先我需要所有XY職位的總和。我如何在這種情況下做到這一點?C++ BLOB平均值

contourFinder.findContours(grayImg, minBlobSize, maxBlobSize, blobNum, false); 
    blobTracker.trackBlobs(contourFinder.blobs); 

    std::vector<ofxCvBlob>::iterator blob; 
    for (auto blob = contourFinder.blobs.begin(); blob!= contourFinder.blobs.end(); blob++) { 
     float xpos = (*blob).center.x; 
     float ypos = (*blob).center.y; 

     int blobSize = contourFinder.blobs.size(); 

     int sumX = ? 
     int sumY = ? 

     float averageX = sumX/blobSize; 
     float averageY = sumY/blobSize; 

UPDATE:

 contourFinder.findContours(grayImg, minBlobSize, maxBlobSize, blobNum, false); 
     blobTracker.trackBlobs(contourFinder.blobs); 

     std::vector<ofxCvBlob>::iterator blob; 

     int blobSize = contourFinder.blobs.size(); 
     int sumX = 0; 
     int sumY = 0; 
     int sumArea = 0; 

     if(blobSize > 0){ 

     for (auto blob = contourFinder.blobs.begin(); blob!= contourFinder.blobs.end(); blob++) { 
      float xpos = (*blob).center.x; 
      float ypos = (*blob).center.y; 
      float areaBlob = (*blob).area; 

      sumX += xpos * areaBlob; 
      sumY += ypos * areaBlob; 
      sumArea += areaBlob; 


     } 

      float averageX = sumX/sumArea; 
      float averageY = sumY/sumArea; 

回答

1

試試這個:

int blobSize = contourFinder.blobs.size(); 
int sumX = 0; 
int sumY = 0; 

std::vector<ofxCvBlob>::iterator blob; 
for (auto blob = contourFinder.blobs.begin(); blob!= contourFinder.blobs.end(); blob++) { 
    float xpos = (*blob).center.x; 
    float ypos = (*blob).center.y; 

    sumX += xpos; 
    sumY += ypos; 

    // Manage each point here ... 
} 

float averageX = sumX/blobSize; 
float averageY = sumY/blobSize; 
+0

謝謝!你真棒! :d –