-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointmarker.py
40 lines (31 loc) · 1.38 KB
/
pointmarker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import cv2 as cv
from typing import Any
from numpy.typing import ArrayLike, NDArray
class PointMarker:
"""Mark points on an image using Left Mouse Button click."""
def __init__(self, window: str = "Image") -> None:
self._window = window
self._points: list[tuple[int, int]] = list()
def __call__(self, image: NDArray, inplace: bool = False) -> list[tuple[int, int]]:
return self.mark(image, inplace)
@property
def points(self) -> list[tuple[int, int]]:
return self._points
def mark(self, image: NDArray, inplace: bool = False) -> list[tuple[int, int]]:
if not inplace:
image = image.copy()
cv.namedWindow(self._window, cv.WINDOW_NORMAL)
cv.setMouseCallback(self._window, self._record_point, param=image)
while True:
cv.imshow(self._window, image)
if cv.waitKey(1) == ord("q"):
break
cv.destroyAllWindows()
return self._points
def _record_point(self, event: int, x: int, y: int, flags: int, image: Any | None) -> None:
if event == cv.EVENT_LBUTTONDOWN:
self._points.append((x, y))
if image is not None:
self._draw_point(image, (x, y))
def _draw_point(self, image: NDArray, point: tuple[int, int]) -> None:
cv.drawMarker(image, point, (0, 123, 255), cv.MARKER_CROSS, 20, 4, cv.LINE_AA)