#include <cxcore.h>
#include <cv.h>
#include <highgui.h>
#include <iostream.h>
int main( int argc, char* argv[] )
{
// int i;
int mode = CV_RETR_EXTERNAL;//提取轮廓的模式
int contours_num = 0;//图像中提取轮廓的数目
CvMemStorage* storage = cvCreateMemStorage(0);//开辟64K的存储空间
CvSeq* contour = 0;//存储提取轮廓的序列指针
IplImage* bin_img = cvLoadImage("C:\\Users\\xht\\Desktop\\open\\ba.jpg",0);//读入要检测的图像
cvThreshold(bin_img,bin_img,128,255,CV_THRESH_BINARY);//二值化
IplImage* pContourImg = cvCreateImage(cvGetSize(bin_img),8,3);
//显示原始图像
cvNamedWindow("bin_img",1);
cvShowImage("bin_img",bin_img);
//找到轮廓数目
mode = CV_RETR_EXTERNAL;
contours_num = cvFindContours(bin_img,storage,&contour,sizeof(CvContour),mode,CV_CHAIN_APPROX_NONE);
cout<<contours_num<<endl;//输出轮廓的数目
/************************************************************************/
/* 逐点绘出轮廓 */
/************************************************************************/
CvSeqReader reader;//读序列
int count = 0;
if(contour != 0)//输出组成轮廓的点的数目
{
count = contour->total;
cout<<count<<endl;
}
cvStartReadSeq(contour,&reader,0);
CvPoint pt1;
CvScalar color = CV_RGB(255,255,255);
cvNamedWindow("contour",1);
cvShowImage("contour",pContourImg);
for (int i = 1;i<count;i++)//逐点将轮廓的点绘出
{
CV_READ_SEQ_ELEM(pt1,reader);
cvCircle(pContourImg,pt1,1,color);
cvShowImage("contour",pContourImg);
cvWaitKey(5);
}
cvWaitKey(0);//等待
cvReleaseImage(&bin_img);
cvReleaseImage(&pContourImg);
cvReleaseMemStorage(&storage);
return 0;
}
逐点绘出检测出的轮廓图
最新推荐文章于 2024-08-29 14:45:23 发布