转载自:http://blog.csdn.net/quarryman/article/details/6450788
变直线就是绘制后的直线可以修改起始点和终止点(当然包括直线的长度)。绘制后的直线两端有矩形方块,在矩形方块内拖动光标就可以修改直线了。
#include <cv.h>
#include <highgui.h>
#pragma comment( lib, "cv.lib" )
#pragma comment( lib, "cxcore.lib" )
#pragma comment( lib, "highgui.lib" )
IplImage* org=0;
IplImage* tmp=0;
IplImage* img=0;
void on_mouse( int event, int x, int y, int flags, void* ustc)
{
static CvPoint pre={-1,-1};
static CvPoint cur={-1,-1};
static int complete=0;
static int inbrect=0;
static int inerect=0;
if( event == CV_EVENT_LBUTTONDOWN && !complete)
{
if((short)x<5)
{
x=5;
}
else if((short)x>org->width-5)
{
x=org->width-5;
}
if((short)y<5)
{
y=5;
}
else if((short)y>org->height-5)
{
y=org->height-5;
}
pre=cvPoint(x,y);
cur=cvPoint(x,y);
cvRectangle(tmp,cvPoint(x-5,y-5),cvPoint(x+5,y+5),CV_RGB(0,255,0));
cvCopy(tmp,img);
cvShowImage("image",tmp);
}
else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) && !complete)
{
cvCopy(img,tmp);
if((short)x<5)
{
x=5;
}
else if((short)x>org->width-5)
{
x=org->width-5;
}
if((short)y<5)
{
y=5;
}
else if((short)y>org->height-5)
{
y=org->height-5;
}
cur = cvPoint(x,y);
cvLine( tmp, pre, cur, cvScalar(0,255,0,0), 1, CV_AA, 0 );
cvRectangle(tmp,cvPoint(x-5,y-5),cvPoint(x+5,y+5),CV_RGB(0,255,0));
cvShowImage( "image", tmp );
}
else if(event == CV_EVENT_LBUTTONUP && !complete)
{
complete=1;
}
else if((event == CV_EVENT_LBUTTONDOWN) && complete)
{
if(x>=pre.x-5 && x<=pre.x+5 && y>=pre.y-5 && y<=pre.y+5)
{
inbrect=1;
}
else
{
inbrect=0;
}
if(x>=cur.x-5 && x<=cur.x+5 && y>=cur.y-5 && y<=cur.y+5)
{
inerect=1;
}
else
{
inerect=0;
}
}
else if( (event == CV_EVENT_MOUSEMOVE) && (flags & CV_EVENT_FLAG_LBUTTON) && complete && (inbrect || inerect))
{
cvCopy(org,tmp);
if(inbrect)
{
if(cur.x<5)
{
cur.x=5;
}
else if(cur.x>org->width-5)
{
cur.x=org->width-5;
}
if(cur.y<5 && cur.y>=0)
{
cur.y=5;
}
else if(cur.y>org->height-5)
{
cur.y=org->height-5;
}
cvRectangle(tmp,cvPoint(cur.x-5,cur.y-5),cvPoint(cur.x+5,cur.y+5),CV_RGB(0,255,0));
if((short)x<5)
{
x=5;
}
else if((short)x>org->width-5)
{
x=org->width-5;
}
if((short)y<5)
{
y=5;
}
else if((short)y>org->height-5)
{
y=org->height-5;
}
cvRectangle(tmp,cvPoint(x-5,y-5),cvPoint(x+5,y+5),CV_RGB(0,255,0));
cvLine( tmp, cvPoint(x,y),cur, cvScalar(0,255,0,0), 1, CV_AA, 0 );
pre=cvPoint(x,y);
}
else if(inerect)
{
if(pre.x<5)
{
pre.x=5;
}
if(pre.x>org->width-5)
{
pre.x=org->width-5;
}
if(pre.y<5)
{
pre.y=5;
}
if(pre.y>org->height-5)
{
pre.y=org->height-5;
}
cvRectangle(tmp,cvPoint(pre.x-5,pre.y-5),cvPoint(pre.x+5,pre.y+5),CV_RGB(0,255,0));
if((short)x<5)
{
x=5;
}
else if((short)x>org->width-5)
{
x=org->width-5;
}
if((short)y<5)
{
y=5;
}
else if((short)y>org->height-5)
{
y=org->height-5;
}
cvRectangle(tmp,cvPoint(x-5,y-5),cvPoint(x+5,y+5),CV_RGB(0,255,0));
cvLine( tmp, pre, cvPoint(x,y), cvScalar(0,255,0,0), 1, CV_AA, 0 );
cur=cvPoint(x,y);
}
cvShowImage( "image", tmp );
}
}
int main()
{
org=cvLoadImage("lena.jpg",1);
tmp=cvCloneImage(org);
img=cvCloneImage(org);
cvNamedWindow("image", 1 );
cvShowImage("image",org);
cvSetMouseCallback( "image", on_mouse, 0 );
cvWaitKey(0);
cvDestroyWindow("image");
cvReleaseImage(&org);
cvReleaseImage(&tmp);
cvReleaseImage(&img);
return 0;
}