Fix the mouse bug and the problem of collapse
This commit is contained in:
parent
e3690db4bd
commit
c7481c55f7
@ -46,28 +46,22 @@ class App(object):
|
|||||||
|
|
||||||
self.selection = None
|
self.selection = None
|
||||||
self.drag_start = None
|
self.drag_start = None
|
||||||
self.tracking_state = 0
|
|
||||||
self.show_backproj = False
|
self.show_backproj = False
|
||||||
|
self.track_window = None
|
||||||
|
|
||||||
def onmouse(self, event, x, y, flags, param):
|
def onmouse(self, event, x, y, flags, param):
|
||||||
x, y = np.int16([x, y]) # BUG
|
|
||||||
if event == cv2.EVENT_LBUTTONDOWN:
|
if event == cv2.EVENT_LBUTTONDOWN:
|
||||||
self.drag_start = (x, y)
|
self.drag_start = (x, y)
|
||||||
self.tracking_state = 0
|
self.track_window = None
|
||||||
return
|
|
||||||
if self.drag_start:
|
if self.drag_start:
|
||||||
if flags & cv2.EVENT_FLAG_LBUTTON:
|
xmin = min(x, self.drag_start[0])
|
||||||
h, w = self.frame.shape[:2]
|
ymin = min(y, self.drag_start[1])
|
||||||
xo, yo = self.drag_start
|
xmax = max(x, self.drag_start[0])
|
||||||
x0, y0 = np.maximum(0, np.minimum([xo, yo], [x, y]))
|
ymax = max(y, self.drag_start[1])
|
||||||
x1, y1 = np.minimum([w, h], np.maximum([xo, yo], [x, y]))
|
self.selection = (xmin, ymin, xmax, ymax)
|
||||||
self.selection = None
|
if event == cv2.EVENT_LBUTTONUP:
|
||||||
if x1-x0 > 0 and y1-y0 > 0:
|
|
||||||
self.selection = (x0, y0, x1, y1)
|
|
||||||
else:
|
|
||||||
self.drag_start = None
|
self.drag_start = None
|
||||||
if self.selection is not None:
|
self.track_window = (xmin, ymin, xmax - xmin, ymax - ymin)
|
||||||
self.tracking_state = 1
|
|
||||||
|
|
||||||
def show_hist(self):
|
def show_hist(self):
|
||||||
bin_count = self.hist.shape[0]
|
bin_count = self.hist.shape[0]
|
||||||
@ -88,7 +82,6 @@ class App(object):
|
|||||||
|
|
||||||
if self.selection:
|
if self.selection:
|
||||||
x0, y0, x1, y1 = self.selection
|
x0, y0, x1, y1 = self.selection
|
||||||
self.track_window = (x0, y0, x1-x0, y1-y0)
|
|
||||||
hsv_roi = hsv[y0:y1, x0:x1]
|
hsv_roi = hsv[y0:y1, x0:x1]
|
||||||
mask_roi = mask[y0:y1, x0:x1]
|
mask_roi = mask[y0:y1, x0:x1]
|
||||||
hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
|
hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
|
||||||
@ -100,7 +93,7 @@ class App(object):
|
|||||||
cv2.bitwise_not(vis_roi, vis_roi)
|
cv2.bitwise_not(vis_roi, vis_roi)
|
||||||
vis[mask == 0] = 0
|
vis[mask == 0] = 0
|
||||||
|
|
||||||
if self.tracking_state == 1:
|
if self.track_window and self.track_window[2] > 0 and self.track_window[3] > 0:
|
||||||
self.selection = None
|
self.selection = None
|
||||||
prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
|
prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
|
||||||
prob &= mask
|
prob &= mask
|
||||||
|
Loading…
x
Reference in New Issue
Block a user