Building new alpha masks for each distinct shape (ofxCv, FBOs)

What I am trying to do is take camera input, find contours, use as alpha masks, and have different videos under each distinct shape. I have the alpha mask working, I can get a single video underneath all contours found. Would anybody have any tips in making each alpha mask unique?

http://pastebin.com/FFuRnreU

But in short, what I am trying to do is get a different video under each distinct shape.

Any tip would be hugely appreciated.

Thank you all!

Hi, you can draw each contour with a different gray level. Then in the shader only mask the pixels that correspond to the pixel vlue you want to mask. You might need to set a uniform in the shader to define the mask pixel value.
For eficiency you should pass to the shader each video texture and its corresponding mask value all at the same time, hence you´ll need a texture uniform for each video.
hope it helps.

Ahh, that might work! Thank you! I am trying to get each contour drawn at a different gray level, by putting pathFromContour.setColor(); in the contour loop. The problem now is that the color value doesn’t stick to the path, the number of contours in the screen has to be changing for it to continue changing the color too. Would you have any tips on how to get each contour to have a different gray level and have it stick?

void testApp::update(){

//UPDATE VIDEOS
    cam.update();
    video1.update();
    //video2.update();
    //video3.update();
    //video4.update();

    maskFbo.begin();
        ofClear(0,0,0,255);
    maskFbo.end();

//CONTOUR FINDER
    if(cam.isFrameNew()) {//if new frame
        contourFinder.setThreshold(80);
        contourFinder.findContours(cam);//find contour of current frame
    }

//NUMBER OF SHAPES
    int n = contourFinder.size();

//BUILD POLYLINES - PATHS
    for(int i = 0; i < n; i++) {//FOR EACH CONTOUR...
        
        //NOTE: MUST BE LINKED WITH AN IDENTIFIER.
            //ofPolyline polyline = contourFinder.getPolyline(i).getResampledBySpacing(5);
            ofPolyline polyline = contourFinder.getPolyline(i);
        
        //CREATE MAP
            //map<int, ofPolyline> polyMap;
            //polyMap[i] = polyline;
        
        ofPath pathFromContour;//path to be built REALLY AFFECTS SPEED, KEEP HERE
        
        //CREATE MAP
            //map<int, ofPath> pathMap;
            //pathMap[i] = pathFromContour;
        
        //CREATE PATHS FROM POLYLINES
        for(int j = 0; j < polyline.getVertices().size(); j++) {
            if(j == 0) {
                pathFromContour.newSubPath();
                pathFromContour.moveTo(polyline.getVertices()[j]);
            } else {
                pathFromContour.lineTo(polyline.getVertices()[j]);
            }
        }
        
        pathFromContour.close();
        pathFromContour.simplify();
        
        // MASK (frame buffer object)
        maskFbo.begin();
            pathColor = ofColor(i++);//GRAY TEST
            pathFromContour.setColor(pathColor);
            pathFromContour.draw(0,0);//PATH
        maskFbo.end();
        
    }

}

hi,
sure, check the examples in ofxCv. Theres one that´s exactly what you need. It´s called contourtracking.
best

So I am looking at the contour tracking example, and I can see how information about location could be used, but I still can’t figure out a way to bind a different color to each contour…

Then in the shader only mask the pixels that correspond to the pixel vlue you want to mask. You might need to set a uniform in the shader to define the mask pixel value.
For eficiency you should pass to the shader each video texture and its corresponding mask value all at the same time, hence you´ll need a texture uniform for each video.

I have figured out how to get different gray values on each shape of the mask! So I am moving forward, but now I am at this step. Does anyone know of an example that I could look at where the shader only masks certain pixels? uniform mask1, mask2, etc below are kind of my attempt at setting a uniform for this purpose (with 50, 100, 150, and 200 and the grayscale values later assigned to them). Is this along the right lines?

string vertex = "#version 150\n\
        \n\
        uniform mat4 projectionMatrix;\n\
        uniform mat4 modelViewMatrix;\n\
        uniform mat4 modelViewProjectionMatrix;\n\
        \n\
        \n\
        in vec4  position;\n\
        in vec2  texcoord;\n\
        \n\
        out vec2 texCoordVarying;\n\
        \n\
        void main()\n\
        {\n\
        texCoordVarying = texcoord;\
        gl_Position = modelViewProjectionMatrix * position;\n\
        }";
string fragment = "#version 150\n\
        \n\
        uniform sampler2DRect tex0;\
        uniform sampler2DRect maskTex;\
        \
        uniform int mask1;\
        uniform int mask2;\
        uniform int mask3;\
        uniform int mask4;\
        \
        in vec2 texCoordVarying;\n\
        \
        out vec4 fragColor;\n\
        void main (void){\
        vec2 pos = texCoordVarying;\
        \
        vec3 src = texture(tex0, pos).rgb;\
        float mask = texture(maskTex, pos).r;\
        \
        fragColor = vec4( src , mask);\
        }";