Skip to content

Commit

Permalink
refactor: small refactor fot detect function
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioReyesSan committed Jan 21, 2025
1 parent a2462e0 commit 6dca0a4
Showing 1 changed file with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ def get_roi(corners, frame_shape, padding=local_padding):
x_min, y_min = max(0, x_min), max(0, y_min)
x_max, y_max = min(frame_shape[1], x_max), min(frame_shape[0], y_max)
return (x_min, y_min, x_max, y_max)

def calculate_new_size(w,h):
# Find the resized dimensions
ratio = float(w) / float(h)

if w > h:
resized_w = int(resized_max_resolution)
resized_h = int(resized_max_resolution / ratio)
else:
resized_w = int(resized_max_resolution * ratio)
resized_h = int(resized_max_resolution)

return resized_w, resized_h

def do_corner_sub_pix(corners):
dist_matrix = np.linalg.norm(
corners.reshape(-1, 1, 2) - corners.reshape(1, -1, 2), axis=-1
)
np.fill_diagonal(dist_matrix, np.inf)
min_distance = dist_matrix.min()
radius = max(
1, int(np.ceil(min_distance * 0.5))
) # ensuring radius has a value of at least 1

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

try:
corners = cv2.cornerSubPix(grayscale, corners, (radius, radius), (-1, -1), criteria)
except Exception as e:
logging.error(e)

return corners

h, w = img.shape[0:2]
grayscale = to_grayscale(img)
Expand Down Expand Up @@ -103,18 +135,10 @@ def get_roi(corners, frame_shape, padding=local_padding):
self.detection_results_signal.emit(img, None, stamp)
return
else:
# Find the resized dimensions
ratio = float(w) / float(h)

if w > h:
resized_w = int(resized_max_resolution)
resized_h = int(resized_max_resolution / ratio)
else:
resized_w = int(resized_max_resolution * ratio)
resized_h = int(resized_max_resolution)
resized_w, resized_h = calculate_new_size(w,h)

# Resize
resized = cv2.resize(img, (resized_w, resized_h), interpolation=cv2.INTER_NEAREST)
resized = cv2.resize(grayscale, (resized_w, resized_h), interpolation=cv2.INTER_NEAREST)

# Run the detector on the resized image
(ok, resized_corners) = cv2.findChessboardCorners(resized, (cols, rows), flags=flags)
Expand All @@ -129,10 +153,7 @@ def get_roi(corners, frame_shape, padding=local_padding):
)

# Estimate the ROI in the original image
roi_min_j = int(max(0, corners[:, 0, 1].min() - 10))
roi_min_i = int(max(0, corners[:, 0, 0].min() - 10))
roi_max_j = int(min(w, corners[:, 0, 1].max() + 10))
roi_max_i = int(min(w, corners[:, 0, 0].max() + 10))
roi_min_i, roi_min_j, roi_max_i, roi_max_j = get_roi(corners, img.shape[:2])

# Extract the ROI of the original image
roi = grayscale[roi_min_j:roi_max_j, roi_min_i:roi_max_i]
Expand All @@ -148,21 +169,7 @@ def get_roi(corners, frame_shape, padding=local_padding):
corners = roi_corners + np.array([roi_min_i, roi_min_j], dtype=np.float32)

if sub_pixel_refinement:
dist_matrix = np.linalg.norm(
corners.reshape(-1, 1, 2) - corners.reshape(1, -1, 2), axis=-1
)
np.fill_diagonal(dist_matrix, np.inf)
min_distance = dist_matrix.min()
radius = max(
1, int(np.ceil(min_distance * 0.5))
) # ensuring radius has a value of at least 1

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

try:
corners = cv2.cornerSubPix(grayscale, corners, (radius, radius), (-1, -1), criteria)
except Exception as e:
logging.error(e)
corners = do_corner_sub_pix(corners=corners)

image_points = corners.reshape((rows, cols, 2))
x_array = cell_size * (np.array(range(cols)) - 0.5 * cols)
Expand Down

0 comments on commit 6dca0a4

Please sign in to comment.