Thresholding whole-slide image

In this process, the algorithm generate a binary image according to the source image and the given threshold. For WSI, the operation must be applied to a small part of the image at a time due to the hardware limitation. With the MMSI library developers can easily perform image thresholding on WSI with access to parts of the high resolution image and perform the image thresholding at such level.

The result of the higher layer of the image is shown in the following figure. Noted that the binary image shown in the figure is automatically down sampled by libMI from the image with full resolution.

The source code of this example can be found at GitLab.

Explanation

# Create a mmsi file using mmsi Writer class.
mslide = mmsi.Writer('threshold.mmsi')

# Set the image dimensions.
mslide.SetImageSize(oslide.dimensions)

# Set the mmsi file tile size.
mslide.SetTileSize((256, 256))

# Set the image bit count (1 for 8-bit image).
mslide.SetPixelSize(1)

# Set number of levels in the pyramid image.
mslide.SetLevelCount(4)

# Open and Close to create the file.
mslide.Open()
mslide.Close()

The mmsi.Writer class is used to generate new MMSI files. Then the essential image parameters are assigned using APIs provided by the library, and at last the Open and Close functions are called to create the file on disk.

# Open the file using mmsi Accessor to operate the file.
mslide = mmsi.Accessor('threshold.mmsi')

# Set the thread number for parallel processing.
mslide.SetThreadNum(4)

# Set compression mode to JPG to accelerate the process.
mslide.SetCompressMode(mmsi.MMSIMODE_JPG)

# For an accessor class, we must open it first to do other actions.
mslide.Open()

# Get the image dimensions.
msize = (mslide.GetImageWidth(), mslide.GetImageHeight())
# Close the file to save the image data.
print('writing image data')
mslide.Close()

The mmsi.Accessor class is used to manipulate existing MMSI files. The Open function must be called first to load the image data to memory, and the Close function must be called at last to save all the modifications. Please note that each MMSI file can only be opened one time until it is closed. Attempt to open the same file again will raise an exception.


# Tile by tile read the data from source image and write to the mmsi file.
# The operating region size is 1024 x 1024.
x = 0
while x < oslide.dimensions[0]:
    y = 0
    while y < oslide.dimensions[1]:

        # Calculate the region size.
        w = min(1024, oslide.dimensions[0] - x)
        h = min(1024, oslide.dimensions[1] - y)

        # Read data from source image and do thresholding.
        tile = numpy.array(oslide.read_region((x, y), 0, (w, h)))
        ret, tile = cv2.threshold(tile, 128, 255, cv2.THRESH_BINARY)
        tile = tile[...,1]

        # Write data to mmsi file level 0.
        mslide.SetCurrentRegion((x, y), (w, h))
        mslide.WriteRegion(0, tile, None)

        print('thresholding %d, %d' % (x, y))

        y = y + 1024
    x = x + 1024

# Reading completed. Now we can close the source file.
oslide.close()

The SetCurrentRegion function is used to indicate the operating region, all parameters are in level 0 reference frame, which has the original image resolution. The image data given in the following WriteRegion function must have the same dimensions as the return value of GetRegionWidthLevel and GetRegionHeightLevel. The pixel size must be equal to the image pixel size, for example, if the image pixel size is 4 and region resolution is 100, 200, then the numpy array can be [100, 200, 4] with dtype = int8, [100, 200, 2] with dtype = int16, or [100, 200] with dtype = int32.

# Get the overview of the generated mmsi image.
mslide.SetCurrentRegion((0, 0), msize)
print('writing result image')

# Read level 3 to get the top-level image.
data = mslide.ReadRegion(3)
cv2.imwrite('threshold.png', data)

The ReadRegion function will return a numpy array with the same resolution of GetRegionWidthLevel and GetRegionHeightLevel, and the dtype depends on image pixel size. It is uint16 when image pixel size is 2, for example.

results matching ""

    No results matching ""