How to leave trails of polyline?

#1

Hello all,

My apologies for such a novice question, but I am running into some trouble with OfxCV. I have taken the contour from my camera, fed it into a polyline and have smoothed out the polyline to achieve an illustrative effect.

Now I would like to do further manipulations to the appearance of the polyline, for example changing its size or creating a trailing effect, where the user’s previous image stays on the screen for a small duration and fades away.

for(int i = 0; i < n; i++) {
        //FOR FILLING
        ofPolyline polyline = contourFinder.getPolyline(i);//to convert
        ofPolyline smoothPoly = polyline.getSmoothed(70);
        
        ofPath pathFromContour;//path to be built
        
        for(int i = 0; i < smoothPoly.getVertices().size(); i++) {
            if(i == 0) {
                pathFromContour.newSubPath();
                pathFromContour.moveTo(smoothPoly.getVertices()[i]);
                
            } else {
                pathFromContour.lineTo(smoothPoly.getVertices()[i]);
            }
        }
 
        pathFromContour.close();
        pathFromContour.setFillColor(col);
        pathFromContour.draw(100,100);
        smoothPoly.draw();
    }

Above is my current code where I take the contour and convert it to a polyline. What I have done already is take the declaration ‘ofPath pathFromContouer’ and threw that in my header file to globally declare it. This actually created a trailing effect but it broke my program after a short amount of time as the old frames weren’t clearing and obvious memory leaks.

Is there any direction someone could point me in terms of taking the already created polyLine/ofPath and manipulating it further, specifically in terms of trailing?

Thank you guys so much!

#2

I guess a better way of putting this is,

I have an ofPath that is being drawn on the screen from the user’s image. This is happening in the draw() function, where ofPath pathFromContour is declared.

I would like to manipulate my pathFromContour, and I believe that should be done in the update() function [correct me if I am wrong, please]. If I were to globally declare my pathFromContour, so that I can access it across all functions, my program breaks. Any pointers?

#3

In general, yes to get an effect over time, you’d tend to keep a state and alter it when Update is called. Update and Draw will get called once per frame, so you can have Update incrementally modify the data drawn in Draw.

The data needs to persist between calls and be accessible from both Update and Draw. Usually I’d do this not with a global per se, but with some other object, say a data member of class ofApp, since Update and Draw are both members of ofApp. So define the data in class ofApp in ofApp.h.

You didn’t say in which way your program breaks, but you would also want to have a safe initial safe for your data (that doesn’t cause a problem when Update or Draw tries to use it before it is set up), perhaps an initial state set in ofApp:Setup, and then at whatever appropriate point, the data coming in from the camera.

1 Like
#4

Hi, yes thank you so much for your response, this helps me a lot.

I do have one more question for you.

  pp = contourFinder.getPolylines();
    // 1. We iterate through everything within pp (vector of polyLines)
    // 2. pp[i] represents the entire POLYLINE. IT IS WHAT YOU ARE LOOKING AT, ON THE SCREEN
    
    for(int i = 0; i < pp.size(); i++) {
        pp[i] = pp[i].getSmoothed(50); //this takes pp at position i, (there should only be 1 pp), and smooths out the ENTIRE polyline
        
        int polySize = pp[i].size() ; //this will be a large number, as it is the size of all the polylines/points? within our main (1) poly. Polylines are objects consisting of smaller polylines?
        for (int j = 0; j < polySize; j++){ //now we iterate through all the little polylines within our main pp
            ofPoint test = pp[i].getPointAtIndexInterpolated(j);
        }
     
    }

Above is my code in the update function. I have declared a global variable called pp which is the vector of polylines given from the contour. I iterate through the vector, in my draw() function, and am attempting to manipulate it in my update() function. I would like to edit the specific points within the polyline. To my understanding, a polyline is just a line consisting of little polylines right? Or is it just a bunch of ofPoints?

So when I call pp = pp.contourFinder.getPolylines() it returns a vector of the polylines which is only 1 or two, depending on number of objects in the frame. I now want to iterate through this vector and manipulate the individual points/smaller lines within the main polyline.

By doing this I iterate through the size of pp[i], which is a large number, so I know I must be iterating through all the smaller points.

My question is, how do I then manipulate the data from pp[i]? I have a variable called polySize which is the size of the vector at pp[i], but how do I access the data from that specific point? When I call size(), what type of data is being counted?

Is there something like pp[i].getPointAt[j]? I tried to do the getPointAtIndexInterpolated(j), but had no luck with that.

Thank you so much, your help is greatly appreciated.

#5

Glad to help!

To my understanding, a polyline is just a line consisting of little polylines right? Or is it just a bunch of ofPoints?

Each ofPolyline is a vector of points.

My question is, how do I then manipulate the data from pp[i]? I have a variable called polySize which is the size of the vector at pp[i], but how do I access the data from that specific point? When I call size(), what type of data is being counted?

int polySize = pp[i].size() is just the number of points in the polyline pp[i].

To access the data, i.e. the points, you’d iterate through or index into the ofPolyline. Since you have an array of ofPolylines, you might refer to pp[i][n], where n is which point in polyline i in pp. The second index is “getPointAt”. So if you wanted to move point n in polyline by adjusting its x and y, you could do pp[i][n].x++; pp[i][n].y–; …

1 Like
#6

A different approach, but for a simpler solution to leaving trails/fading out effects, you can also use FBOs - check out the fboTrailsExample in the examples folder.

1 Like
FBO and Polylines
#7

Right on. Thanks so much to the both of you, this information has helped me greatly!