2024年3月30日发(作者:)

if( event == CV_EVENT_LBUTTONDOWN )

{

CvPoint pt = cvPoint(x,y);

//cout<

if(conner_pt_index >-1)

conner_pt_index = -1;

else

{

conner_pt_index = getNearPointIndex(pt);

//添加新的控制点

if(conner_pt_index==-1)

{

if(mark_count<=(WW_MAX_MARK_COUNT-1))

{

Control_pts[mark_count].x = (float)pt.x;

Control_pts[mark_count].y = (float)pt.y;

Control_pts[mark_count].z = 0;

mark_count++;

}

}

}

}

else if ( event == CV_EVENT_MOUSEMOVE ) //修改控制点坐标

{

if(conner_pt_index >-1)

{

Control_pts[conner_pt_index].x = (float)x;

Control_pts[conner_pt_index].y = (float)y;

}

}

};

int main(int argc, char* argv[])

{

CvSize image_sz = cvSize( 1000,1000);

image = cvCreateImage(image_sz , 8, 3 );

cvNamedWindow("Win",0);

cvSetMouseCallback( "Win", on_mouse, 0 );

cvResizeWindow("Win",500,500);

cout<<"============== Bezier curve DEMO =============="<

cout<<" "<

cout<<" mouse to click control point (red) to select a control point"<

cout<<" mouse to modify control point"<

cout<<" mouse on somewhere to add a control point,add three points for add a new curve"<

cout<<" 'W','S' to add precision or reduce precision."<

cout<<" 'Z' to show control points."<

cout<<"===press ESC to exit==="<

//初始化四个控制点

Control_pts[0].x = 200;

Control_pts[0].y = 200;

Control_pts[0].z = 0;

Control_pts[1].x = 300;

Control_pts[1].y = 500;

Control_pts[1].z = 0;

Control_pts[2].x = 400;

Control_pts[2].y = 560;

Control_pts[2].z = 0;

Control_pts[3].x = 500;

Control_pts[3].y = 100;

Control_pts[3].z = 0;

int divs = 50; //控制精细度

for(;;)

{

CvPoint pt_now,pt_pre;

cvZero(image);

//绘制控制点

if(is_showControlLines)

{

for(int i =0;i

{

CvPoint ptc;

ptc.x = (int) Control_pts[i].x;

ptc.y = (int) Control_pts[i].y;

cvCircle( image, ptc, 4, CV_RGB(255,0,0), 1,CV_AA, 0);

}

}

//绘制Bezier曲线

CvPoint3D32f *pControls = Control_pts;

for(int j=0;j

{

for (int i=0;i<=divs;i++)

{

float u = (float)i/divs;

CvPoint3D32f newPt = Bernstein(u,pControls);

pt_now.x = (int)newPt.x;

pt_now.y = (int)newPt.y;

if(i>0) cvLine(image,pt_now,pt_pre,CV_RGB(230,255,0),2,CV_AA, 0 );

pt_pre = pt_now;

}

//画控制线

if(is_showControlLines)DrawControlLine(pControls);

pControls+=3;

}

cvShowImage("Win",image);

int keyCode = cvWaitKey(20);

if (keyCode==27) break;

if(keyCode=='w'||keyCode=='W') divs+=2;

if(keyCode=='s'||keyCode=='S') divs-=2;

if(keyCode=='z'||keyCode=='Z') is_showControlLines = is_showControlLines^1;

//cout<<"precision : "<

}

return 0;

}