Filling a contour and not using cvFillPoly

I implemented the code from Darel Rex Finley about Efficient Polygon Fill Algorithm.

however the results were not that good.

here is the code for OF

  
  
fillPolygon(ofxCvContourFinder & contour, int x, int y)  
{  
int  nodes, columX, rowY, i, j,t, swap, rowMax ;  
  
  
    glPushMatrix();  
    glTranslatef( x, y, 0.0 );  
  
    for (int i=0; i<contour.nBlobs; i++){  
    int nodeX[contour.blobs[i].nPts];  
       //  Loop through the rows of the image.  
        rowMax=contour.blobs[i].boundingRect.y+contour.blobs[i].boundingRect.height;  
  
        for (rowY=contour.blobs[i].boundingRect.y; rowY<rowMax; rowY++) {  
             //  Build a list of nodes.  
            nodes=0; t=contour.blobs[i].nPts-1;  
            for (j=0; j<contour.blobs[i].nPts; j++) {  
                if (contour.blobs[i].pts[j].y<(double) rowY && contour.blobs[i].pts[t].y>=(double) rowY  
                ||  contour.blobs[i].pts[t].y<(double) rowY && contour.blobs[i].pts[j].y>=(double) rowY) {  
  
                  nodeX[nodes++]=(int) (contour.blobs[i].pts[j].x+(rowY-contour.blobs[i].pts[j].y)  
                  /(contour.blobs[i].pts[t].y-contour.blobs[i].pts[j].y)*(contour.blobs[i].pts[t].x-contour.blobs[i].pts[j].x));  
  
                }  
                t=j;  
            }  
  
              //  Sort the nodes, via a simple “Bubble” sort.  
              i=0;  
            while (i<nodes-1){  
                if (nodeX[i]>nodeX[i+1]){  
                  swap=nodeX[i]; nodeX[i]=nodeX[i+1]; nodeX[i+1]=swap; if (i) i--;  
                }  
                else{  
                  i++;  
                }  
            }  
  
              //  Fill the pixels between node pairs.  
            for (i=0; i<nodes; i+=2) {  
                if   (nodeX[i  ]>=contour.blobs[i].boundingRect.x+contour.blobs[i].boundingRect.width) break;  
                if   (nodeX[i+1]> contour.blobs[i].boundingRect.x ) {  
                  if (nodeX[i  ]< contour.blobs[i].boundingRect.x ) \  
                    nodeX[i  ]=contour.blobs[i].boundingRect.x ;  
                  if (nodeX[i+1]> contour.blobs[i].boundingRect.x+contour.blobs[i].boundingRect.width)  
                    nodeX[i+1]=contour.blobs[i].boundingRect.x+contour.blobs[i].boundingRect.width;  
  
                  ofSetColor(0x000000);  
                  ofBeginShape();  
                  for (j=nodeX[i]; j<(columX=nodeX[i+1]); j++)  
                    ofVertex( j,rowY );  
                  ofEndShape();  
                  //ofLine((float)nodeX[i],(float)rowY,(float)nodeX[i+1],(float)rowY);  
                }  
            }  
  
        }  
    }  
  
    glPopMatrix();  
}  
  
  


Does anyone has a better algorithm for filling a polygon without using and Image?

i want to be able to draw JUST a hand in my app without any background form the video that might interfere with other drawings

Is this what you want

  
void draw(ofxCvBlob * blob, float x = 0, float y = 0){  
            ofFill();  
            ofSetColor(0xFF00FF);  
            ofBeginShape();  
            for (int i = 0; i < blob->nPts; i++){  
                   ofVertex(x + blob->pts[i].x, y + blob->pts[i].y);  
            }  
            ofEndShape(true);	  
        }  

ding

hello, thanks for your quick respond

If i am not mistaken the ofxCVContourfinder just gives the points of the contour. therefore the code will only draw the contour.

  
  
// get the points for the blob:  
		CvPoint           pt;  
		CvSeqReader       reader;  
		cvStartReadSeq( cvSeqBlobs[i], &reader, 0 );  
  
    	for( int j=0; j < cvSeqBlobs[i]->total; j++ ) {  
			CV_READ_SEQ_ELEM( pt, reader );  
            blobs[i].pts.push_back( ofPoint((float)pt.x, (float)pt.y) );  
		}  
		blobs[i].nPts = blobs[i].pts.size();  
  

it there a way of getting the whole blob, in other words, all the point/pixels that make up the blob?

if not, then how can I fill up a polygon such as the blobs ofxCvContourFinder gives?

sorry but I made a mistake. ofnofill() should be ofFill() that should give you a fill I just corrected it.

ding