opencv画图练习:目前只做到矩形之间会有覆盖,矩形只能越画越大,颜色,宽度可调
import cv2 as cv
import numpy as np
img = np.zeros((512,512,3),np.uint8)
cv.namedWindow('image')
def nothing(x):
pass
cv.createTrackbar('R','image',0,255,nothing)
cv.createTrackbar('G','image',0,255,nothing)
cv.createTrackbar('B','image',0,255,nothing)
cv.createTrackbar('width','image',1,50,nothing)
drawing = False
mode = True
ix,iy=-1,-1
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode,width,b,g,r
if event == cv.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv.EVENT_MOUSEMOVE:
if drawing:
cv.rectangle(img,(ix,iy),(x,y),(b,g,r),-1)
cv.rectangle(img,(ix+width,iy+width),(x-width,y-width),(0,0,0),-1)#画出未填充矩形
#else:
#cv.circle(img,(x,y),5,(0,0,255),-1)#追踪鼠标移动生成点
elif event == cv.EVENT_LBUTTONUP:
drawing = False
if mode:
cv.rectangle(img,(ix,iy),(x,y),(b,g,r),1)
else:
cv.circle(img,(x,y),5,(b,g,r),-1)
cv.setMouseCallback('image', draw_circle)
while 1:
cv.imshow('image',img)
k = cv.waitKey(1) & 0xFF
if k == 27:
break
r = cv.getTrackbarPos('R', 'image')
g = cv.getTrackbarPos('G', 'image')
b = cv.getTrackbarPos('B', 'image')
width = cv.getTrackbarPos('width', 'image')
cv.destroyAllWindows()