python-opencv实现简易画图板
"""
Created on Sat May 19 17:34:54 2018@author: xxx
"""import cv2 as cv
import numpy as npdef nothing(x):pass
drawing = False
mode = True
ix, iy = -1, -1
def draw_circle(event, x, y, flags, param):r = cv.getTrackbarPos('R', 'image')g = cv.getTrackbarPos('G', 'image')b = cv.getTrackbarPos('B', 'image')color = (b, g, r)global ix, iy, drawing, modeif event == cv.EVENT_LBUTTONDOWN:drawing = Trueix, iy = x, y
elif event == cv.EVENT_MOUSEMOVE and flags == cv.EVENT_FLAG_LBUTTON:if drawing == True:if mode == True:cv.rectangle(img, (ix, iy), (x, y), color, -1)else:cv.circle(img, (ix, iy), 3, color, -1)
elif event == cv.EVENT_LBUTTONUP:drawing == False
img = np.zeros((512, 512, 3), np.uint8)
cv.namedWindow('image')cv.createTrackbar('R', 'image', 0, 255, nothing)
cv.createTrackbar('G', 'image', 0, 255, nothing)
cv.createTrackbar('B', 'image', 0, 255, nothing)
cv.setMouseCallback('image', draw_circle)while(1):cv.imshow('image', img)k = cv.waitKey(1)&0xFFif k == ord('m'):mode = not modeelif k==27:breakcv.destroyAllWindow()