Multimedia Programming 19: Mouse with OpenCV

Preview:

DESCRIPTION

Multimedia Programming 19: Mouse with OpenCV. Departments of Digital Contents Sang Il Park. Mouse Callback in OpenCV. OpenCV 에서 마우스 사용하기. Mouse. With OPENCV. Keyboard. With OPENCV. int cvWaitKey( interval ). Mouse. With OPENCV. cvSetMouseCallback( … ). Mouse Callback?. - PowerPoint PPT Presentation

Citation preview

Multimedia Programming 19:

Mouse with OpenCVDepartments of Digital

ContentsSang Il Park

Mouse Callback in OpenCV

OpenCV 에서 마우스 사용하기

Mouse

With OPENCV

Keyboard With OPENCV

int cvWaitKey(interval)

Mouse With OPENCV

cvSetMouseCallback(…)

Mouse Callback?• 콜백 함수 (Callback):

– 특정 함수가 어떤 함수의 인자로서 사용될 때 이 함수를 콜백 함수라고 한다 .

• 마우스 콜백 (mouse callback):– 마우스의 상태에 변화가 생겼을 때 불리는 함수– 예시 )

• 마우스가 움직인다• 왼 ( 오른 ) 버튼이 눌리고 있음 ( 올라가고 있음 ), 더블클릭 ,

마우스 콜백 사용하기• OpenCV 에서는 사용자가 임의의 함수를 만들고

이를 마우스에 변화가 생겼을 때 호출되게 할 수 있다 .

• 이를 위해서는 다음 2 가지 일을 하여야 한다 :1. 구현 (Implementation)

• 마우스에 변화가 생겼을 때 무엇을 할 것인가 ? 를 정의함 .

2. 등록 (Registration)• 위에 정의한 함수를 opencv 에게 사용하겠다고 등록시킴 .

void cvSetMouseCallback(window_name, yourFunction)

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

구현 (Implementation)

• 함수의 이름은 자유롭게 지을 수 있다• 예 ) on_mouse(..), myMouse(…), …

• 입력 인자들은 OpenCV 가 알려주는 것으로 마우스에 어떤 변화가 생겼는 지 알려주는 정보가 담겨있다 .

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

Implementation

• OpenCV 가 넘겨주는 메세지• 어떤 마우스 관련 이벤트 종류인지 알려줌• Examples)

CV_EVENT_MOUSEMOVE ( 마우스가 움직임 )CV_EVENT_LBUTTONDOWN ( 왼쪽버튼이 눌리고 있음 )CV_EVENT_RBUTTONDOWN ( 오른버튼이 눌리고 있음 )CV_EVENT_MBUTTONDOWN ( 중간버튼이 눌리고 있음 )CV_EVENT_LBUTTONUP ( 왼쪽버튼이 올라가고 있음 ) CV_EVENT_RBUTTONUP ( 오른버튼이 올라가고 있음 )CV_EVENT_MBUTTONUP ( 중간버튼이 올라가고 있음 )

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

Implementation

• OpenCV 가 넘겨주는 메세지• 마우스의 현재 위치를 표현 (x,y)• 값은 이미지 좌표계를 기준으로 함

( 왼쪽 윗편이 원점 )

y (0,0)(8,2)

x

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

Implementation

• OpenCV 가 넘겨주는 메세지• 마우스 및 키보드의 현재 상태

– Event 는 마우스가 현재 취하는 동작– Flags 는 현재 마우스나 키보드의 상태

• Examples)CV_EVENT_FLAG_LBUTTON ( 왼버튼이 눌린 상태 ) CV_EVENT_FLAG_RBUTTON ( 오른버튼이 눌린 상태 )CV_EVENT_FLAG_MBUTTON ( 중간버튼이 눌린 상태 )CV_EVENT_FLAG_CTRLKEY ( 콘트롤키가 눌린 상태 )CV_EVENT_FLAG_SHIFTKEY ( 쉬프트키가 눌린 상태 )CV_EVENT_FLAG_ALTKEY ( 알트키가 눌린 상태 )

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

등록 (Registration)

• 어떤 창 (window_name) 에 마우스에 변화가 생기면 특정 작업 (yourFunction) 을 수행하라 .

• Example)cvSetMouseCallback( “test”, on_mouse); “test” 라는 창에서 마우스가 움직이면 on_mouse 라는 함수를 호출하라

void cvSetMouseCallback(window_name, yourFunction)

코딩연습• 마우스에 변화가 생기면 그 때 마우스의 좌표를

출력하라

1. 구현 ( Implement your )

2. main() 함수 내에 등록 (registration)

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

