garmentiq.landmark.refinement.refine_landmark_with_blur
Snapping a landmark onto a blurred mask boundary.
1"""Snapping a landmark onto a blurred mask boundary.""" 2import cv2 3 4 5def refine_landmark_with_blur(x, y, blurred_mask, window_size=5): 6 """ 7 Refine the landmark location using a Gaussian-blurred version of the segmentation mask. 8 The function searches a small window around (x, y) in the blurred mask and returns 9 the coordinates of the pixel with the maximum value, which is assumed to be a more 10 reliable location for the landmark. 11 12 Args: 13 x (float): The initial x-coordinate of the landmark. 14 y (float): The initial y-coordinate of the landmark. 15 blurred_mask (np.ndarray): The Gaussian-blurred segmentation mask. 16 window_size (int, optional): The size of the square window (in pixels) around (x, y) 17 to search for the maximum value. Defaults to 5. 18 19 Returns: 20 tuple: A tuple (refined_x, refined_y) representing the refined coordinates. 21 """ 22 height, width = blurred_mask.shape 23 x_min = int(max(0, x - window_size)) 24 x_max = int(min(width, x + window_size + 1)) 25 y_min = int(max(0, y - window_size)) 26 y_max = int(min(height, y + window_size + 1)) 27 28 # Extract the local region from the blurred mask. 29 local_region = blurred_mask[y_min:y_max, x_min:x_max] 30 # Find the location of the maximum value within the local region. 31 _, max_val, _, max_loc = cv2.minMaxLoc(local_region) 32 33 # Compute refined coordinates relative to the entire image. 34 refined_x = x_min + max_loc[0] 35 refined_y = y_min + max_loc[1] 36 return refined_x, refined_y
def
refine_landmark_with_blur(x, y, blurred_mask, window_size=5):
6def refine_landmark_with_blur(x, y, blurred_mask, window_size=5): 7 """ 8 Refine the landmark location using a Gaussian-blurred version of the segmentation mask. 9 The function searches a small window around (x, y) in the blurred mask and returns 10 the coordinates of the pixel with the maximum value, which is assumed to be a more 11 reliable location for the landmark. 12 13 Args: 14 x (float): The initial x-coordinate of the landmark. 15 y (float): The initial y-coordinate of the landmark. 16 blurred_mask (np.ndarray): The Gaussian-blurred segmentation mask. 17 window_size (int, optional): The size of the square window (in pixels) around (x, y) 18 to search for the maximum value. Defaults to 5. 19 20 Returns: 21 tuple: A tuple (refined_x, refined_y) representing the refined coordinates. 22 """ 23 height, width = blurred_mask.shape 24 x_min = int(max(0, x - window_size)) 25 x_max = int(min(width, x + window_size + 1)) 26 y_min = int(max(0, y - window_size)) 27 y_max = int(min(height, y + window_size + 1)) 28 29 # Extract the local region from the blurred mask. 30 local_region = blurred_mask[y_min:y_max, x_min:x_max] 31 # Find the location of the maximum value within the local region. 32 _, max_val, _, max_loc = cv2.minMaxLoc(local_region) 33 34 # Compute refined coordinates relative to the entire image. 35 refined_x = x_min + max_loc[0] 36 refined_y = y_min + max_loc[1] 37 return refined_x, refined_y
Refine the landmark location using a Gaussian-blurred version of the segmentation mask. The function searches a small window around (x, y) in the blurred mask and returns the coordinates of the pixel with the maximum value, which is assumed to be a more reliable location for the landmark.
Arguments:
- x (float): The initial x-coordinate of the landmark.
- y (float): The initial y-coordinate of the landmark.
- blurred_mask (np.ndarray): The Gaussian-blurred segmentation mask.
- window_size (int, optional): The size of the square window (in pixels) around (x, y) to search for the maximum value. Defaults to 5.
Returns:
tuple: A tuple (refined_x, refined_y) representing the refined coordinates.