Code to import .npdi files and their annotations (in .ndpa files). The annotations in these files are typically saved in nanometers which cannot be converted to pixels without the use of Openslide. Using Openslide, a low resolution image can be exported and the pixel-based annotations scaled accordingly.
import sys
from flamingo.Flamingo import HE
import sys
from src.flamingo.Flamingo import HE
Using file test3-FITC 2 (485).ndpi. Please download the file and place in the example/ folder where the test annotations .ndpa file is stored.
Providing a resolution on initiation will automatically generate a low resolution image with one dimension equal to resolution and the other proportionally smaller. Annotations will be scaled to this new low res image size.
# File path and name
fp1 = 'example/'
fn1 = 'test3-FITC 2 (485).ndpi'
# Load file and scale automatically to 2000 pixels
ex1 = HE(fp1,fn1,resolution=2000)
pointer: this annotation type is not yet supported. pin: this annotation type is not yet supported. linearmeasure: this annotation type is not yet supported.
Annotations are plotted as an image mask adjacent to the low-res image. Each annoation is uniquely coloured, and overlapping annotations are considered void.
ex1.annotationPlot(type='combined')
ex1.annotationPlot(type='anno')
The annotations coordinates (pixel basis) and the low-res image can be exported. Additionally, the image mask can also be exported.
type='json' : annotation labels and coordinatestype='image': low resolution imagetype='mask' : masked image of size matching low res thumbnailtype='all' : all of the aboveex1.export(type='json',path='example/results/')
Using file test3-DAPI 2 (387) .ndpi. Please download the file and place in the example/ folder where the test annotations .ndpa file is stored.
resolution is not specified, with the various methods worked through in turn.
fp2 = 'example/'
fn2 = 'test3-DAPI 2 (387) .ndpi'
ex2 = HE(fp2,fn2)
# Determine image properties: offsets from centre and the microns per pixel
ex2.imageProperties()
# Import the .ndpa file and parse annotations
ex2.annotationImport()
# Openslide's get_thumbail method
ex2.imageLoRes(resolution=1750)
# Convert annotations into an image mask matching the low res image size
ex2.annotationMask()
ex2.annotationPlot(type='combined')
ex2.annotationPlot(type='anno')
ex2.export(type='json',path='example/results/')