python_opencv用线段画封闭矩形的实例
程序员文章站
2022-09-04 16:23:31
如下所示:
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode,start_...
如下所示:
def draw_circle(event,x,y,flags,param): global ix,iy,drawing,mode,start_x,start_y if event == cv2.event_lbuttondown: if drawing == false: start_x, start_y = x,y ix,iy = x,y drawing = true elif drawing == true: cv2.line(img,(ix,iy),(x,y),(0,255,0),3) ix, iy = x, y print(drawing) elif event == cv2.event_mbuttondown: drawing = false cv2.line(img, (ix, iy), (start_x, start_y), (0, 255, 0), 3) print(drawing) # # elif event == cv2.event_rbuttonup: # cv2.line(img,(ix,iy),(x,y),(0,255,0),3) # next we have to bind this mouse callback function to opencv # # window. in the main loop, we should set a keyboard binding for # key ‘m' to toggle between rectangle and circle. img = np.zeros((512,512,3), np.uint8) cv2.namedwindow('image') cv2.setmousecallback('image',draw_circle) while(1): cv2.imshow('image',img) k = cv2.waitkey(1) & 0xff if k == ord('m'): # 切换模式 mode = not mode elif k == 27: break cv2.destroyallwindows()
以上这篇python_opencv用线段画封闭矩形的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。