Question

I want to calculate the average position of all active blobs. For that, first i need the sum of all Xand Y positions. How can i do that in this case?

    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;
Was it helpful?

Solution

Try this:

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top