키보드 입력받기
import numpy as np
import cv2
width, height = 512, 512
x, y, R = 256, 256, 50
direction = 0
while True:
key = cv2.waitKey(30)
if key == 0x1B:
break;
elif key == 0x270000:
direction = 0
elif key == 0x280000:
direction = 1
elif key == 0x250000:
direction = 2
elif key == 0x260000:
direction = 3
if direction == 0:
x += 10
elif direction == 1:
y += 10
elif direction == 2:
x -= 10
else:
y -= 10
if x < R:
x = R
direction = 0
if x > width - R:
x = width - R
direction = 2
if y < R:
y = R
direction = 1
if y > height - R:
y = height - R
direction = 3
img = np.zeros((width, height,3), np.uint8) + 255
cv2.circle(img, (x, y), R, (0, 0, 255), -1)
cv2.imshow('img', img)
cv2.destroyAllWindows()
마우스 입력받기
import numpy as np
import cv2
def onMouse(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
if flags & cv2.EVENT_FLAG_SHIFTKEY:
cv2.rectangle(param[0], (x-5, y-5), (x+5, y+5), (255, 0, 0))
else:
cv2.circle(param[0], (x, y), 5, (255, 0, 0), 3)
elif event == cv2.EVENT_RBUTTONDOWN:
cv2.circle(param[0], (x, y), 5, (0, 0, 255), 3)
elif event == cv2.EVENT_LBUTTONDBLCLK:
param[0] = np.zeros(param[0].shape, np.uint8) + 255
cv2.imshow("img", param[0])
img = np.zeros((512,512,3), np.uint8) + 255
cv2.imshow('img', img)
cv2.setMouseCallback('img', onMouse, [img])
cv2.waitKey()
cv2.destroyAllWindows()