1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
int main()
{ IplImage *img=cvLoadImage(votre image);
IplImage *image ;
CvMemStorage* storage=cvCreateMemStorage();
int i, count = rand()%100 + 1, hullcount;
CvPoint pt0;
CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2,sizeof(CvContour),sizeof(CvPoint),storage );
CvSeq* hull;
for( i = 0; i < count; i++ )
{ pt0.x = rand() % (img->width/2) + img->width/4;
pt0.y = rand() % (img->height/2) + img->height/4;
cvSeqPush( ptseq, &pt0 );}
IplImage* g_gray = NULL;
int g_thresh = 100;
CvMemStorage* g_storage = NULL;
CvMemStorage* g=NULL ;
if( g_storage == NULL ){
g_gray = cvCreateImage( cvGetSize( img ), 8, 1 );
g_storage = cvCreateMemStorage(0);
} else {
cvClearMemStorage( g_storage );
}
CvSeq* contours =0;
cvCvtColor( img, g_gray, CV_BGR2GRAY );
cvThreshold( g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY );
cvFindContours( g_gray, g_storage, &contours );
// hull = cvConvexHull2( contours, 0, CV_CLOCKWISE, 0 );
CvSeq *li=cvConvexityDefects(contours,hull,g) ;
for(int i=0;i<li->total;i++)
{CvPoint* q = (CvPoint*)cvGetSeqElem ( contours, i );
int k=q->x ;
int v=q->y ;
CvScalar pix=cvGet2D(img,v,k) ;
pix.val[0]=0 ;
pix.val[1]=0;
pix.val[2]=255 ;
cvSet2D(img,v,k,pix);
}
hullcount = hull->total;
pt0 = **CV_GET_SEQ_ELEM( CvPoint*, hull, hullcount - 1 );
for( i = 0; i < hullcount; i++ )
{ CvPoint pt = **CV_GET_SEQ_ELEM( CvPoint*, hull, i );
cvLine( img, pt0, pt, CV_RGB( 0, 255, 0 ));
pt0 = pt; }
cvNamedWindow("ma fan",CV_WINDOW_AUTOSIZE);
cvShowImage("ma fan",img);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&img);
} |
Partager