Scale and translate a blob

Hello. I am trying to scale individual blobs from ofxCv but I’m having a hard time moving them individually. I am detecting contours on a very simple image, a png with 7 circles. The goal is to create first an ofPolyline out of each blob, and then a ofPath out of each ofPolyline. I created a vector of ofPolylines and then I loop through all the blobs and add the vertices of each instance. So far so good. I then proceed to scale the polyline and to move it to the x and y position of the bounding box of the blob, but for some reason that I cannot understand the polylines don’t get drawn where they should. Here is my function:

void Layer::buildContour(){

    for (int i = 0; i < contourFinder.nBlobs; i ++){
        if (contourFinder.blobs[i].hole){
            ofPolyline blobPoly;
            ofPath  blobPath;
            ofPoint center;
            ofRectangle boundingBox;
            blobPoly.addVertices(contourFinder.blobs[i].pts);
            center.set(contourFinder.blobs[i].centroid);
            boundingBox = contourFinder.blobs[i].boundingRect;

            blobPoly.scale(0.5,0.5);
            blobPoly.translate(ofPoint(boundingBox.x, boundingBox.y));

            for (int i = 0; i < blobPoly.size(); i++){
                blobPath.lineTo(blobPoly[i]);

            }
            blobPath.setStrokeWidth(5);
            blobPath.close();
            blobPath.setStrokeColor(ofColor::red);
            blobPath.setFilled(false);
            blobs.push_back(blobPoly);
            blobPaths.push_back(blobPath);
        }
    }

Here is how it looks.

Any ideas what I’m doing wrong? Thanks!

Hi Antonio,

I guess you want something like that, am I right ?

goal

ofPolyline::scale() simply multiply the coordinates of the polyline vertices. Look at this gif to see what happens when we multiply the vertices coordinates by a variable amount:

bad

The result is that the polyline is scaled “around” the upper left corner (x=0, y=0)

But you want to do this:

good

A solution is a 3 steps process:
1- translate the polyline to make it centered around the point (x=0, y=0)
2- scale the polyline
3- translate it back to its original position

solution

You can try:

blobPoly.translate( - center ); // step 1
blobPoly.scale( 0.5, 0.5 ); // step 2
blobPoly.translate( center ); // step 3

Thank you very much for your explanation lilive, it worked and it’s clear now. My problem was that I was thinking that translate was going to move the Polyline in absolute terms, not in relation to the current position, so whenever I tried to move to the point (0, 0) nothing happened and it was confusing.