Skip to content

Commit

Permalink
fix: mypy complains about types
Browse files Browse the repository at this point in the history
  • Loading branch information
dilawar committed Jul 14, 2024
1 parent 7f37d72 commit 890a914
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 242 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PYTHON := $(shell which python)
POETRY := $(PYTHON) -m poetry
POETRY := poetry

all : test

Expand Down
17 changes: 11 additions & 6 deletions plotdigitizer/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
__email__ = "[email protected]"

from pathlib import Path
import typing as T
import cv2 as cv
import numpy as np
import numpy.typing as npt

import tempfile

Expand Down Expand Up @@ -31,18 +33,21 @@ def heal(orig):


def remove_grid(
orig, num_iter=3, background_color: int = 255, grid_size: int = 2
orig: npt.ArrayLike,
num_iter: int = 3,
background_color: T.Sequence[int] = (255, 255, 255),
grid_size: int = 2,
) -> np.ndarray:
img = orig.copy()
img = np.array(orig, copy=True)
thres = cv.threshold(img, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1]
# Remove horizontal lines
horizontal_kernel = cv.getStructuringElement(cv.MORPH_RECT, (40, 1))
remove_horizontal = cv.morphologyEx(
thres, cv.MORPH_OPEN, horizontal_kernel, iterations=num_iter
)
cnts = cv.findContours(remove_horizontal, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
contours = cnts[0] if len(cnts) == 2 else cnts[1]
for c in contours:
cv.drawContours(img, [c], -1, background_color, grid_size)

# Remove vertical lines
Expand All @@ -51,8 +56,8 @@ def remove_grid(
thres, cv.MORPH_OPEN, vertical_kernel, iterations=num_iter
)
cnts = cv.findContours(remove_vertical, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
contours = cnts[0] if len(cnts) == 2 else cnts[1]
for c in contours:
cv.drawContours(img, [c], -1, background_color, grid_size)
return img

Expand Down
8 changes: 5 additions & 3 deletions plotdigitizer/plotdigitizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
locations_: T.List[geometry.Point] = []
points_: T.List[geometry.Point] = []

img_: np.ndarray = np.zeros((1, 1))
img_: npt.NDArray[np.float64] = np.zeros((1, 1))


def cache() -> Path:
Expand All @@ -52,7 +52,7 @@ def save_img_in_cache(
if filename is None:
filename = Path(f"{data_to_hash(img)}.png")
outpath = cache() / filename
cv.imwrite(str(outpath), img)
cv.imwrite(str(outpath), np.array(img))
logging.debug(f" Saved to {outpath}")


Expand All @@ -66,7 +66,9 @@ def plot_traj(traj, outfile: Path):

for p in locations_:
csize = img_.shape[0] // 40
cv.circle(img_, (p.x, img_.shape[0] - p.y), csize, 128, -1)
cv.circle(
img_, (int(p.x), int(img_.shape[0] - p.y)), int(csize), (128, 128, 128), -1
)

plt.imshow(img_, interpolation="none", cmap="gray")
plt.axis(False)
Expand Down
Loading

0 comments on commit 890a914

Please sign in to comment.