关闭

鼠标响应绘图

658人阅读 评论(0) 收藏 举报

问题:

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


#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;   
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:63521次
    • 积分:747
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:13篇
    • 译文:0篇
    • 评论:14条
    文章分类
    文章存档
    最新评论