File size: 1,906 Bytes
f3e324b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e6faa2
 
f3e324b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import annotations

# Clear logger to use tiatoolbox.logger
import logging

if logging.getLogger().hasHandlers():
    logging.getLogger().handlers.clear()

from pathlib import Path

import matplotlib as mpl
import matplotlib.pyplot as plt
import requests
import skimage.color

from tiatoolbox import data, logger
from tiatoolbox.tools import stainnorm
from tiatoolbox.wsicore import wsireader
from PIL import Image
import numpy as np

stain_normalization_wsi_examples = [['images/sample_wsi_small.svs']]

def normalize_stain(source_wsi_file, output):
    # create a file handler
    wsi_reader = wsireader.WSIReader.open(input_img=source_wsi_file.name)
    wsi_info = wsi_reader.info.as_dict()
    # we will print out each info line by line
    print(*list(wsi_info.items()), sep="\n")  # noqa: T201
    wsi_thumb = wsi_reader.slide_thumbnail(resolution=1.25, units="power")
    sample = wsi_reader.read_region(
        location=[800, 1600],
        level=0,
        size=[800, 800],  # in X, Y
    )
    target_image = data.stain_norm_target()
    method_name_list = ["Reinhard", "Ruifrok", "Macenko", "Vahadane"]
    plt.subplot(2, 3, 1)
    plt.imshow(sample)
    plt.title("Source Image")
    plt.axis("off")
    plt.subplot(2, 3, 4)
    plt.imshow(target_image)
    plt.title("Target Image")
    plt.axis("off")

    pos = [2, 3, 5, 6]
    for idx, method_name in enumerate(method_name_list):
        stain_normalizer = stainnorm.get_normalizer(method_name)
        stain_normalizer.fit(target_image)

        normed_sample = stain_normalizer.transform(sample.copy())
        plt.subplot(2, 3, pos[idx])
        plt.imshow(normed_sample)
        plt.title(method_name.capitalize())
        plt.axis("off")
    plt.tight_layout()
    # plt.show()
    img = plt.savefig('images/test.png')
    image = Image.open('images/test.png')
    numpy_array = np.array(image)
    return numpy_array