import numpy as np
import cv2
from math import sqrt, pow
img = np.zeros((512, 512, 3), np.uint8)
img_ = img.copy()
drawing = False
mode = True
(ix, iy) = (-1, -1)
windowName = 'Drawing'
cv2.namedWindow(windowName)
green = (0, 255, 0)
red = (0, 0, 255)
def draw(event, x, y):
global ix, iy, drawing, mode, img, img_
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
(ix, iy) = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
if mode == True:
img = img_.copy()
cv2.rectangle(img, (ix, iy), (x, y), green, 2)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if mode == True:
cv2.rectangle(img, (ix, iy), (x, y), green, 2)
img_ = img.copy()
cv2.setMouseCallback(windowName, draw)
def main():
global mode
while(True):
cv2.imshow(windowName, img)
key = cv2.waitKey(1)
if key == ord('m') or key == ord('M'):
mode = not mode
elif key == 27:
break
cv2.destroyAllWindows()
if __name__ == '__main__':
main()