how to :: cut out texture from countour vertex list

hi aLL :wink:

e*

I’m not sure if there is a better way to do it with shaders, but maybe this could work for you:

opencv has a polygon fill function (cvFillPoly) that takes vertices and converts it to an image. So you could get an image mask from the vertices and then use that to copy just those pixels.

I have some code for this here:
cvContourToImage.cpp
cvContourToImage.h

I think this should work for copying with a mask:

  
  
void copyWithMask(ofCvGrayscaleImage & src, ofCvGrayscaleImage & dst, ofCvGrayscaleImage & mask)  
{  
	  
	cvCopy( src.getCvImage(), dst.getCvImage(),  mask.getCvImage() );  
  
}  

hi chris & all :wink:

well , thanks a lot for your indications. the cv mask solution seems quite a good approach, but i face then some other problems to solve it all out :

the problem now is how to get the “src” cvImage for the “copyWithMask” function. The image input should be what has been drawn in the framebuffer (using a shader on this which makes luma-key). So how to get this “src” from the framebuffer?

* I tried with ofTexture::loadScreenData() and i get the right texture but then: ?¿ how to convert or pass the data from an ofTexture to a cvImage for being able to apply the cvCopy(…with mask) ??

* I als tried with glReadPixels() that should give me back a * to screen pixel data but i don’t know why, this function always returns like a black screen . Anyidea of how to use glReadPixels() ?

i tried with :

glReadPixels(originX,originY,640,480,GL_UNSIGNED_BYTE,GL_RGB,pixels);
but the pixels always show a black screen ??

any suggestions ?

hi eloi,

loadScreenData wont help you, because the data stays on the graphics card (and is thus fast to grab and reuse)

glReadPixels will help you, though. I think your problem might very well be that the 0,0 for opengl (lower left) is different then the 0,0 for OF (upper left). You can take a peak in ofImage and see the grabPixels call, there should be enough info in there about usage to help you get started, for example, you’ll see this:

  
  
int height = ofGetHeight();  
_y = height - _y;  
_y -= _h;  

what we do to get the x,y,w,h of glReadPixels right, etc

good luck!
zach

Hello Zach and Chris,

I used the code from chris named cvContourToImage.cpp

But I have some problems dealing with the blobs[i].pts that are in vector and Chris´s source have them on ofPoint2f *
So I change it for:

  
IplImage * cvContourToImage::convertContourToImage( vector<ofPoint> pts, int totalPts){  
    // max contour points should be defined in shared keying header  
	int numRegions		= 1;  
	int numInner		= 1;  
	int currentRegion	= 0;  
	int currentContour	= 0;  
	  
	// get storage  
	storage	= cvCreateMemStorage();  
	  
	CvPoint * cPts = new CvPoint[totalPts+1];  
	// copy points from array  
	for(int i = 0; i < totalPts; i++)  
	{  
		cPts[i].x = pts[i].x;  
		cPts[i].y = pts[i].y;  
	}  
	cPts[totalPts].x = pts[0].x;  
	cPts[totalPts].y = pts[0].y;  
	  
    cvSetZero( image );  
	  
    CvPoint * cPtss = cPts;   
    //cvFillConvexPoly( image, (CvPoint*)cPtss, totalPts, cvScalar(255,255,255,255));  
	cvFillPoly( image, &cPtss, &totalPts, 1,cvScalar(255,255,255,255) );  
	  
	delete cPts;  
	cvReleaseMemStorage(&storage);  
	  
	return image;//(unsigned char*) image->imageData;  
}  

It compiles! But whene I do:

  
  
ofxCvGrayscaleImage maskedImg;  
  
maskedImg.allocate(vid_width, vid_height);  
  
cvCopy(grayImage.getCvImage() , maskedImg.getCvImage(), contourToImage.convertContourToImage(blobTracker.blobs[i].pts, blobTracker.blobs[i].nPts));  
  
maskedImg.draw(i*vid_width,0*2);  
  

The maskedImg is all black no signs of a white poligon.
Can you help me knowing why?

Thanks

Patricio

hi,

_ how can i cut out a texture from a list of contour pixels and make a new texture with just that inner contour pixels ? _

I am not sure of what you want, but i would use the stencil buffer.
you render the contour as a fill poly in the srtencil buffer, then you render the texture using stencil buffer information.

++