To normalize histopathological images, use a normalizer from torchstain.normalizers. The workflow involves:
- Initializing the normalizer with a specific
backend (e.g., 'torch'). - Calling
.fit(target_image) on a target image to learn the stain characteristics. - Calling
.normalize(I=source_image, stains=True) on the source image to perform the transformation.
Note: The input images should be compatible with the chosen backend (e.g., PyTorch tensors).
import torch
from torchvision import transforms
import torchstain
import cv2
target = cv2.cvtColor(cv2.imread("./data/target.png"), cv2.COLOR_BGR2RGB)
to_transform = cv2.cvtColor(cv2.imread("./data/source.png"), cv2.COLOR_BGR2RGB)
T = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x*255)
])
normalizer = torchstain.normalizers.MacenkoNormalizer(backend='torch')
normalizer.fit(T(target))
t_to_transform = T(to_transform)
norm, H, E = normalizer.normalize(I=t_to_transform, stains=True)