python-opencv实现简易画图板
"""
Created on Sat May 19 17:34:54 2018
@author: xxx
"""
import cv2 as cv
import numpy as np
def 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, mode
if event == cv.EVENT_LBUTTONDOWN:
drawing = True
ix, 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 )&0xFF
if k == ord('m' ):
mode = not mode
elif k==27 :
break
cv.destroyAllWindow()