利用OpenCV绘制可变直线

转载自: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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值