Update:
It works fine now.
Original:
question from the exercise in the link: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_trackbar/py_trackbar.html#trackbar
I want to create a Paint application with adjustable colors and brush radius using trackbars. But when I run my code for some seconds, my python.exe shut down, it seems that there is a memory access violation but I don't know why.
Thanks for your help!
import cv2
import numpy as np
draw = False
def nothing(x):
pass
# use mouse to draw lines
def draw_line(event,x,y,flags,param):
global draw
if event == cv2.EVENT_LBUTTONDOWN:
draw = True
elif event == cv2.EVENT_MOUSEMOVE:
if draw == True:
cv2.circle(img, (x,y),t,(b,g,r),-1)
elif event == cv2.EVENT_LBUTTONUP:
draw = False
else:
pass
# get current positions of five trackbars
def get_tar():
global r,g,b,s,t
r = cv2.getTrackbarPos('R','image')
g = cv2.getTrackbarPos('G','image')
b = cv2.getTrackbarPos('B','image')
s = cv2.getTrackbarPos(switch,'image')
t = cv2.getTrackbarPos('T','image')
# Create a black image, a window
img = np.zeros((300,512,3),np.uint8)
img[:] = 255
cv2.namedWindow('image')
# create trackbars for color and thickness change
cv2.createTrackbar('R','image',0,255,nothing)
cv2.createTrackbar('G','image',0,255,nothing)
cv2.createTrackbar('B','image',0,255,nothing)
cv2.createTrackbar('T','image',0,20,nothing)
# create switch for ON/OFF and reset functionality
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image',0,1,nothing)
while(1):
cv2.imshow('image',img)
k = cv2.waitKey(100) & 0xFF
if k == 27:
break
# get current positions of five trackbars
get_tar()
if s == 0:
img[:] = 255
draw = False
else:
cv2.setMouseCallback('image', draw_line)
cv2.destroyAllWindows()
and the error is:
Process finished with exit code -1073741819 (0xC0000005)
and then my python.exe shut down
I should put the
cv2.setMouseCallback('image', draw_line)
out of the 'while' loop, or there will be a memory error
Please check the below code this was working for me.
import numpy as np
import cv2 as cv
drawing = False
ix, iy = -1, -1
def nothing():
pass
# Create a black window
img = np.zeros((512, 512, 3), np.uint8)
cv.namedWindow('paint')
# Create the trackbar for change color
cv.createTrackbar('Blue', 'paint', 0, 255, nothing)
cv.createTrackbar('Green', 'paint', 0, 255, nothing)
cv.createTrackbar('Red', 'paint', 0, 255, nothing)
cv.createTrackbar('Brush Size', 'paint', 0, 20, nothing)
# Function Get current position of four trackers
def get_position():
global r, g, b, s
b = cv.getTrackbarPos('Blue', 'paint')
g = cv.getTrackbarPos('Green', 'paint')
r = cv.getTrackbarPos('Red', 'paint')
s = cv.getTrackbarPos('Brush Size', 'paint')
return (b, g, r, s)
# Function to draw as per mouse event
def draw(event, x, y, flags, param):
global drawing, ix, iy
if event == cv.EVENT_LBUTTONDOWN:
drawing = True
x, y = ix, iy
elif event == cv.EVENT_MOUSEMOVE:
drawing = True
cv.circle(img, (x, y), s, (b, g, r), -1)
elif event == cv.EVENT_LBUTTONUP:
drawing = False
cv.circle(img, (x, y), s, (b, g, r), -1)
while (1):
cv.imshow('paint', img)
if cv.waitKey(1) == 27:
break
else:
get_position()
cv.setMouseCallback('paint', draw)
User contributions licensed under CC BY-SA 3.0