int waitKey(int delay=0);
setMouseCallback(const String& winname, MouseCallback onMouse, void* userdata = 0);
typedef void (*MouseCallback)(int event, int x, int y, int flags, void* userdata);
Mat src;
Point ptOld;
void on_mouse(int event, int x, int y, int flags, void*);
int main(void)
{
src = imread("lenna.bmp");
if (src.empty()) {
cerr << "Image load failed!" << endl;
return -1;
}
namedWindow("src");
setMouseCallback("src", on_mouse);
imshow("src", src);
waitKey();
}
void on_mouse(int event, int x, int y, int flags, void*)
{
switch (event) {
case EVENT_LBUTTONDOWN:
ptOld = Point(x, y);
cout << "EVENT_LBUTTONDOWN: " << x << ", " << y << endl;
break;
case EVENT_LBUTTONUP:
cout << "EVENT_LBUTTONUP: " << x << ", " << y << endl;
break;
case EVENT_MOUSEMOVE:
if (flags & EVENT_FLAG_LBUTTON) {
//cout << "EVENT_MOUSEMOVE: " << x << ", " << y << endl;
//circle(src, Point(x, y), 2, Scalar(0, 255, 255), -1, LINE_AA);
line(src, ptOld, Point(x, y), Scalar(0, 255, 255), 3, LINE_AA);
ptOld = Point(x, y);
imshow("src", src);
}
break;
default:
break;
}
}
int createTrackbar(const String& trackbarname, const String& winname, int* value, int count,
TrackbarCallback onChange = 0, void* userdata = 0);
typedef void (*TrackbarCallback)(int pos, void* userdata);
void on_level_change(int pos, void* userdata);
int main(void)
{
Mat img = Mat::zeros(400, 400, CV_8UC1);
namedWindow("image");
createTrackbar("level", "image", 0, 16, on_level_change, (void*)&img);
imshow("image", img);
waitKey();
}
void on_level_change(int pos, void* userdata)
{
Mat img = *(Mat*)userdata;
img.setTo(pos * 16);
imshow("image", img);
}
main함수에 있는 어떤 영상을 마우스콜백함수 혹은 트랙바 콜백함수를 통해 변경하려면 userdata 포인터를 통해 주솟값을 전달해주거나 전역변수로 설정해야한다.