轮廓的查找和过滤

int GetContourPoint1(IplImage const* const src,vector<CvPoint>& ContourPoint)
{
	CvSeq* temp_contours = NULL;
	CvSeq* MaxContours = NULL;
	CvMemStorage* storage = cvCreateMemStorage(0);//开辟默认大小的空间
	IplImage *temp=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
	cvCopy(src,temp);
	cvFindContours(temp, storage, &temp_contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0, 0));

	//CV_RETR_LIST获取轮廓的内外层的所有点


	int minsize = 7;//限制最小轮廓的大小
	int cnt = 0;

	IplImage* temp2 = cvCreateImage(cvGetSize(temp),8,0);

	int pointx,pointy;
	BOOL result;

	int srcw=src->width;

	for(;temp_contours!=NULL;temp_contours=temp_contours->h_next)
	{ 
		CvSeq* temp_contour = NULL;
		temp_contour = temp_contours;
		unsigned int tempsize = temp_contour->total;
		result=GetContourCenter1(temp,temp_contour);//GetContourCenter1过滤掉一些边缘的轮廓
		CvRect rect=cvBoundingRect(temp_contour);
		if (tempsize > minsize&&result&&rect.width>srcw*0.59&&rect.width<srcw*0.95)
		{
			for(int pi=0;pi<tempsize;pi++)
			{  
				CvPoint* p=(CvPoint*)cvGetSeqElem(temp_contour,pi); 
				//CvPoint* exp=(CvPoint*)cvGetSeqElem(MaxContours,pi-1); 
				pointx = p->x;
				pointy = p->y;
				ContourPoint.push_back(cvPoint(pointx,pointy));
			}
			cvDrawContours(temp2, temp_contour, CV_RGB(255, 255, 255), CV_RGB(255, 255, 255), 0, 2, CV_FILLED, cvPoint(0, 0)); 
		}
	}
	// IplImage *test=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
	// cvZero(test);
	// for (int p=0;p<ContourPoint.size();p++)
	// {
	// cvCircle(test,ContourPoint[p],1,CV_RGB(255,255,255),1,8,0);
	// }
	// cvNamedWindow("test");
	// cvShowImage("test",test);
	// cvWaitKey(0);
	//   cvNamedWindow("test");
	//   cvShowImage("test",temp2);
	// cvWaitKey(0);
	cvReleaseImage(&temp);
	cvReleaseImage(&temp2);
	cvReleaseMemStorage(&storage);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/kai69/article/details/78860303