import cv2
import numpy as np
class App:
startX = 0
startY = 0
flag_rect = False
def onmouse(self, event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
self.flag_rect = True
self.startX = x
self.startY = y
print('EVENT_LBUTTONDOWN',x,y)
elif event == cv2.EVENT_LBUTTONUP:
cv2.rectangle(self.img,
(self.startX,self.startY),
(x,y),
(0,0,255),
2
)
self.flag_rect = False
pass
elif event == cv2.EVENT_MOUSEMOVE:
if self.flag_rect:
self.img = self.img2.copy()
cv2.rectangle(self.img,
(self.startX,self.startY),
(x,y),
(0,0,255),
2
)
pass
def run(self):
print('run....')
cv2.namedWindow('input')
cv2.setMouseCallback('input',self.onmouse)
self.img = cv2.imread('imgs/lena.png')
self.img2 = self.img.copy()
while(1):
cv2.imshow('input',self.img)
k = cv2.waitKey(100)
if k == 27:
break
App().run()
|