opencv 轮廓的凸包,凸缺陷

来源:互联网 发布:网游mac版的游戏有什么 编辑:程序博客网 时间:2024/05/16 08:03

代码:

[cpp] view plaincopy
  1. #include <cv.h>  
  2. #include <cxcore.h>  
  3. #include <highgui.h>  
  4. #include <cvaux.h>  
  5. #include <iostream>  
  6. using namespace std;  
  7.   
  8. int main()  
  9. {  
  10.     IplImage *src = cvLoadImage("f:\\images\\test2.bmp",CV_LOAD_IMAGE_GRAYSCALE);  
  11.     IplImage *dst = cvCreateImage(cvGetSize(src),8,3);cvZero(dst);  
  12.     CvMemStorage *storage = cvCreateMemStorage();  
  13.     CvSeq *contour = NULL , *hull = NULL;  
  14.   
  15.     CvContourScanner scanner = cvStartFindContours(src,storage);  
  16.     while((contour = cvFindNextContour(scanner)) != NULL){  
  17.         cvDrawContours(dst,contour,CV_RGB(255,0,0),CV_RGB(0,255,0),0);  
  18.           
  19.         cout<<cvCheckContourConvexity(contour)<<endl;  
  20.   
  21.         hull = cvConvexHull2(contour,0,CV_CLOCKWISE,0);  
  22.           
  23.         CvPoint pt0 = **(CvPoint**)cvGetSeqElem(hull,hull->total - 1);  
  24.         for(int i = 0;i<hull->total;++i){  
  25.             CvPoint pt1 = **(CvPoint**)cvGetSeqElem(hull,i);  
  26.             cvLine(dst,pt0,pt1,CV_RGB(0,0,255));  
  27.             pt0 = pt1;  
  28.         }  
  29.   
  30.         CvSeq *defect = cvConvexityDefects(contour,hull);  
  31.           
  32.         for(int i = 0;i<defect->total;++i){  
  33.             CvConvexityDefect df = *(CvConvexityDefect*)cvGetSeqElem(defect,i);  
  34.             cvCircle(dst,*df.start,2,CV_RGB(255,255,0),-1);  
  35.             cvCircle(dst,*df.end,2,CV_RGB(255,255,0),-1);  
  36.             cvCircle(dst,*df.depth_point,2,CV_RGB(0,255,255),-1);  
  37.         }  
  38.           
  39.         cvShowImage("dst",dst);  
  40.         cvWaitKey();  
  41.     }  
  42.     cvEndFindContours(&scanner);  
  43. }  






网址:http://blog.csdn.net/fdl19881/article/details/6732682
0 0