Пример #1
0
def create_tilesummary(wsiPath,
                       is_wsi,
                       tilesFolderPath,
                       img_pil: PIL.Image.Image,
                       img_pil_filtered: PIL.Image.Image,
                       wsi_original_width: int,
                       wsi_original_height: int,
                       wsi_scaled_width: int,
                       wsi_scaled_height: int,
                       tile_height: int,
                       tile_width: int,
                       scale_factor: int,
                       tile_score_thresh: float,
                       tile_scoring_function,
                       tile_naming_func,
                       level: int,
                       best_level_for_downsample: int = 0) -> TileSummary:
    """
  
    Args:

    """
    np_img = util.pil_to_np_rgb(img_pil)
    np_img_filtered = util.pil_to_np_rgb(img_pil_filtered)

    tile_sum = score_tiles(np_img, np_img_filtered, wsiPath, is_wsi,
                           tilesFolderPath, tile_height, tile_width,
                           scale_factor, wsi_original_width,
                           wsi_original_height, wsi_scaled_width,
                           wsi_scaled_height, tile_score_thresh,
                           tile_scoring_function, tile_naming_func, level,
                           best_level_for_downsample)

    return tile_sum
Пример #2
0
def show_wsi_with_marked_tiles(figsize: Tuple[int] = (10, 10),
                               scale_factor: int = 32,
                               tilesummary: TileSummary = None,
                               wsi_path: pathlib.Path = None,
                               df_tiles: pandas.DataFrame = None,
                               level: int = 0):
    """
    Either provide a TileSummary object or wsi_path, df_tiles and level.
    
    Loads a whole slide image, scales it down, converts it into a numpy array and shows it with a grid overlay for all tiles
    that passed scoring to visualize which tiles e.g. "tiles.WsiOrROIToTilesMultithreaded" calculated as worthy to keep.
    Arguments:
        figsize: Size of the plotted matplotlib figure containing the image.
        scale_factor: The larger, the faster this method works, but the plotted image has less resolution.
        tilesummary: a TileSummary object of one wsi
        wsi_path: Path to a whole-slide image
        df_tiles: A pandas dataframe from e.g. "tiles.WsiOrROIToTilesMultithreaded" with spacial information about all tiles           
        level: The level that was specified in e.g. "tiles.WsiOrROIToTilesMultithreaded". 0 means highest magnification.
    """
    if tilesummary != None:
        wsi_pil, large_w, large_h, new_w, new_h, best_level_for_downsample = wsi_to_scaled_pil_image(
            tilesummary.wsi_path,
            scale_factor=tilesummary.scale_factor,
            level=tilesummary.level)
        wsi_np = util.pil_to_np_rgb(wsi_pil)
        boxes = []
        for tile in tilesummary.top_tiles():
            box = np.array([
                tile.get_x(),
                tile.get_y(),
                tile.get_width(),
                tile.get_height()
            ]) / scale_factor
            boxes.append(box)
        show_np_with_bboxes(wsi_np, boxes, figsize)

    else:
        wsi_pil, large_w, large_h, new_w, new_h, best_level_for_downsample = wsi_to_scaled_pil_image(
            wsi_path, scale_factor=scale_factor, level=level)
        wsi_np = util.pil_to_np_rgb(wsi_pil)
        boxes = []
        for index, row in df_tiles.iterrows():
            if row['wsi_path'] == wsi_path:
                box = np.array([
                    row['x_upper_left'], row['y_upper_left'],
                    row['pixels_width'], row['pixels_height']
                ]) / scale_factor
                boxes.append(box)

        show_np_with_bboxes(wsi_np, boxes, figsize)
def tile_to_np_tile(tile):
    """
  Convert tile information into the corresponding tile as a NumPy image read from the whole-slide image file.

  Args:
    tile: Tile object.

  Return:
    Tile as a NumPy image.
  """
    pil_img = tile_to_pil_tile(tile)
    np_img = util.pil_to_np_rgb(pil_img)
    return np_img
def add_tile_stats_to_top_tile_summary(pil_img, tiles, z):
    np_sum = util.pil_to_np_rgb(pil_img)
    sum_r, sum_c, sum_ch = np_sum.shape
    np_stats = np_tile_stat_img(tiles)
    st_r, st_c, _ = np_stats.shape
    combo_c = sum_c + st_c
    combo_r = max(sum_r, st_r + z)
    combo = np.zeros([combo_r, combo_c, sum_ch], dtype=np.uint8)
    combo.fill(255)
    combo[0:sum_r, 0:sum_c] = np_sum
    combo[z:st_r + z, sum_c:sum_c + st_c] = np_stats
    result = util.np_to_pil(combo)
    return result
def open_image_np(filename):
  """
  Open an image (*.jpg, *.png, etc) as an RGB NumPy array.

  Args:
    filename: Name of the image file.

  returns:
    A NumPy representing an RGB image.
  """
  pil_img = open_image(filename)
  np_img = util.pil_to_np_rgb(pil_img)
  return np_img
Пример #6
0
def slide_to_scaled_np_image(slide_number):
    """
  Convert a WSI training slide to a scaled-down NumPy image.

  Args:
    slide_number: The slide number.

  Returns:
    Tuple consisting of scaled-down NumPy image, original width, original height, new width, and new height.
  """
    pil_img, large_w, large_h, new_w, new_h = slide_to_scaled_pil_image(
        slide_number)
    np_img = util.pil_to_np_rgb(pil_img)
    return np_img, large_w, large_h, new_w, new_h
Пример #7
0
def tile_to_np_tile(tile, is_wsi: bool):
    """
  Convert tile information into the corresponding tile as a NumPy image read from the whole-slide image file.

  Args:
    tile: Tile object.
    is_wsi: if true, a WSI format like .ndpi is assumed, if false, a format like png is assumed (ROI)

  Return:
    Tile as a NumPy image.
  """
    pil_img = tile_to_pil_tile(tile, is_wsi)
    np_img = util.pil_to_np_rgb(pil_img)
    return np_img
def np_text(text,
            w_border=TILE_TEXT_W_BORDER,
            h_border=TILE_TEXT_H_BORDER,
            font_size=TILE_TEXT_SIZE,
            text_color=TILE_TEXT_COLOR,
            background=TILE_TEXT_BACKGROUND_COLOR):
    """
  Obtain a NumPy array image representation of text.

  Args:
    text: The text to convert to an image.
    w_border: Tile text width border (left and right).
    h_border: Tile text height border (top and bottom).
    font_size: Size of font.
    text_color: Tile text color.
    background: Tile background color.

  Returns:
    NumPy array representing the text.
  """
    pil_img = pil_text(text, w_border, h_border, font_size, text_color,
                       background)
    np_img = util.pil_to_np_rgb(pil_img)
    return np_img