Get subSection or ROI from a bigger section

Hi
I am trying to get a portion, a subsection from a texture (in specific from video source/webcam).
I see there is drawSubsection()method which draws the portion I want, but how do I translate the same portion as texture to bind()on to a ofMesh?

Ultimately what I want to achieve is having portions of videos delimited by a mesh area. That would work exactly like using a mesh as a mask. (which is my next test)

Anyway, so far, this is what I have tried:

in .h

ofxCvColorImage testImage;

in cpp

setup(){
//leRect is a getboundingBox() of the area I need - it is a dynamic value
    testImage.allocate(leRect.width, leRect.height); 
}

update(){
 testImage = cam.getPixels();
 testImage.setROI(feature.getBoundingBox());
}

draw(){

        testImage.getTextureReference().bind();
        mesh.draw();
        testImage.getTextureReference().unbind();
}

I get these errors like the allocation doesn’t work…

[ error ] ofxCvImage: setROI(): image not allocated
[ error ] ofxCvColorImage: setFromPixels(): width and height are zero

is there anything/anyone that can help please?

Cheers

the most flexible way to do this is to use texture coordinates in the mesh, for example:

//setup
mesh.setMode(OF_PRIMITIVE_TRIANGLE_FAN);
mesh.addVertex(ofVec3f(x,y));
mesh.addVertex(ofVec3f(x+w,y));
mesh.addVertex(ofVec3f(x+w,y+h));
mesh.addVertex(ofVec3f(x,y+h));

mesh.addTexCoord(ofVec2f(tx,ty));
mesh.addTexCoord(ofVec2f(tx+tw,ty));
mesh.addTexCoord(ofVec2f(tx+tw,ty+th));
mesh.addTexCoord(ofVec2f(tx,ty+th));

//draw
cam.getTextureRef().bind();
mesh.draw();
cam.getTextureRef().unbind();

where x,y,w,h are the position and size of the final rectangle and tx,ty,tw,th are the texture subsection x,y and width/height.

with that method you can draw whatever combination of position/size in the screen and texture.

1 Like

Hi @arturo!
thanks for replying.
So I started from your suggestion. I tried few ways but I didn’t get that right.
I was trying to adapt a rectangle o a mesh that is not rectangular.

I eventually managed to get for each mesh point the relative coordinate

for (int i=0; i<feature.getVertices().size(); i++) {
    mesh.addTexCoord(ofVec2f(feature.getVertices()[i].x, feature.getVertices()[i].y));
}

this did the trick!

I also tried the solution with the masks that worked very well. I used ofxLayerMask very easy and intuitive.
Now I need to understand which one works better and a way to smooth the mesh.

Thanks a lot!