📄 snake3.cpp
字号:
#ifdef _CH_
#pragma package <opencv>
#endif
#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <math.h>
#endif
#define w 500
int levels = 0;
CvSeq* contours = 0;
int ialpha = 20;
int ibeta=20;
int igamma=20;
void on_snakerun(int pos, int numcontours,IplImage* image)
{
if(numcontours<2) return;
if(image==NULL) return;
int length = numcontours;
IplImage* imgsnake=0;
imgsnake=(IplImage*)cvClone(image);
IplImage* image2=0;
image2=(IplImage*)cvClone(image);
CvPoint* point = new CvPoint[length]; //分配轮廓点
memset(point,0,sizeof(CvPoint)*length);
CvSeqReader reader;
// CvPoint pt= cvPoint(0,0);
int i,j;
float alpha=ialpha/100.0f;
float beta=ibeta/100.0f;
float gamma=igamma/100.0f;
CvSize size;
size.width=3;
size.height=3;
CvTermCriteria criteria;
criteria.type=CV_TERMCRIT_ITER;
criteria.max_iter=1000;
criteria.epsilon=0.1;
cvSnakeImage(imgsnake, point,length,&alpha,&beta,&gamma,CV_VALUE,size,criteria,0 );
//显示曲线
for(i=0;i<length;i++)
{
j = (i+1)%length;
cvLine( image2, point[i],point[j],CV_RGB( 0, 255, 0 ),1,8,0 );
}
// cvNamedWindow( "wndname2", 2 );
// cvShowImage( "image", img );
// cvShowImage("wndname2", image2);
// delete []point;
}
void on_trackbar(int pos)
{
IplImage* cnt_img = cvCreateImage( cvSize(w,w), 8, 3 );
CvSeq* _contours = contours;
int len=0;
len=contours->total;
// int _levels = levels - 3;
int _levels = levels;
if( _levels <= 0 ) // get to the nearest face to make it look more funny
{
cnt_img=cvLoadImage("black.bmp", 1);
cvShowImage( "contours", cnt_img );
cvReleaseImage( &cnt_img );
return;
}
// _contours = _contours->h_next->h_next->h_next;
cvZero( cnt_img );
cvDrawContours( cnt_img, _contours, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels, 3, CV_AA, cvPoint(0,0) );
cvShowImage( "contours", cnt_img );
cvReleaseImage( &cnt_img );
}
int main( int argc, char** argv )
{
int i, j;
CvMemStorage* storage = cvCreateMemStorage(0);
IplImage* img = cvCreateImage( cvSize(w,w), 8, 1 );
cvZero( img );
for( i=0; i < 6; i++ )
{
int dx = (i%2)*250 - 30;
int dy = (i/2)*150;
CvScalar white = cvRealScalar(255);
CvScalar black = cvRealScalar(0);
if( i == 0 )
{
for( j = 0; j <= 10; j++ )
{
double angle = (j+5)*CV_PI/21;
cvLine(img, cvPoint(cvRound(dx+100+j*10-80*cos(angle)),
cvRound(dy+100-90*sin(angle))),
cvPoint(cvRound(dx+100+j*10-30*cos(angle)),
cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);
}
}
cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(100,70), 0, 0, 360, white, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(10,5), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+150, dy+150), cvSize(40,10), 0, 0, 360, black, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+27, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
cvEllipse( img, cvPoint(dx+273, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
}
cvNamedWindow( "image", 1 );
cvShowImage( "image", img );
// cvSaveImage( "GHW.bmp", img);
cvFindContours( img, storage, &contours, sizeof(CvContour),
CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
// comment this out if you do not want approximation
contours = cvApproxPoly( contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );
cvNamedWindow( "contours", 1 );
cvCreateTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );
on_snakerun(0,contours->total,img);
on_trackbar(0);
cvWaitKey(0);
cvReleaseMemStorage( &storage );
cvReleaseImage( &img );
return 0;
}
#ifdef _EiC
main(1,"");
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -