[Python] Drawing polygons on large microscopic image with scikit-image

Discussão em 'Python' iniciado por Stack, Junho 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to draw polygons on the microscopic images to highlight the important areas. I will skip the part how the coordinates of the vertices are obtained, because this is not important here.

    To draw polygons on the image I use a generated list of dictionaries with information about coronal section index cortical, layer number (1, 2, 3, 4, 5, 6), cortical area id value, and coordinates for given layer (so a set of vertices that correspond to particular layer with shape (Y, SLICE, X)).

    Here is how the list of dictionaries looks like: example_image

    The problem is, when I am trying to run the function responsible for drawing polygons, the process is taking very much time (above 2 hours). I understand that this is due to large size (~20000x30000 pixels), but is there any way to make this faster using skimage?

    I can't use OpenCV or Pillow.

    layer_colors = {
    1: (255, 0, 0), # Red
    2: (0, 255, 0), # Green
    3: (0, 0, 255), # Blue
    4: (255, 255, 0), # Yellow
    5: (0, 255, 255), # Cyan
    6: (255, 0, 255) # Magenta
    }

    def draw_full_res(image, coords, image_slice_idx, thickness=5, layer_colors=layer_colors):

    disk_kernel = ski.morphology.disk(thickness)
    mask = np.zeros(image.shape[:2], dtype=bool)

    for idx in range(len(coords)):
    if coords[idx]['slice'] == image_slice_idx:
    layer = coords[_idx]['layer']
    color = layer_colors.get(layer)

    print('Progress layer: ', layer, '|', color)

    for contours in coords[idx]['coords']:
    # Draw only those polygons where number of vertices is greater than 5
    if len(contours) > 5:
    # Because shape for coords is (Y, SLICE, X)
    y_coords = contours[:, 0].astype(int)
    x_coords = contours[:, 2].astype(int)

    # Create polygon
    rr, cc = ski.draw.polygon_perimeter(x_coords, y_coords, shape=image.shape[:2])

    mask[rr, cc] = True
    # Make polygon thicker
    mask = ski.morphology.dilation(mask, disk_kernel)

    image[mask] = color

    output_path = f'{image_slice_idx}_slice.png'
    ski.io.imsave(output_path, image)

    Continue reading...

Compartilhe esta Página