3 print "OpenCV Python version of lkdemo"
7 # import the necessary things for OpenCV
9 from opencv import highgui
11 #############################################################################
17 #############################################################################
18 # some "global" variables
26 # the default parameters
30 #############################################################################
33 # the callback on the trackbar
34 def on_mouse (event, x, y, flags, param):
36 # we will use the global pt and add_remove_pt
41 # not initialized, so skip
44 if event == highgui.CV_EVENT_LBUTTONDOWN:
45 # user has click, so memorize it
46 pt = cv.cvPoint (x, y)
49 #############################################################################
50 # so, here is the main part of the program
52 if __name__ == '__main__':
55 # try to get the device number from the command line
56 device = int (sys.argv [1])
58 # got it ! so remove it from the arguments
60 except (IndexError, ValueError):
61 # no device number on the command line, assume we want the 1st device
64 if len (sys.argv) == 1:
65 # no argument on the command line, try to use the camera
66 capture = highgui.cvCreateCameraCapture (device)
69 # we have an argument on the command line,
70 # we can assume this is a file name, so open it
71 capture = highgui.cvCreateFileCapture (sys.argv [1])
73 # check that capture device is OK
75 print "Error opening capture device"
78 # display a small howto use it
79 print "Hot keys: \n" \
80 "\tESC - quit the program\n" \
81 "\tr - auto-initialize tracking\n" \
82 "\tc - delete all the points\n" \
83 "\tn - switch the \"night\" mode on/off\n" \
84 "To add/remove a feature point click it\n"
86 # first, create the necessary windows
87 highgui.cvNamedWindow ('LkDemo', highgui.CV_WINDOW_AUTOSIZE)
89 # register the mouse callback
90 highgui.cvSetMouseCallback ('LkDemo', on_mouse, None)
95 # 1. capture the current image
96 frame = highgui.cvQueryFrame (capture)
98 # no image captured... end the processing
102 # create the images we need
103 image = cv.cvCreateImage (cv.cvGetSize (frame), 8, 3)
104 grey = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1)
105 prev_grey = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1)
106 pyramid = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1)
107 prev_pyramid = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1)
108 eig = cv.cvCreateImage (cv.cvGetSize (frame), cv.IPL_DEPTH_32F, 1)
109 temp = cv.cvCreateImage (cv.cvGetSize (frame), cv.IPL_DEPTH_32F, 1)
112 # copy the frame, so we can draw on it
113 cv.cvCopy (frame, image)
115 # create a grey version of the image
116 cv.cvCvtColor (image, grey, cv.CV_BGR2GRAY)
119 # night mode: only display the points
123 # we want to search all the good points
124 # create the wanted images
126 # search the good points
127 points [1] = cv.cvGoodFeaturesToTrack (
130 quality, min_distance, None, 3, 0, 0.04)
132 # refine the corner locations
133 cv.cvFindCornerSubPix (
136 cv.cvSize (win_size, win_size), cv.cvSize (-1, -1),
137 cv.cvTermCriteria (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS,
140 elif len (points [0]) > 0:
141 # we have points, so display them
143 # calculate the optical flow
144 [points [1], status], something = cv.cvCalcOpticalFlowPyrLK (
145 prev_grey, grey, prev_pyramid, pyramid,
146 points [0], len (points [0]),
147 (win_size, win_size), 3,
150 cv.cvTermCriteria (cv.CV_TERMCRIT_ITER|cv.CV_TERMCRIT_EPS,
158 for the_point in points [1]:
159 # go trough all the points
161 # increment the counter
165 # we have a point to add, so see if it is close to
166 # another one. If yes, don't use it
167 dx = pt.x - the_point.x
168 dy = pt.y - the_point.y
169 if dx * dx + dy * dy <= 25:
174 if not status [point_counter]:
175 # we will disable this point
178 # this point is a correct point
179 new_points.append (the_point)
181 # draw the current point
183 cv.cvPointFrom32f(the_point),
184 3, cv.cvScalar (0, 255, 0, 0),
187 # set back the points we keep
188 points [1] = new_points
191 # we want to add a point
192 points [1].append (cv.cvPointTo32f (pt))
194 # refine the corner locations
195 points [1][-1] = cv.cvFindCornerSubPix (
198 cv.cvSize (win_size, win_size), cv.cvSize (-1, -1),
199 cv.cvTermCriteria (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS,
202 # we are no more in "add_remove_pt" mode
203 add_remove_pt = False
206 prev_grey, grey = grey, prev_grey
207 prev_pyramid, pyramid = pyramid, prev_pyramid
208 points [0], points [1] = points [1], points [0]
211 # we can now display the image
212 highgui.cvShowImage ('LkDemo', image)
215 c = highgui.cvWaitKey (10)
218 # user has press the ESC key, so exit
221 # processing depending on the character
224 elif c in ['c', 'C']:
226 elif c in ['n', 'N']:
227 night_mode = not night_mode