printf(“mouse: %d %d \n”, x, y);}

cvSetMouseCallback(“test”, myMouse);

Coding Practice• 마우스 왼쪽버튼을 누른 곳에 검은 점을 찍으라

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

if(event == CV_EVENT_LBUTTONDOWN){

cvSet2D(img, y, x, CV_RGB(0,0,0));cvShowImage(“test”, img);

}}

1. 구현 ( Implement your )

2. main() 함수 내에 등록 (registration)

cvSetMouseCallback(“test”, myMouse);

팔렛트 만들기• 마우스 클릭 한 곳의 색 알아내기

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

if(event == CV_EVENT_LBUTTONDOWN){

s = cvGet2D(img, y, x);}

}

구현부 (Implement)

OpenCV 를 이용한 도형 그리기

OpenCV 에서 마우스 사용하기

선 그리기void cvLine( IplImage, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1);

void cvLine( IplImage, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1);

• pt1 부터 pt2 까지 주어진 두깨로 선을 그린다• 점의 위치를 표현하는 구조체 : CvPoint:

– cvPoint(x,y) 함수를 사용하면 쉽게 쓸 수 있다– 예 ) (0,0) 부터 (100,100) 까지 검은 선분을 그리는 명령

struct CvPoint{

int x; // x-coordinateint y; // y-coordinate

}

struct CvPoint{

int x; // x-coordinateint y; // y-coordinate

}

cvLine( img, cvPoint(0,0), cvPoint(100,100), CV_RGB(0,0,0), 3); cvLine( img, cvPoint(0,0), cvPoint(100,100), CV_RGB(0,0,0), 3);

사각형 그리기void cvRectangle( IplImage, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1 )

void cvRectangle( IplImage, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1 )

• pt1 를 pt2 모서리로 가지는 사각형을 그린다

pt1

pt2

두께에 -1 을 주면 어떻게 될까 ?

원 그리기void cvCircle( IplImage, CvPoint center, int radius, CvScalar color, int thickness=1 )

void cvCircle( IplImage, CvPoint center, int radius, CvScalar color, int thickness=1 )

• 주어진 중심과 반지름을 갖는 원을 그린다

centerradius

코딩연습1. 마우스의 왼쪽 버튼 클릭하고 드래깅하여 그

크기만큼 사각형을 그리라

2. 마우스의 오른 버튼이 눌린 곳에 반지름 50 짜리 원을 그리라

또는

마우스의 오른 버튼을 드래깅하여 그 크기만큼 원을 그리라

Topics for the Term Project

Feature based Image Metamorphosis

• Thaddeus Beier and Shawn Neely, “Feature-Based Image Metamorphosis “, SIGGRAPH 1992

Image Deformation

• S. Schaefer, T. McPhail, J. Warren, “Image Deformation Using Moving Least Squares”, SIGGRAPH 2006

Texture synthesis

• A. A. Efros and T. K. Leung, "Texture Synthesis by Non-parametric Sampling", ICCV 99

• Wei and Levoy, “Fast Texture Synthesis using Tree-structured Vector Quantization “, SIGGRAPH 2000

Image Analogy

• A. Hertzmann, C. Jacobs, N. Oliver, B. Curless, D. Salesin, “Image Analogy”,SIGGRAPH 2001

Colorization

• Tomihisa Welsh, Michael Ashikhmin, and Klaus Mueller, ”Transferring color to grayscale images”, SIGGRAPH 2002

Image Blending

• Patrick Perez, Michel Gangnet, Andrew Blake, “Poisson Image Editing”, SIGGRAPH 2003

• http://www.cs.tau.ac.il/~tommer/adv-graphics/ex1.htm

source images Poisson seamless cloning

Matting

• J. Sun, J. Jia, C. Tang, H. Shum, “Poisson Matting”, SIGGRAPH 2004

source image opacityextracted

foreground objectimage

composition

Content-Aware Image Resizing

• Shai Avidan, Ariel Shamir, “Seam Carving for Content-Aware Image resizeing”, SIGGRAPH 2006

• http://www.seamcarving.com/

source image resized image

Painterly Rendering

• Aaron Hertzmann, "Painterly Rendering with Curved Brush Strokes of Multiple Sizes“, SIGGRAPH 1998

Painterly Rendering2

• Michio Shiraishi and Yasushi Yamaguchi , “Image Moment-Based Stroke Placement”, ACM SIGGRAPH 99 Conference abstracts and applications, 1999

High Dynamic Range Compression

• Raanan Fattal, Dani Lischinski, Michael Werman,“Gradient Domain High Dynamic Range Compression”, ACM SIGGRAPH 2002