OpenCv 使用vector画出轮廓外接矩形

Hai

 1 IplImage* printrect(IplImage *contourim)
 2 {
 3     
 4     IplImage *rectim=cvCreateImage(cvGetSize(contourim), IPL_DEPTH_8U, 3);
 5     int flag=1;
 6     vector points;
 7      for(;contourSeq!=NULL;contourSeq=contourSeq->h_next)
 8     {
 9         
10         
11         for(int i=0;itotal;i++) 
12         {
13             CvPoint p;
14             CvPoint* p1=(CvPoint*)cvGetSeqElem(contourSeq,i);
15             p.x=p1->x;
16             p.y=p1->y;
17             points.push_back(p);
18             
19         }
20         printf("No.%d contoured has pushed back\n",flag);
21         flag++;
22          CvRect rect = boundingRect(points); 
23          CvPoint pt1,pt2;  
24          pt1.x=rect.x;  
25          pt1.y=rect.y;  
26          pt2.x=rect.x+rect.width;  
27          pt2.y=rect.y+rect.height;
28          cvRectangle(contourim,pt1,pt2,CV_RGB(0,0,255));
29          points.swap(vector());//每次使用完必须清空容器
30     }
31     rectim=contourim;
32     return rectim;
33 
34     
35 }
printrect

将一个轮廓的点全部置于轮廓中并画出其外接矩形后需要清空vector

points.swap(vector());

否则第二个轮廓点放入容器中会画出这两个轮廓所有点共同的外接矩形

直接运行代码

  1 #include "cv.h"
  2 #include "highgui.h"
  3 #include 
  4 #include 
  5 #include 
  6 #include 
  7 //#include 
  8 using namespace cv;
  9 CvSeq *contourSeq = NULL,*contourSeq1=NULL,*contourSeq4rect=NULL;
 10 
 11 IplImage* ImageThreshold(IplImage* src)
 12 {
 13     IplImage *gray,*binaryim;
 14     int height,width;
 15     gray=cvCreateImage(cvGetSize(src),src->depth,1);
 16     cvCvtColor(src,gray,CV_BGR2GRAY);
 17     height=gray->height;
 18     width=gray->width;
 19     printf("The canvas'width is :%d,  height is :%d\n\n",width,height);
 20 
 21     binaryim=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
 22     cvThreshold(gray,binaryim,128,255,CV_THRESH_BINARY_INV);
 23     return binaryim;
 24 
 25 }
 26 
 27 IplImage* printlunkuo(IplImage *binaryim)
 28 {
 29     CvMemStorage *contourStorage=cvCreateMemStorage();
 30     CvMemStorage *contourStorage4rect=cvCreateMemStorage();
 31     cvFindContours(binaryim, contourStorage, &contourSeq, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0)); 
 32     
 33     IplImage *pOutlineImage = cvCreateImage(cvGetSize(binaryim), IPL_DEPTH_8U, 3);    
 34     int nLevels = 5;
 35 
 36     cvRectangle(pOutlineImage, cvPoint(0, 0), cvPoint(pOutlineImage->width, pOutlineImage->height), CV_RGB(0,0,0), CV_FILLED);    
 37     cvDrawContours(pOutlineImage, contourSeq, CV_RGB(255,0,0), CV_RGB(0,255,0), nLevels,0.5); 
 38     
 39     int contourcount=0;
 40     for(;contourSeq!=NULL;contourSeq=contourSeq->h_next)
 41     {
 42         contourcount++;
 43     }
 44     printf("contour count is %d\n",contourcount);
 45 
 46     
 47     int pointcount=0;
 48     cvFindContours(binaryim, contourStorage, &contourSeq, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));//重置contourSeq序列
 49 
 50         for(;contourSeq!=NULL;contourSeq=contourSeq->h_next)
 51     {
 52         for(int i=0;itotal;i++)  
 53     {  
 54         CvPoint* p=(CvPoint*)cvGetSeqElem(contourSeq,i); 
 55         cvCircle(pOutlineImage, *p, 1, CV_RGB(255, 255,255), 1); 
 56         printf("p->x=%d,p->y=%d\n",p->x,p->y); 
 57         pointcount++;
 58     } 
 59         printf("point count =%d\n",pointcount);
 60         pointcount=0;
 61         
 62        }
 63     cvFindContours(binaryim, contourStorage4rect, &contourSeq, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));//重置contourSeq
 64 
 65     return pOutlineImage;
 66 
 67 
 68 
 69 }
 70 
 71 IplImage* printrect(IplImage *contourim)
 72 {
 73     
 74     IplImage *rectim=cvCreateImage(cvGetSize(contourim), IPL_DEPTH_8U, 3);
 75     int flag=1;
 76     vector points;
 77      for(;contourSeq!=NULL;contourSeq=contourSeq->h_next)
 78     {
 79         
 80         
 81         for(int i=0;itotal;i++) 
 82         {
 83             CvPoint p;
 84             CvPoint* p1=(CvPoint*)cvGetSeqElem(contourSeq,i);
 85             p.x=p1->x;
 86             p.y=p1->y;
 87             points.push_back(p);
 88             
 89         }
 90         printf("No.%d contoured has pushed back\n",flag);
 91         flag++;
 92          CvRect rect = boundingRect(points); 
 93          CvPoint pt1,pt2;  
 94          pt1.x=rect.x;  
 95          pt1.y=rect.y;  
 96          pt2.x=rect.x+rect.width;  
 97          pt2.y=rect.y+rect.height;
 98          cvRectangle(contourim,pt1,pt2,CV_RGB(0,0,255));
 99          points.swap(vector());//每次使用完必须清空容器
100     }
101     rectim=contourim;
102     return rectim;
103 
104     
105 }
106 
107 
108 
109 int main(int argc,char** argv)
110 {
111     IplImage *src,*binaryim,*contourim,*rectim;
112     src=cvLoadImage("gtest2.bmp",1);
113 
114     binaryim=ImageThreshold(src);
115     cvNamedWindow("Binary_image",0);    
116     cvShowImage("Binary_image",binaryim);
117 
118     contourim = printlunkuo(binaryim);
119     cvNamedWindow("Contour_image",0);    
120     cvShowImage("Contour_image",contourim);
121 
122     rectim=printrect(contourim);
123     cvNamedWindow("Rect_image",0);    
124     cvShowImage("Rect_image",rectim);
125     
126     
127     
128     cvWaitKey(0);
129 
130     cvDestroyWindow("Binary_image");
131     cvReleaseImage(&binaryim);
132     cvDestroyWindow("Contours_image");
133     cvReleaseImage(&contourim);
134     cvDestroyWindow("Rect_image");
135     cvReleaseImage(&rectim);
136     
137     return 0;
138 
139 }
The all

 

转载于:https://www.cnblogs.com/gaohai/p/5744059.html

你可能感兴趣的:(人工智能)