Commit 7cf535be authored by Alessia Marcolini's avatar Alessia Marcolini
Browse files

Extract tiles from SVS

parent 5548a6d3
# %%
import gc
import os
import sys
from pathlib import Path
import numpy as np
import pandas as pd
# custom Histology library
from histo_lib import wsi
from inf.config import SVS_DIR
# %%
#PATH = Path('..')
SIZE = 512
SIZE_NO_OVERLAP = 1024
LEVEL = 0
N_TILES_MAX = 100
OUT_DIR = SVS_DIR.parents[0] / f'tiles_level_{0}' # folder in the same parent folder of SVS_DIR
os.makedirs(OUT_DIR, exist_ok=True)
# %%
svs_files = os.listdir(SVS_DIR)
# %%
wrong_svs = []
for index, SVS in enumerate(svs_files): #for each svs file
#sys.stdout.flush()
sys.stdout.write(f'\r Processing svs: {index+1}/{len(svs_files)} : {SVS}')
try:
img = wsi.open_wsi(f'{SVS_DIR}/{SVS}') #open the image
prefix = f'{OUT_DIR}/{SVS}_'
# prefix = f'{OUTDIR}/{SVS}/{SVS}' if you want to create a subfolder for each wsi
# get tissue box
box_tissue = wsi.get_tissue_region(img) # get the tissue box
w = box_tissue[2] - box_tissue[0] #get height of the tissue box
h = box_tissue[3] - box_tissue[1] #get width of the tissue box
#compute the maximum number of tiles that can be extracted
min_size = min(h,w)
max_tiles = (min_size // SIZE_NO_OVERLAP)**2
# generate random tiles
n_tiles = min(N_TILES_MAX, max_tiles)
wsi.wsi2random_tiles(img, SIZE, LEVEL, n_tiles, check_tissue=True, prefix=prefix, max_iter=2*N_TILES_MAX)
#clean workspace
del img
gc.collect()
except Exception as e:
wrong_svs.append(SVS)
print(e)
# %%
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment