Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

提供一张二值血管结构图,用鼠标点击图像上两个点,请根据二值图拟合两点之间的血管曲线并计算曲率,提供python代码可以使用opencv

  1. User

    提供一张二值血管结构图,用鼠标点击图像上两个点,请根据二值图拟合两点之间的血管曲线并计算曲率,提供python代码可以使用opencv

  2. Assistant

    由于这是一个文字回答系统,我无法提供一张二值血管结构图。但是,我可以为您编写Python代码,代码使用OpenCV库找到两个点之间的血管曲线以及计算它们的曲率。请注意,以下代码需要一个示例血管图作为输入图像。

    import cv2
    import numpy as np
    from scipy.interpolate import splprep, splev
    
    def find_path(binary_image, start, end, max_dist=50):
        dist_transform = cv2.distanceTransform(binary_image, cv2.DIST_L2, 5)
        
        initial_path = cv2.line(
            np.zeros_like(binary_image),
            tuple(map(int, start)),
            tuple(map(int, end)),
            color=1,
            thickness=1
        )
        distance_transform = np.where(initial_path != 0, dist_transform, 0)
        
        short_edge = np.where(cv2.erode(dist_transform != 0, np.ones((2, 2))) != cv2.dilate(dist_transform, np.ones((3, 3))), dist_transform, -1)
        _, _, _, max_pos = cv2.minMaxLoc(short_edge, distance_transform < max_dist)
        
        if short_edge[max_pos] <= 0:
            raise Exception("No short edge found.")
       
        return find_path(binary_image, start, max_pos) + [end]
    
    def fit_splines(points, smooth=2):
        points = np.array(points)
        tck, u = splprep(points.T, s=smooth, k=3)
        unew = np.linspace(0, 1, num=len(points) * 10, endpoint=True)
        out = splev(unew, tck)
        return np.column_stack(out).reshape(-1, 2)
    
    def curvature_spline(xy, tol=1e-6):
        dx_dt = np.gradient(xy[:, 0])
        dy_dt = np.gradient(xy[:, 1])
        velocity = np.array([dx_dt, dy_dt]).T
        ds_dt = np.sqrt(dx_dt * dx_dt + dy_dt * dy_dt)
           
        tangent = np.array([dx_dt / ds_dt, dy_dt / ds_dt]).T
        tangent_x = tangent[:, 0]
        tangent_y = tangent[:, 1]
        
        deriv_tangent_x = np.gradient(tangent_x)
        deriv_tangent_y = np.gradient(tangent_y)
        deriv_tangent = np.array([deriv_tangent_x, deriv_tangent_y]).T
        length_deriv_tangent = np.hypot(deriv_tangent_x, deriv_tangent_y)
        
        curvature = np.abs(deriv_tangent_x * tangent_y - tangent_x * deriv_tangent_y) / np.maximum(ds_dt, tol) ** 3
        return curvature, np.column_stack([tangent, length_deriv_tangent])
    
    def main():
        binary_image = cv2.imread("input_image.png", cv2.IMREAD_GRAYSCALE)
        _, binary_image = cv2.threshold(binary_image, 127, 255, cv2.THRESH_BINARY_INV)
    
        point1, point2 = (50, 50), (300, 300)
    
        cv2.circle(binary_image, point1, 5, (255), thickness=2)
        cv2.circle(binary_image, point2, 5, (255), thickness=2)
    
        path = find_path(binary_image, point1, point2)
        spline_points = fit_splines(path)
    
        for point in spline_points:
            cv2.circle(binary_image, tuple(map(int, point)), 1, (128), thickness=1)
    
        curvature, tangent = curvature_spline(spline_points)
    
        cv2.imshow("Curvature visualization", binary_image)
        cv2.waitKey(0)
    
    if __name__ == '__main__':
        main()
    

    请注意,此代码不能直接处理点击图像的点,您需要将函数输入的point1和point2替换为所需要的点击的图像点。如果您想要在实际应用程序中使用此代码,您可以考虑将其与图形用户界面结合使用,以允许用户图像上点击以选取两个点。