3 This script will test highgui's mouse functionality
6 # name of this test and it's requirements
7 TESTNAME = "cvSetMouseCallback"
8 REQUIRED = ["cvShowImage"]
11 # needed for sys.exit(int) and .works file handling
16 # check requirements and delete old flag file, if it exists
17 if not works.check_files(REQUIRED,TESTNAME):
20 # import the necessary things for OpenCV
22 from python.highgui import *
23 from python.cv import *
25 # global variable which stores information about the pressed mousebuttons
26 mouse_events = [False,False,False,False,False,False,False,False,False,False]
27 event_detected = False
30 win_name = "testing..."
31 EVENTS = ['CV_EVENT_MOUSEMOVE', 'CV_EVENT_LBUTTONDOWN', 'CV_EVENT_RBUTTONDOWN', 'CV_EVENT_MBUTTONDOWN', 'CV_EVENT_LBUTTONUP',
32 'CV_EVENT_RBUTTONUP', 'CV_EVENT_MBUTTONUP' , 'CV_EVENT_LBUTTONDBLCLK','CV_EVENT_RBUTTONDBLCLK','CV_EVENT_MBUTTONDBLCLK']
35 # our callback function, 5th parameter not used here.
36 def callback_function(event,x,y,flag,param):
37 globals()["event_detected"] = True
38 # check if event already occured; if not, output info about new event.
39 if globals()["mouse_events"][event] == False:
40 print "Event "+globals()["EVENTS"][event]+" detected."
41 globals()["mouse_events"][event] = True
45 # create a window ('cvNamedWindow.works' exists, so it must work)
46 cvNamedWindow(win_name,CV_WINDOW_AUTOSIZE)
47 # show the baboon in the window
48 PREFIX = os.environ["top_srcdir"]+"/tests/python/testdata/images/"
49 cvShowImage(win_name, cvLoadImage(PREFIX+"cvSetMouseCallback.jpg"))
50 # assign callback function 'callback_function' to window, no parameters used here
51 cvSetMouseCallback( win_name, callback_function )
53 # give the user information about the test and wait for input
54 print "(INFO) Please hover the mouse over the baboon image and press"
55 print "(INFO) your available mousebuttons inside the window to 'PASS' this test."
56 print "(INFO) You may also perform double-clicks."
57 print "(INFO) Press a key on your keyboard ot wait 20 seconds to continue."
58 print "(HINT) If no mouseevent was detected this test will be remarked as 'FAIL'."
60 # now wait 20 seconds for user to press a key
63 # reset mouse callback
64 cvSetMouseCallback( win_name, 0 )
66 cvDestroyWindow( win_name )
68 # check if a mouse event had beed detected
69 if not event_detected:
70 # user didn't interact properly or mouse functionality doesn't work correctly
71 print "(ERROR) No mouse event detected."
74 # create flag file for following tests
75 works.set_file(TESTNAME)