Skip to content

[Question] Why are the edges of my heat map jagged and not covered? #139

@DeepLuckLab

Description

@DeepLuckLab

What's your question?

Thank you very much for your excellent work so far. I am currently encountering an issue: my heatmap does not cover the entire organization slice. The first image below shows the visualization of the cropped patch, and the second image shows the corresponding heatmap. I have attached the implementation code below. I would appreciate your guidance, as I am not sure where the problem lies.

Image Image
  def visualize_heatmap(
          wsi,
          scores: np.ndarray,
          coords: np.ndarray,
          patch_size_level0: int,
          vis_level: Optional[int] = 2,
          cmap: str = 'coolwarm',
          normalize: bool = True,
          blur: bool = False,
          overlap: float = 0.5,
          num_top_patches_to_save: int = -1,
          output_dir: Optional[str] = "output",
  ) -> str:
      """
      Generate a heatmap visualization overlayed on a whole slide image (WSI).
  
      Args:
          wsi: Whole slide image object.
          scores (np.ndarray): Scores associated with each coordinate.
          coords (np.ndarray): Coordinates of patches at level 0.
          patch_size_level0 (int): Patch size at level 0.
          vis_level (Optional[int]): Visualization level.
          cmap (str): Colormap to use for the heatmap.
          normalize (bool): Whether to normalize the scores.
          num_top_patches_to_save (int): Number of high-score patches to save. If set to -1, do not save any. Defaults to -1.
          output_dir (Optional[str]): Directory to save heatmap and top-k patches.
  
      Returns:
          str: Path to the saved heatmap image.
      """
  
      if normalize:
          scores = rankdata(scores, 'average') / len(scores) * 100 / 100
  
      downsample = wsi.level_downsamples[vis_level]
      scale = np.array([1 / downsample, 1 / downsample])
      region_size = tuple((np.array(wsi.level_dimensions[0]) * scale).astype(int))
  
      overlay = create_overlay(scores, coords, patch_size_level0, scale, region_size)
  
      if blur:
          patch_size = np.ceil(np.array([patch_size_level0, patch_size_level0]) * scale).astype(int)
          overlay = cv2.GaussianBlur(overlay, tuple((patch_size * (1 - overlap)).astype(int) * 2 + 1), 0)
  
      img = wsi.read_region((0, 0), vis_level, wsi.level_dimensions[vis_level]).convert("RGB")
      img = img.resize(region_size, resample=Image.Resampling.BICUBIC)
      img = np.array(img)
  
      overlay_colored = apply_colormap(overlay, cmap)
      blended_img = cv2.addWeighted(img, 0.6, overlay_colored, 0.4, 0)
      blended_img = Image.fromarray(blended_img)
  
      os.makedirs(output_dir, exist_ok=True)
      heatmap_path = os.path.join(output_dir, "heatmap.png")
      blended_img.save(heatmap_path)
  
      if num_top_patches_to_save > 0:
          topk_dir = os.path.join(output_dir, "topk_patches")
          os.makedirs(topk_dir, exist_ok=True)
          topk_indices = np.argsort(scores)[-num_top_patches_to_save:]
          for idx, i in enumerate(topk_indices):
              x, y = coords[i]
              patch = wsi.read_region((x, y), 0, (patch_size_level0, patch_size_level0))
              patch.save(os.path.join(topk_dir, f"top_{idx}_score_{scores[i]:.4f}.png"))
      print('Visualize_heatmap Done!')
  
      return heatmap_path

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions