def main(args): head, _ = os.path.splitext(os.path.basename(args.in_fn)) levels = ecvl.OpenSlideGetLevels(args.in_fn) # for each level, extract a region with size = size of the last level dims = [0, 0] + levels[-1] # [x, y, w, h] region to read print("Reading %s" % args.in_fn) for i in range(len(levels)): img = ecvl.OpenSlideRead(args.in_fn, i, dims) out_fn = "%s_level_%d.png" % (head, i) print("Writing %s" % out_fn) ecvl.ImWrite(out_fn, img)
def read_slide(slide_fn, level=4): levels = ecvl.OpenSlideGetLevels(slide_fn) dims = [0, 0] + levels[level] img = ecvl.OpenSlideRead(slide_fn, level, dims) t = ecvl.ImageToTensor(img) t_np = t.getdata() s = t_np.shape t_np = t_np.transpose((1, 2, 0)).reshape( (s[1] * s[2], 3)) # Channel last and reshape t_eval = Tensor.fromarray(t_np) print(t_eval.getShape()) return t_eval, s
def get_mask_tissue_from_slide(self, slide_fn, level=2, use_openslide=False): if use_openslide and not no_openslide: # Using open slide slide = openslide.open_slide(slide_fn) dims = slide.level_dimensions ds = [int(i) for i in slide.level_downsamples] x0 = 0 y0 = 0 x1 = dims[0][0] y1 = dims[0][1] delta_x = x1 - x0 delta_y = y1 - y0 pil_img = slide.read_region(location=(x0, y0), level=level, size=(delta_x // ds[level], delta_y // ds[level])) np_img = np.array(pil_img) msk_pred = self.get_tissue_mask(np_img, channel_first=False, BGR=False) else: ## Using ECVL levels = ecvl.OpenSlideGetLevels(slide_fn) dims = [0, 0] + levels[level] img = ecvl.OpenSlideRead(slide_fn, level, dims) t = ecvl.ImageToTensor(img) np_img = t.getdata( ) # Tensor to numpy array (ecvl read images with channel first) msk_pred = self.get_tissue_mask(np_img, channel_first=True, BGR=True) return msk_pred
def main(args): slide_fn = args.slide_fn level = args.level t_dec = td.tissue_detector(model_fn=args.weights_fn, th=args.threshold) mask = t_dec.get_mask_tissue_from_slide(slide_fn, level=level, use_openslide=args.use_openslide) dimensions = ecvl.OpenSlideGetLevels(slide_fn)[0] if args.more_info_output: with open(args.output, 'wb') as f: pickle.dump( { 'mask': mask, 'dimensions': dimensions, 'extraction_level': args.level, 'threshold': args.threshold, 'input': args.slide_fn, 'weights': args.weights_fn, }, f) else: np.save(args.output, mask)