문제

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;
도움이 되었습니까?

해결책

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top