Skip to content

Commit

Permalink
feat: finished slate detector
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrutchf committed Sep 15, 2024
1 parent 66f4f39 commit cdc3f85
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
17 changes: 17 additions & 0 deletions pyfishsensedev/image/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy as np
import pymupdf

from pyfishsensedev.library.constants import INCH_TO_M


class Pdf:
DPI = 300
Expand All @@ -18,3 +20,18 @@ def __init__(self, file_name: Path) -> None:
@property
def image(self) -> np.ndarray:
return self._image

@property
def width(self) -> int:
_, width = self._image.shape

return width

@property
def height(self) -> int:
height, _ = self._image.shape

return height

def get_points_body_space(self, points: np.ndarray) -> np.ndarray:
return (points / float(Pdf.DPI)) * INCH_TO_M
1 change: 1 addition & 0 deletions pyfishsensedev/library/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PIXEL_PITCH_MM = 1.5 * 1e-3
CHECKERBOARD_SQUARE_SIZE_MM = 41.3
MM_TO_M = 1e-3
INCH_TO_M = 0.0254
4 changes: 2 additions & 2 deletions pyfishsensedev/plane_detector/checkerboard_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(
self._columns = columns
self._square_size = square_size

def _get_points_image_space(self):
def _get_points_image_space(self) -> np.ndarray:
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
gray = cv2.cvtColor(cv2.UMat(self.image), cv2.COLOR_RGB2GRAY)

Expand All @@ -34,7 +34,7 @@ def _get_points_image_space(self):
else:
return None

def _get_points_body_space(self):
def _get_points_body_space(self) -> np.ndarray:
# coordinates of squares in the checkerboard world space
objp = np.zeros((self._rows * self._columns, 3), np.float32)
objp[:, :2] = np.mgrid[0 : self._rows, 0 : self._columns].T.reshape(-1, 2)
Expand Down
28 changes: 16 additions & 12 deletions pyfishsensedev/plane_detector/slate_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def __init__(self, image: np.ndarray[np.uint8], pdf: Pdf) -> None:
self._pdf = pdf
self._tensor_template = numpy_image_to_torch(self._pdf.image)
self._ran_template_matches = False
self._feats0_matches = None
self._feats1_matches = None
self._template_matches = None
self._image_matches = None

def _get_template_matches(self) -> Tuple[torch.Tensor, torch.Tensor]:
if not self._ran_template_matches:
Expand All @@ -35,33 +35,37 @@ def _get_template_matches(self) -> Tuple[torch.Tensor, torch.Tensor]:
},
)

feats0_matches, feats1_matches = image_matcher(
template_matches, image_matches = image_matcher(
numpy_image_to_torch(self.image)
)

num_keypoints, _ = feats0_matches.shape
num_keypoints, _ = template_matches.shape
if num_keypoints > max_num_keypoints:
max_num_keypoints = num_keypoints

self._feats0_matches = feats0_matches
self._feats1_matches = feats1_matches
self._template_matches = template_matches
self._image_matches = image_matches

self._ran_template_matches = True

return self._feats0_matches, self._feats1_matches
return self._template_matches, self._image_matches

def _get_points_image_space(self):
pass
_, image_matches = self._get_template_matches()

return image_matches.cpu().numpy()

def _get_points_body_space(self):
pass
template_matches, _ = self._get_template_matches()

return self._pdf.get_points_body_space(template_matches.cpu().numpy())

def is_valid(self):
feats0_matches, _ = self._get_template_matches()
template_matches, _ = self._get_template_matches()

if feats0_matches is None:
if template_matches is None:
return None

num_matches, _ = feats0_matches.shape
num_matches, _ = template_matches.shape

return num_matches > 10

0 comments on commit cdc3f85

Please sign in to comment.