// m_imgFeature为黑白目标图像,白色为前景,黑色为背景
// 注意此函数会修改m_imgFeature内容。若其不可更改,应另建立副本
// 1. count contour
CvMemStorage* storage = 0;
storage = cvCreateMemStorage(0); //开辟默认大小的空间
CvSeq* contour = 0;
cvFindContours( m_imgFeature, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); // 查找外边缘
int num = 0;
for( ; contour != 0; contour = contour->h_next )
{
CvRect rect;
rect = cvBoundingRect(contour,0); // 转化为点列
m_arrLine.SetSize(num+1);
m_arrLine.GetAt(num).SetLine(rect.x,rect.x+rect.width ,rect.y,rect.y+rect.height);
num = num+1;
for( i = 0; i < contour->total; i++ )
{
CvPoint* line = (CvPoint*)cvGetSeqElem(contour,i);
cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, 8 );
}
}
// .release
cvReleaseMemStorage(&storage);
//////////////////end of code////////////////////////////
cvFindContours的第5个参数
CV_RETR_EXTERNAL 查找外边缘,各边缘以指针h_next相连
CV_RETR_LIST 查找所有边缘(包含内部空洞),各边缘以指针h_next相连
CV_RETR_CCOMP 查找所有边缘(包含内部空洞),按照如下方式组织