Particles + ContourFinder

Hello everyone,
I’m a little newbie in the huge universe of Openframeworks. I have a generic idea for a little project: I want to create a particle system wich is attracted to the contours of my body.

I’m capable to generate the particle system and to use the openCV contourfinder, but how can I mix the two things?

Thanks guys

In the folder example/math there is a project called “particles Example”. If you look at the code, there are 2 modes that can help you to figure out what to do. One is mode 1, “attractive”, the other one is mode 3 “nearest”. You could use one of this 2 modes as skeleton for your app, and substitute the attractive points with that ones provided by openCV

thanks!Now I’ll try

I did something similar, the particle system generates on my body countour line (openCV contourfinder).

–> the pink particles is generating on the convexHull (openCV contourfinder)

It’s not exactly what you want, but hope it could help you to figure out how to combine Particle System and ContourFinder!

The rough structure of the code:

ofApp.h

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();
		void keyPressed(int key);

                void createSphereParticle();`
 
    
//--------------- contour finder
    /// \brief The video player.
    ofVideoPlayer video;
    
    /// \brief The ContourFinder object.
    ofxCv::ContourFinder contourFinder;
    
    /// \brief The contour threshold.
    ofParameter<float> finderThreshold;
    
    /// \brief True if hue / saturation should be tracked.
    ofParameter<bool> finderTrackHueSaturation;
    
    /// \brief The target color to threshold.
    ofParameter<ofColor> finderTargetColor;
    
    float hullMinumumDefectDepth = 10;

    //--------------- baseParticle
    std::vector<std::shared_ptr<baseParticle> > particles;
    int maxParticles = 100;
		
};

ofApp.cpp

void ofApp::update(){

    video.update();
    
    if (video.isFrameNew())
    {
        contourFinder.setTargetColor(finderTargetColor, finderTrackHueSaturation ? ofxCv::TRACK_COLOR_HS : ofxCv::TRACK_COLOR_RGB);
        contourFinder.findContours(video);
    }
    
    for (auto contourIndex = 0; contourIndex < contourFinder.size(); ++contourIndex)
    {
        //get convelHull
        ofPolyline contour = contourFinder.getPolylines()[contourIndex];
        ConvexHull convexHull(contour, hullMinumumDefectDepth);
        
        //draw particles on convexHull
        if (convexHull.size() > 0)
        {
            while (particles.size() <= maxParticles)
            {
                createSphereParticle();
            }
        }
    }

    for (std::size_t i = 0; i < particles.size(); i++)
    {
        // Update the particle.
        particles[i]->update();
    }
}
    
void ofApp::draw(){
    for (std::size_t i = 0; i < particles.size(); i++)
    {
        // Draw the particle.
        particles[i]->draw();
    }
}
void ofApp::createSphereParticle()
{
    
    // Instantiate our base particle.
    std::shared_ptr<sphereParticle> aParticle = std::make_shared<sphereParticle>();
    
    // Change postion on convexHull
    for (auto contourIndex = 0; contourIndex < contourFinder.size(); ++contourIndex)
    {
        ofPolyline contour = contourFinder.getPolylines()[contourIndex];
        
        ConvexHull convexHull(contour, hullMinumumDefectDepth);

        for (auto point: convexHull){
            
            aParticle->position = ofPoint(point.x, point.y, 0);
        }
    }
    
    // Add the particle to our collection.
    particles.push_back(aParticle);
}

And special thank to @bakercp who found this page and helped me with contourFinder & particle system! :clap: :clap: :clap:

thank you for your help, this is truly amazing!

Hi! I have been trying this code, and I think it should work. The only problem is that it gives an error because it says ConvexHull is not defined. Why could this be? Thanks in advance.

Hmm I am not sure where that is defined @amoz

I thought it might be coming from ofxCv addon but I think it might be defined somewhere else.

The addon does show how do use convex hull though: