garmentiq.landmark.refinement.refine_landmark_with_blur

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

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.