Programming/openCV2015. 10. 5. 12:55

마우스 이벤트에서 마우스 위치를 저장하고

메인 루프에서 imshow와 rectangle을 이용해서

다시 그리게 하는데 영 느리길래.. 속도를 어떻게 하면 올릴수 있을까 고민중..


[링크 : http://answers.opencv.org/question/5163/how-to-use-callback-to-draw-a-rectangle-in-a-video/]


$ cat mousecb.cpp

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>


using namespace std;

using namespace cv;


Point mpos(0,0);


void CallBackFunc(int event, int x, int y, int flags, void* userdata)

{

cout << "pos (" << x << "," << y << ") ";

mpos.x = x;

mpos.y = y;


switch(event)

{

case EVENT_MOUSEMOVE:

cout << "EVENT_MOUSEMOVE" << endl;

break;

case EVENT_LBUTTONDOWN:

cout << "EVENT_LBUTTONDOWN" << endl;

break;

case EVENT_LBUTTONUP:

cout << "EVENT_LBUTTONUP" << endl;

break;


case EVENT_RBUTTONDOWN:

cout << "EVENT_RBUTTONDOWN" << endl;

break;

case EVENT_RBUTTONUP:

cout << "EVENT_RBUTTONUP" << endl;

break;


case EVENT_MBUTTONDOWN:

cout << "EVENT_MBUTTONDOWN" << endl;

break;

case EVENT_MBUTTONUP:

cout << "EVENT_MBUTTONUP" << endl;

break;


case EVENT_LBUTTONDBLCLK:

cout << "EVENT_LBUTTONDBLCLK" << endl;

break;

case EVENT_RBUTTONDBLCLK:

cout << "EVENT_RBUTTONDBLCLK" << endl;

break;

case EVENT_MBUTTONDBLCLK:

cout << "EVENT_MBUTTONDBLCLK" << endl;

break;

/*

case EVENT_MOUSEWHEEL:

cout << "EVENT_MOUSEWHEEL" << endl;

break;

case EVENT_MOUSEHWHEEL:

cout << "EVENT_MOUSEHWHEEL" << endl;

break;

*/

}

}


int main(int argc, char** argv)

{

// Read image from file

Mat img = imread("MyPic.JPG");


//if fail to read the image

if ( img.empty() )

{

cout << "Error loading the image" << endl;

return -1;

}


//Create a window

namedWindow("My Window", 1);


//set the callback function for any mouse event

setMouseCallback("My Window", CallBackFunc, NULL);


//show the image

imshow("My Window", img);

for(;;)

{

rectangle(img, Point(mpos.x - 10, mpos.y - 10),

Point(mpos.x + 10, mpos.y + 10),

Scalar(0,0,0),2,8,0);


imshow("My Window", img);

// Wait until user press some key

if(waitKey(30) >= 0) break;

}


return 0;

} 


+

CV_EXPORTS_W int waitKey(int delay = 0);

delay를 msec 단위로 주는거라.. 이녀석을 줄이면 좀 나아지긴 한다.



thread로 구분해서 하는 예제

[링크 : http://answers.opencv.org/.../waitkey1-timing-issues-causing-frame-rate-slow-down-fix/]

'Programming > openCV' 카테고리의 다른 글

시야각 내 각도 계산  (0) 2015.10.14
openCV + openMP 합치는게 잘 안되네?  (0) 2015.10.11
opencv 마우스 이벤트 관련 2  (0) 2015.10.05
opencv 템플릿 매칭 코드 관련  (0) 2015.10.03
opencv neon 최적화  (0) 2015.10.01
Posted by 구차니