Код: Выделить всё
import datetime
import cv2
import numpy
clicked = False
path = "files/file1.png"
coordinates = {'x':[],'y':[]}
def onMouse(event, x, y, flags, param):
global image
global clicked
if event == cv2.EVENT_LBUTTONUP:
coordinates['x'].append(x)
coordinates['y'].append(y)
clicked = True
if (len(coordinates['x'])> 1 ):
print("debug drawing line")
cv2.line(image, (coordinates['x'][-2],coordinates['y'][-2]), (coordinates['x'][-1],coordinates['y'][-1]),(255,0,0),1)
print('debug drawing point')
cv2.circle(image,(coordinates['x'][-1],coordinates['y'][-1]), 3,(255,0,255),3)
def saveImage():
xmin, xmax = min(coordinates['x']),max(coordinates['x'])
ymin, ymax = min(coordinates['y']),max(coordinates['y'])
nombre = datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d-%H-%M-%S')
cv2.imwrite(f'files/Result-{nombre}.png',image[ymin:ymax,xmin:xmax])
image = cv2.imread(path)
cv2.namedWindow("Display", cv2.WINDOW_AUTOSIZE)
cv2.setMouseCallback('Display', onMouse)
active = True
while active:
cv2.imshow('Display', image)
key = cv2.waitKey(1)
if key == ord('q'):
cv2.destroyAllWindows()
active = False
if key == ord('s'):
saveImage()
cv2.destroyAllWindows()
active = False
Я пытаюсь добавить кодовый блок для сброса изображения вот так
Код: Выделить всё
if key == ord('r'):
resetImage()
Код: Выделить всё
def resetImage():
print('Debug: reset Image')
coordinates['x'].clear()
coordinates['y'].clear()
# image = cv2.imread(path)
# cv2.destroyAllWindows()
Есть подсказки?
Заранее спасибо.
Подробнее здесь: https://stackoverflow.com/questions/790 ... e-with-cv2