Custom Search

Friday, December 26, 2008

OpenCV read coordinates from cvFindContours

CvSeq* contour1 = 0;
cvThreshold( src, src, threshold, 255, CV_THRESH_BINARY );//convert to black and white
cvFindContours( src, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
cvZero( dst );
contour1 = contour;
for( ; contour != 0; contour = contour->h_next )
{
cvDrawContours( dst, contour, CV_RGB(0,255,0), CV_RGB(255,255,0), -1, 1, 8 );
}

int maxLevel = 3;
CvTreeNodeIterator iterator;
CvPoint pt,pt1,pt2;
if(contour1 != 0)
cvInitTreeNodeIterator( &iterator, contour1, maxLevel );
int i = 0;
while( (contour1 = (CvSeq*)cvNextTreeNode( &iterator )) != 0 )
{
CvSeqReader reader;
int count = contour1->total;
cvStartReadSeq( contour1, &reader, 0 );
count -= !CV_IS_SEQ_CLOSED ( contour1 );
CV_READ_SEQ_ELEM ( pt, reader );
i++;
ptArray[i] = pt;
printf("coordinates%d: %d %d \n",i ,pt.x, pt.y);
cvRectangle( dst, pt, cvPoint(pt.x+2,pt.y+2) , CV_RGB(0,0,255), 1, 8, 0 );
}
// replace CV_FILLED with 1 to see the outlines

Taken from http://cv-kolaric.blogspot.com/ thanks SINISA KOLARIC :)

No comments: