Ask Your Question
-1

Paint application with adjustable colors

asked 2016-01-19 07:08:40 -0600

note5 gravatar image

updated 2016-01-19 07:15:07 -0600

berak gravatar image

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()
edit retag flag offensive close merge delete

Comments

1

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 gravatar imagekbarni ( 2016-01-19 08:58:23 -0600 )edit
1

@kbarni, all critique goes down much better with a spoon of sugar ;)

berak gravatar imageberak ( 2016-01-19 11:45:38 -0600 )edit

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

note5 gravatar imagenote5 ( 2016-01-20 10:19:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-01-20 10:41:43 -0600

kbarni gravatar image

As I said, you should rewrite most of your code. It should look like something like this (pseudocode):

Global variables: R,G,B,erase,drawing

#Main application
Create window('image')
Add 3 trackbars to this window: cv2.createTrackbar('R','image',0,255,trackbarCallbackFunc) etc.
Add mouse callback function cv2.setMouseCallback('image',paint)
While 1:
     leave only the keyboard code here
end main

#Trackbar callback: trackbarCallbackFunc
R = cv2.getTrackbarPos('R','trackbar') etc.

#Mouse callback: paint
This function is mostly OK, just delete the imShow and CreateTrackbar part 
and draw the circle with (r,g,b) color and the rectangle with (0,0,0).
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-01-19 07:08:40 -0600

Seen: 841 times

Last updated: Jan 20 '16