分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
提取轮廓在OpenCV里有一个函数 cvFindContours :
int cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_contour,int header_size=sizeof(CvContour),int mode=CV_RETR_LIST,int method=CV_CHAIN_APPROX_SIMPLE, CvPoint offset=cvPoint(0,0) );
这个函数用起来很方便,但是随着你使用的深入,你会发现有一些迷惑在这里。比如当你提取轮廓时只需要最外围的一个轮廓,但是你会发现当轮廓画出来时是好几个;当你需要找一个最大轮廓时却发现找出来的却根本就不是你想要的那个。带着这样问题我们再来仔细看看 cvFindContours这个函数。
下边的是一位仁兄写的测试程序和测试图片,说明提取轮廓的两种方法及绘制轮廓中最大等级分析 的问题,非常感谢他的分享,原文戳
这里:
/************************************************************************/ /* 提取轮廓两种方法对比及绘制轮廓'最大等级'分析 */ /************************************************************************/ #include "stdafx.h" #include "cv.h" #include "highgui.h" int main() { IplImage* img = cvLoadImage("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE); IplImage* img_temp = cvCreateImage(cvGetSize(img), 8, 1); cvThreshold(img, img, 128, 255, CV_THRESH_BINARY); CvMemStorage* mem_storage = cvCreateMemStorage(0); CvSeq *first_contour = NULL, *c = NULL; // // 1、 cvNamedWindow("contour1"); cvCopyImage(img, img_temp); double t = (double)cvGetTickCount(); cvFindContours(img_temp, mem_storage, &first_contour); cvZero(img_temp); cvDrawContours( img_temp, first_contour, cvScalar(100), cvScalar(100), 1 ); t = (double)cvGetTickCount() - t; cvShowImage("contour1", img_temp); printf("run1 = %gms\n", t/(cvGetTickFrequency()*1000.)); cvClearMemStorage(mem_storage); // // 2、 cvNamedWindow("contour2"); cvCopyImage(img, img_temp); t = (double)cvGetTickCount(); CvContourScanner scanner = cvStartFindContours(img_temp, mem_storage); while (cvFindNextContour(scanner)); first_contour = cvEndFindContours(&scanner); cvZero(img_temp); cvDrawContours( img_temp, first_contour,