RectSelector in common.py

This commit is contained in:
Alexander Mordvintsev 2011-09-19 06:28:39 +00:00
parent 07efb17d12
commit a869838807

View File

@ -115,3 +115,36 @@ def nothing(*arg, **kw):
def clock():
return cv2.getTickCount() / cv2.getTickFrequency()
class RectSelector:
def __init__(self, win, callback):
self.win = win
self.callback = callback
cv2.setMouseCallback(win, self.onmouse)
self.drag_start = None
self.drag_rect = None
def onmouse(self, event, x, y, flags, param):
x, y = np.int16([x, y]) # BUG
if event == cv2.EVENT_LBUTTONDOWN:
self.drag_start = (x, y)
if self.drag_start:
if flags & cv2.EVENT_FLAG_LBUTTON:
#h, w = self.frame.shape[:2]
xo, yo = self.drag_start
x0, y0 = np.minimum([xo, yo], [x, y])
x1, y1 = np.maximum([xo, yo], [x, y])
#x0, y0 = np.maximum(0, np.minimum([xo, yo], [x, y]))
#x1, y1 = np.minimum([w, h], np.maximum([xo, yo], [x, y]))
self.drag_rect = None
if x1-x0 > 0 and y1-y0 > 0:
self.drag_rect = (x0, y0, x1, y1)
else:
if self.drag_rect:
self.callback(self.drag_rect)
self.drag_start = None
self.drag_rect = None
def draw(self, vis):
if not self.drag_rect:
return
x0, y0, x1, y1 = self.drag_rect
cv2.rectangle(vis, (x0, y0), (x1, y1), (0, 255, 0), 2)