def get_points(event,x,y,flags,param):
global lpnts,rpnts
if event == cv2.EVENT_LBUTTONDOWN:
lpnts = np.append(lpnts, np.array([[x, y]]), axis=0)
cv2.polylines(img, [lpnts], False, (0, 0, 255))
if event == cv2.EVENT_RBUTTONDOWN:
rpnts = np.append(rpnts, np.array([[x, y]]), axis=0)
cv2.polylines(img, [rpnts], False, (255, 0, 0))
if rpnts.size>2:
check(lpnts, rpnts[-1], rpnts[-2])
#check if the new point crosses a line
python类EVENT_LBUTTONDOWN的实例源码
def get_points(event, x, y, flags, param):
global lpnts, mode, counter, which_intersect
if event == cv2.EVENT_LBUTTONDOWN:
lpnts = np.append(lpnts, np.array([[x, y]]), axis=0)
cv2.polylines(img, [lpnts], False, (0, 0, 255))
if lpnts.size > 2:
if mode == 0:
#check(l1, lpnts[-1], lpnts[-2])
if check(l1, lpnts[-1], lpnts[-2]):
which_intersect = 0
mode = 1
#check(l2, lpnts[-1], lpnts[-2])
if check(l2, lpnts[-1], lpnts[-2]):
which_intersect = 1
mode = 1
elif mode == 1:
counter += 1
if check(lines[(which_intersect + 1) % 2], lpnts[-1], lpnts[-2]):
mode = 3
print counter
# check if the new point crosses a line
def run(im):
im_disp = im.copy()
window_name = "Draw line here."
cv2.namedWindow(window_name,cv2.WINDOW_AUTOSIZE)
cv2.moveWindow(window_name, 910, 0)
print " Drag across the screen to set lines.\n Do it twice"
print " After drawing the lines press 'r' to resume\n"
l1 = np.empty((2, 2), np.uint32)
l2 = np.empty((2, 2), np.uint32)
list = [l1,l2]
mouse_down = False
def callback(event, x, y, flags, param):
global trigger, mouse_down
if trigger<2:
if event == cv2.EVENT_LBUTTONDOWN:
mouse_down = True
list[trigger][0] = (x, y)
if event == cv2.EVENT_LBUTTONUP and mouse_down:
mouse_down = False
list[trigger][1] = (x,y)
cv2.line(im_disp, (list[trigger][0][0], list[trigger][0][1]),
(list[trigger][1][0], list[trigger][1][1]), (255, 0, 0), 2)
trigger += 1
else:
pass
cv2.setMouseCallback(window_name, callback)
while True:
cv2.imshow(window_name,im_disp)
key = cv2.waitKey(10) & 0xFF
if key == ord('r'):
# Press key `q` to quit the program
return list
exit()
def mouseInteraction(self, event, x, y, flags, params):
if self.userInteraction is True:
if event == cv2.EVENT_LBUTTONDOWN:
self.refPt = [(x, y)]
self.workingFrame[y, x] = [0, 0, 255]
self.showFrame(self.selectionWindow, self.workingFrame)
elif event == cv2.EVENT_LBUTTONUP:
self.undoFrames.append(self.workingFrame.copy())
self.refPt.append((x, y))
if self.refPt[0][0] != self.refPt[1][0] and self.refPt[0][1] != self.refPt[1][1]:
area = trackedArea(self.refPt)
area.setStackSize(30)
area.setTemplate(self.processedFrame)
# area.initKalman()
corn = area.getCorners()
self.trackedAreasList.append(area)
cv2.rectangle(self.workingFrame,
corn[0], corn[1],
(0, 0, 255), 1)
self.showFrame(self.selectionWindow, self.workingFrame)
def brush_circle(event, x, y, flags, param):
global ix, iy, drawing, mode, r,g,b,radius
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True # start to draw when L button down
ix, iy = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True and mode == True:
cv2.circle(img, (x,y), radius, (b, g, r), -1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False # end drawing when L button up
if mode == True:
cv2.circle(img, (x,y), radius, (b, g, r), -1)
# Create a black image, a window
def on_mouse(event, x, y, flags, params):
# global img
t = time()
if event == cv2.EVENT_LBUTTONDOWN:
print 'Start Mouse Position: '+str(x)+', '+str(y)
sbox = [x, y]
boxes.append(sbox)
# print count
# print sbox
elif event == cv2.EVENT_LBUTTONUP:
print 'End Mouse Position: '+str(x)+', '+str(y)
ebox = [x, y]
boxes.append(ebox)
print boxes
crop = img[boxes[-2][1]:boxes[-1][1],boxes[-2][0]:boxes[-1][0]]
cv2.imshow('crop',crop)
k = cv2.waitKey(0)
if ord('r')== k:
cv2.imwrite('Crop'+str(t)+'.jpg',crop)
print "Written to file"
def on_mouse(event, x, y, flags, params):
global boxes;
global selection_in_progress;
current_mouse_position[0] = x;
current_mouse_position[1] = y;
if event == cv2.EVENT_LBUTTONDOWN:
boxes = [];
# print 'Start Mouse Position: '+str(x)+', '+str(y)
sbox = [x, y];
selection_in_progress = True;
boxes.append(sbox);
elif event == cv2.EVENT_LBUTTONUP:
# print 'End Mouse Position: '+str(x)+', '+str(y)
ebox = [x, y];
selection_in_progress = False;
boxes.append(ebox);
#####################################################################
# controls
def on_mouse(event, x, y, flags, params):
global boxes;
global selection_in_progress;
current_mouse_position[0] = x;
current_mouse_position[1] = y;
if event == cv2.EVENT_LBUTTONDOWN:
boxes = [];
# print 'Start Mouse Position: '+str(x)+', '+str(y)
sbox = [x, y];
selection_in_progress = True;
boxes.append(sbox);
elif event == cv2.EVENT_LBUTTONUP:
# print 'End Mouse Position: '+str(x)+', '+str(y)
ebox = [x, y];
selection_in_progress = False;
boxes.append(ebox);
#####################################################################
# return centre of a set of points representing a rectangle
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
fengjing_HSV_edit_with_inpaint_Background.py 文件源码
项目:Opencv_learning
作者: wjb711
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def draw_circle(event,x,y,flags,param):
#if event == cv2.EVENT_LBUTTONDBLCLK:
if event == cv2.EVENT_LBUTTONDOWN:
print ('mouse x and y is ')
print (x,y)
px = im1[y,x]
print ('RGB Value:')
print px
px_hsv = cv2.cvtColor(im1, cv2.COLOR_BGR2HSV)
H=px_hsv.item(y,x,0)
S=px_hsv.item(y,x,1)
V=px_hsv.item(y,x,2)
print ('HSV Value: H?S??V')
print (H,S,V)
def draw_circle(event,x,y,flags,param):
global drawing,drawing1
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
if event == cv2.EVENT_RBUTTONDOWN:
drawing1 = True
if event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
cv2.circle(img,(x,y),5,(0,0,255),-1)
if drawing1 == True:
cv2.circle(img,(x,y),5,(0,255,0),-1)
if event == cv2.EVENT_LBUTTONUP:
drawing = False
if event == cv2.EVENT_RBUTTONUP:
drawing1 = False
#print (drawing)
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
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:
xo, yo = self.drag_start
x0, y0 = np.minimum([xo, yo], [x, y])
x1, y1 = 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:
rect = self.drag_rect
self.drag_start = None
self.drag_rect = None
if rect:
self.callback(rect)
interaction_updated_global.py 文件源码
项目:Interactive-object-tracking
作者: abhishekarya286
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def click_and_crop(event, x, y, flags, param):
# grab references to the global variables
global refPt, cropping
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
refPt.append((x, y))
cropping = False
# draw a rectangle around the region of interest
cv2.rectangle(img_copy, refPt[0], refPt[1], (0, 255, 0), 2)
cv2.imshow("image", img_copy)
cv2.waitKey(0)
lab_global_optimisation.py 文件源码
项目:Interactive-object-tracking
作者: abhishekarya286
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def click_and_crop(event, x, y, flags, param):
# grab references to the global variables
global refPt, cropping
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
refPt.append((x, y))
cropping = False
# draw a rectangle around the region of interest
cv2.rectangle(img_copy, refPt[0], refPt[1], (0, 255, 0), 2)
cv2.imshow("image", img_copy)
cv2.waitKey(0)
def on_mouse(self, event, x, y, flags, param):
pt = (x, y)
if event == cv2.EVENT_LBUTTONDOWN:
self.prev_pt = pt
elif event == cv2.EVENT_LBUTTONUP:
self.prev_pt = None
if self.prev_pt and flags & cv2.EVENT_FLAG_LBUTTON:
for dst, color in zip(self.dests, self.colors_func()):
cv2.line(dst, self.prev_pt, pt, color, 5)
self.dirty = True
self.prev_pt = pt
self.show()
# palette data from matplotlib/_cm.py
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:
xo, yo = self.drag_start
x0, y0 = np.minimum([xo, yo], [x, y])
x1, y1 = 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:
rect = self.drag_rect
self.drag_start = None
self.drag_rect = None
if rect:
self.callback(rect)
def annotate_mouse_callback(event, x, y, flags, annotator):
winName = annotator.winName
# If adding a bounding box:
if annotator.editing:
# Set top left corner of rectangle:
if event == cv2.EVENT_LBUTTONDOWN:
annotator.rect_tl = (x, y)
# Set bottom right corner of rectangle:
elif event == cv2.EVENT_RBUTTONDOWN:
annotator.rect_br = (x, y)
# If deleting a bounding box:
elif annotator.deleting:
if event == cv2.EVENT_LBUTTONDOWN:
print 'Delete at {}'.format((x, y))
annotator.delete_rectangles_at_point((x, y))
annotator.deleting = False
annotator.update_display()
image_to_world.py 文件源码
项目:Kinect-ASUS-Xtion-Pro-Live-Calibration-Tutorials
作者: taochenshh
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def draw_rect(self,event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
self.drawing = True
self.rect_done = False
self.ix1 = x
self.iy1 = y
elif event == cv2.EVENT_MOUSEMOVE:
if self.drawing == True:
self.ix2 = x
self.iy2 = y
elif event == cv2.EVENT_LBUTTONUP:
self.drawing = False
self.ix2 = x
self.iy2 = y
cv2.rectangle(self.rgb_image,(self.ix1,self.iy1),(self.ix2,self.iy2),(0,255,0),2)
center_point = self.get_center_point()
cv2.circle(self.rgb_image, tuple(center_point.astype(int)), 3, (0,0,255),-1)
cv2.imshow('RGB Image', self.rgb_image)
cv2.waitKey(5)
self.rect_done = True
def imageMouseCallback(self, event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
x, y = self.calibrator.findImageStar(x, y)
correspondence = self.findCorrespondence(x, y)
if correspondence is None:
if self.calibrator.setCorrespondencePos(self.selected_star, (x, y)):
self.selected_star = self.calibrator.findEmptyPos()
else:
self.selected_star = self.calibrator.addCorrespondence((x, y), None)
else:
self.selected_star = correspondence
self.render()
elif event == cv2.EVENT_RBUTTONDOWN:
self.deleteSelectedStar()
def skyMouseCallback(self, event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
res = self.renderer.findStar(x, y, self.circle_radius)
if res is None:
return
altaz = (res[0], res[1])
correspondence = self.calibrator.findAltAzCorrespondence(altaz)
if correspondence is None:
if self.calibrator.setCorrespondenceAltaz(self.selected_star, altaz):
self.selected_star = self.calibrator.findEmptyAltAz()
else:
self.selected_star = self.calibrator.addCorrespondence(None, altaz)
else:
self.selected_star = correspondence
self.render()
elif event == cv2.EVENT_RBUTTONDOWN:
self.deleteSelectedStar()
def _mouse_ops(self, event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
self._drawing = True
self._pt0 = (x, y)
elif event == cv2.EVENT_LBUTTONUP:
self._drawing = False
self._pt1 = (x, y)
self._bboxes.append((self._cur_label, (self._pt0, self._pt1)))
elif event == cv2.EVENT_MOUSEMOVE:
self._pt1 = (x, y)
elif event == cv2.EVENT_RBUTTONUP:
if self._bboxes:
self._bboxes.pop()
def draw_rect(event,x,y,flags,param):
global x1,y1,x2,y2,drawing,img,imgSmall,finishDraw
# ??????????????
if event==cv2.EVENT_LBUTTONDOWN:
drawing=True
x1,y1=x,y
# ???????????????? event ??????? flag ??????
elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:
if drawing==True:
img[:,:]=newGray[:,:]
img[y1:y,x1:x]=imgSmall[y1:y,x1:x]
cv2.rectangle(img,(x1,y1),(x,y),(0,0,255),1)
# ??????????
elif event==cv2.EVENT_LBUTTONUP:
drawing==False
x2,y2=x,y
finishDraw = True
def on_mouse(self, event, x, y, flags, param):
pt = (x, y)
if event == cv2.EVENT_LBUTTONDOWN:
self.prev_pt = pt
elif event == cv2.EVENT_LBUTTONUP:
self.prev_pt = None
if self.prev_pt and flags & cv2.EVENT_FLAG_LBUTTON:
for dst, color in zip(self.dests, self.colors_func()):
cv2.line(dst, self.prev_pt, pt, color, 5)
self.dirty = True
self.prev_pt = pt
self.show()
# palette data from matplotlib/_cm.py
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)
return
if self.drag_start:
if flags & cv2.EVENT_FLAG_LBUTTON:
xo, yo = self.drag_start
x0, y0 = np.minimum([xo, yo], [x, y])
x1, y1 = 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:
rect = self.drag_rect
self.drag_start = None
self.drag_rect = None
if rect:
self.callback(rect)
def click_and_crop(event, x, y, flags, param):
# grab references to the global variables
global refPt, cropping
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
refPt.append((x, y))
cropping = False
# draw a rectangle around the region of interest
cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
cv2.imshow("image", image)
def on_mouse(self, event, x, y, flags, param):
pt = (x, y)
if event == cv2.EVENT_LBUTTONDOWN:
self.prev_pt = pt
elif event == cv2.EVENT_LBUTTONUP:
self.prev_pt = None
if self.prev_pt and flags & cv2.EVENT_FLAG_LBUTTON:
for dst, color in zip(self.dests, self.colors_func()):
cv2.line(dst, self.prev_pt, pt, color, 5)
self.dirty = True
self.prev_pt = pt
self.show()
# palette data from matplotlib/_cm.py
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:
xo, yo = self.drag_start
x0, y0 = np.minimum([xo, yo], [x, y])
x1, y1 = 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:
rect = self.drag_rect
self.drag_start = None
self.drag_rect = None
if rect:
self.callback(rect)
def annotate(event, x, y, flags, param):
"""Callback for function 'annotate_tracks'.
Tracks cursor and detects if mouse position is to be saved as
a trackpoint. Track points are saved once per frame if the
left mouse button is held down.
"""
global is_read
global px, py
if event == cv2.EVENT_MOUSEMOVE:
px, py = x, y
if event == cv2.EVENT_LBUTTONDOWN:
is_read = 1
if event == cv2.EVENT_LBUTTONUP:
is_read = 0
def click_and_crop(event, x, y, flags, param):
# grab references to the global variables
global refPt
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
print refPt