有思想的鱼

coding fish

鼠标响应绘图

问题:

这个相当于是画点,移动快了就不连续了。


#include <cv.h>
#include <highgui.h>

void my_mouse_callback(
	int event, int x, int y, int flags, void* param 
	);

CvPoint p = cvPoint(0,0);
CvPoint q = cvPoint(0,0);
int thickness=3;
bool draw_line = false;

void drawingline( IplImage* img, CvPoint p, CvPoint q ) {
	cvLine (
		img, 
		cvPoint(p.x,p.y),
		cvPoint(q.x,q.y),
		cvScalar(0xff,0xff,0x00),    
		thickness
		);
}

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

	IplImage* image = cvCreateImage( 
		cvSize(600,500),
		IPL_DEPTH_8U,
		3
		);
	cvZero( image );
	cvNamedWindow( " Example" );

	cvSetMouseCallback( 
		" Example", 
		my_mouse_callback, 
		(void*) image 
		);
	while( 1 ) {
		if( draw_line ) drawingline( image, p , q ); 
		cvShowImage( " Example", image );
		if( cvWaitKey( 15 )==27 ) break;
	}

	cvReleaseImage( &image );
	cvDestroyWindow( " Example" );
}

void my_mouse_callback(
	int event, int x, int y, int flags, void* param )
{

	IplImage* image = (IplImage*) param;

	switch( event ) {
	case CV_EVENT_MOUSEMOVE: {
		if( draw_line ) {
		p.x = x;
		p.y = y;
		q = p;
		}
	 }
	break;
	case CV_EVENT_LBUTTONDOWN: {
		draw_line = true;
	    drawingline( image, p, q );
	   }
	break;   
	case CV_EVENT_LBUTTONUP: {
		draw_line = false; 
		drawingline( image, p, q );
	   }
	break;   
	}
}

画连续的线需要改进:

mousemove状态下,画一下段线段后,把终点赋给起点。这样每次经过一个cpu事件就画出一条小线段。(经过一个cpu时间,计算机进行一次采样)。


修改后的代码

#include <cv.h>
#include <highgui.h>

void my_mouse_callback(
	int event, int x, int y, int flags, void* param 
	);

CvPoint p;
CvPoint q;
int thickness=3;
bool draw_line = false;
IplImage* temp = NULL;

void drawingline( IplImage* img, CvPoint p, CvPoint q ) {
	cvLine (
		img, 
		cvPoint(p.x,p.y),
		cvPoint(q.x,q.y),
		cvScalar(0xff,0xff,0x00),    
		thickness
		);
}

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

	IplImage* image = cvCreateImage( 
		cvSize(600,500),
		IPL_DEPTH_8U,
		3
		);

	cvZero( image );
	cvNamedWindow( "Example" );

	cvSetMouseCallback( 
		"Example", 
		my_mouse_callback, 
		(void*) image 
		);
	
	while( 1 ) {  
		if ( draw_line )
			drawingline( image, p , q );   
		cvShowImage( "Example", image );  
		if( cvWaitKey( 15 )==27 ) break;  
	}  
	cvWaitKey(-1);

	cvReleaseImage( &image );
//	cvReleaseImage( &temp );
	cvDestroyWindow( "Example" );
}

void my_mouse_callback(
	int event, int x, int y, int flags, void* param )
{

	IplImage* image = (IplImage*) param;
	
	switch( event ) 
	{
	case CV_EVENT_LBUTTONDOWN: {
		draw_line = true;
		p.x = x;
		p.y = y;
	    q = p;
		drawingline( image, p , q ); 
							   }
	break; 
	case CV_EVENT_MOUSEMOVE: {
		if( draw_line ) {
			q.x = x;
			q.y = y;
			drawingline( image, p , q );
			p = q;
		}
	 }
	 break;

	case CV_EVENT_LBUTTONUP: {
		draw_line = false; 
	    }
	break;   
	}
}


阅读更多
文章标签: image callback
个人分类: OpenCV
想对作者说点什么? 我来说一句

C# 鼠标绘图鼠标绘图

2010年08月26日 27KB 下载

没有更多推荐了,返回首页

不良信息举报

鼠标响应绘图

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