Paint application with adjustable colors
please help with this example exercise ,Create a Paint application with adjustable colors and brush radius using trackbars. For drawing, refer previous tutorial on mouse handling. i am stuck making trackbar change the color of cv2.drawCirle() fxn
==========================================================================================================
i am trying to add an erase function where a green rectangle acts as an eraser, pressing "m" switches between draw circle and draw rectangle. pressing the left mouse button allows drawing while releasing stops drawing then adjust the track bar for other collors to draw on the window
this is my code:
import cv2
import numpy as np
drawing = True
ix,iy = -1,-1
erase= True
def paint(event,x,y,glags,param):
global ix,iy,drawing,erase,trackbar
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
if erase == False:
cv2.circle(img,(x,y),2,(0,0,255),-1)
def nothing(x):
return()
trackbar = np.zeros((300,512,3), np.uint8)
cv2.namedWindow("trackbar")
cv2.createTrackbar('R','trackbar',0,255,nothing)
cv2.createTrackbar('G','trackbar',0,255,nothing)
cv2.createTrackbar('B','trackbar',0,255,nothing)
else:
cv2.rectangle(img,(x,y),(ix,iy),(0,120,0),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',paint)
while(1):
cv2.imshow('image',img)
r = cv2.getTrackbarPos('R','trackbar')
g = cv2.getTrackbarPos('G','trackbar')
b = cv2.getTrackbarPos('B','trackbar')
k = cv2.waitKey(1) & 0xFF
if k == ord('m'):
erase = not erase
elif k == 27:
break
cv2.destroyAllWindows()
Your code is bad. Really bad.
Try to create the three trackbars right after namedWindow('image'). And get the positions when you need the colors. Or, even better, in a callback function.
@kbarni, all critique goes down much better with a spoon of sugar ;)
thank you for your response, i am new to both python and opencv, what is not clear to me is how to link the mouse events with the trackbar, i can make the same thing with switch cases but the trackbar is more interactive