Skip to content

How to generate point solutions? #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ImmortalSdm opened this issue Mar 27, 2025 · 3 comments
Closed

How to generate point solutions? #11

ImmortalSdm opened this issue Mar 27, 2025 · 3 comments

Comments

@ImmortalSdm
Copy link

Can u give an example code of generating k largest inscribed circles within the mask as described in the implementation details?
Thx a lot~

@LiuRicky
Copy link
Collaborator

LiuRicky commented Apr 4, 2025

Thanks for your interest.

We consider release relative codes in a few weeks.

@ImmortalSdm
Copy link
Author

Thanks for your interest.

We consider release relative codes in a few weeks.

Glad to hear that! Looking forward to the update.

@LiuRicky
Copy link
Collaborator

LiuRicky commented Apr 9, 2025

Thanks for your interest.
We consider release relative codes in a few weeks.

Glad to hear that! Looking forward to the update.

You can try it.

from scipy.ndimage import distance_transform_edt

def get_two_representative_points(m):
    """
    找到两个能较好描述mask形状的点
    
    Args:
        m: 二值图像数组
    
    Returns:
        tuple: ((x1, y1), (x2, y2)) 两个代表性点的坐标
    """
    y_indices, x_indices = np.where(m == 1)
    if len(x_indices) == 0 or len(y_indices) == 0:
        return None, None
    
    # 计算距离变换
    dist_transform = distance_transform_edt(m)
    
    # 找到第一个点(全局最大值点)
    y1, x1 = np.unravel_index(dist_transform.argmax(), dist_transform.shape)
    
    # 计算mask的重心
    center_y = int(np.mean(y_indices))
    center_x = int(np.mean(x_indices))
    
    # 将点分为两组:距离第一个点较远的点和较近的点
    points = np.column_stack((y_indices, x_indices))
    distances_to_first = ((points[:, 0] - y1) ** 2 + (points[:, 1] - x1) ** 2) ** 0.5
    
    # 找到距离第一个点最远的点集
    far_points = points[distances_to_first > np.median(distances_to_first)]
    
    if len(far_points) > 0:
        # 在远点中找到距离变换值最大的点作为第二个点
        far_dist_values = dist_transform[far_points[:, 0], far_points[:, 1]]
        second_point_idx = np.argmax(far_dist_values)
        y2, x2 = far_points[second_point_idx]
    else:
        # 如果没有合适的远点,使用重心附近的点
        local_region = dist_transform[
            max(0, center_y - 10):min(m.shape[0], center_y + 10),
            max(0, center_x - 10):min(m.shape[1], center_x + 10)
        ]
        local_y, local_x = np.unravel_index(local_region.argmax(), local_region.shape)
        y2 = local_y + max(0, center_y - 10)
        x2 = local_x + max(0, center_x - 10)
    
    # 确保两个点都在mask上
    if m[y1, x1] == 0:
        distances = (x_indices - x1)**2 + (y_indices - y1)**2
        nearest_idx = np.argmin(distances)
        x1, y1 = int(x_indices[nearest_idx]), int(y_indices[nearest_idx])
    
    if m[y2, x2] == 0:
        distances = (x_indices - x2)**2 + (y_indices - y2)**2
        nearest_idx = np.argmin(distances)
        x2, y2 = int(x_indices[nearest_idx]), int(y_indices[nearest_idx])
    
    return [x1, y1], [x2, y2] 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants