画点
from PIL import Image from pylab import * import cv2 import matplotlib.pyplot as plt im = array(Image.open('E:/1.jpg')) # im.astype(int) plt.imshow(im) print ('Please click 3 points') click = plt.ginput(3, timeout = -1) plt.close() print ('you clicked:',click) x = [] y = [] for a, b in click: a = int(a) b = int(b) cv2.circle(im, (a, b), 2, (0, 0, 255), -1) cv2.imshow('name', im) cv2.waitKey() cv2.destroyAllWindows()
画框
''' EVENT_FLAG_ALTKEY = 32 摁住Alt EVENT_FLAG_CTRLKEY = 8 摁住Ctrl EVENT_FLAG_LBUTTON = 1 摁住左键 EVENT_FLAG_MBUTTON = 4 摁住中键 EVENT_FLAG_RBUTTON = 2 摁住右键 EVENT_FLAG_SHIFTKEY = 16 摁住Shift EVENT_LBUTTONDBLCLK = 7 左键双击 EVENT_LBUTTONDOWN = 1 左键击下 EVENT_LBUTTONUP = 4 左键弹起 EVENT_MBUTTONDBLCLK = 9 中键双击 EVENT_MBUTTONDOWN = 3 中键击下 EVENT_MBUTTONUP = 6 中键弹起 EVENT_MOUSEHWHEEL = 11 滚动条向左,flags>0。向右,flags<0 EVENT_MOUSEMOVE = 0 鼠标移动 EVENT_MOUSEWHEEL = 10 滚动条向上,flags>0。向下,flags<0 EVENT_RBUTTONDBLCLK = 8 中键双击 EVENT_RBUTTONDOWN = 2 中键击下 EVENT_RBUTTONUP = 5 中键弹起 '''
import numpy as np import cv2 drawing = False #鼠标按下为真 mode = True #如果为真,画矩形,按m切换为曲线 ix,iy=-1,-1 def draw_circle(event,x,y,flags,param): global ix,iy,drawing,mode if event == cv2.EVENT_LBUTTONDOWN: drawing = True ix,iy=x,y cv2.circle(img, (ix, iy), 2, (0, 255, 0), -1) # 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), 2) print((ix, iy), (x, y)) else: cv2.circle(img,(x,y),5,(0,0,255),-1) img = cv2.imread('E:/1.jpg') 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()