Main Content

Create Labeled Blocked Image from ROIs and Masks

This example shows how to create a labeled blocked image from a set of ROIs.

In this example, you use two approaches to obtain and display labeled data. One approach uses polygonal ROI objects that store the coordinates of the boundaries of tumor and normal tissue regions. The polyToBlockedImage function converts the ROI coordinates into a labeled blocked image. The second approach uses a mask to indicate a binary segmentation of the image into tissue and background. This example combines the information in the ROI and mask images to create a single blocked image with numeric pixel labels corresponding to tumor, normal tissue, and background regions.

Create a blocked image using a modified version of a training image of a lymph node containing tumor tissue, from the CAMELYON16 data set. The modified image has three coarse resolution levels. The spatial referencing has been adjusted to enforce a consistent aspect ratio and to register features at each level.

bim = blockedImage("tumor_091R.tif");

Load Label Data

The CAMELYON16 data set provides labels for tumor and normal regions as a set of coordinates specifying the manually annotated region boundaries with respect to the finest resolution level. When pixels exist within the boundaries of both a normal region and a tumor region, the correct label for those pixels is normal tissue.

Load label data for the blocked image. This example uses a modified version of the labels of the tumor_091.tif image from the CAMELYON16 data set. The original labels are stored in XML format. The modified labels have been resampled and saved as a MAT file.

roiPoints = load("labelledROIs.mat")
roiPoints = struct with fields:
    nonCancerRegions: {[46×2 double]}
       cancerRegions: {6×1 cell}

Represent Labels as ROI Objects

Create polygonal ROI objects that store the coordinates of the tumor boundaries and normal tissue boundaries.

tumorPolys = cellfun(@(position) images.roi.Polygon(Position=position, ...
normalPolys = cellfun(@(position) images.roi.Polygon(Position=position, ...

Display ROIs with Labels on Original Data

Display the image overlaid with the polygonal ROIs. The ROIs have the same coordinate system as the image, so changing the resolution level of the displayed image still renders the ROIs accurately.

h = bigimageshow(bim);
title("Resolution Level: " + num2str(h.ResolutionLevel));

Zoom in on one ROI. The tumor region boundary is shown in red, and surrounds an internal region of normal tissue shown in green.

xlim([3940 4290])
ylim([2680 3010])
title("Resolution Level: " + num2str(h.ResolutionLevel));

Create Labeled Blocked Image of ROI Data

Use the polyToBlockedImage function to create a labeled blocked image of the ROI coordinate data. The polyToBlockedImage function requires ROI coordinates, ROI labels, and the size of the output blocked image as inputs.

Obtain the xy-coordinate data for the normal regions and the tumor regions, and combine them into a single roiPositions cell array.

normalRegions = roiPoints.nonCancerRegions;
tumorRegions = roiPoints.cancerRegions; 
roiPositions = [normalRegions; tumorRegions];

Find the number of normal and tumor regions, and assign the label 1 to normal tissue and the label 2 to tumor tissue. Assign labels in the same order specified by roiPositions. Specify labels as uint8 values to reduce the memory required for storage.

numNormalRegions = numel(normalRegions);
numTumorRegions = numel(tumorRegions);
roiLabelIDs = [ones(numNormalRegions,1,"uint8"); 2*ones(numTumorRegions,1,"uint8")];

Select the desired resolution level for the new blocked image. This choice is a tradeoff between efficiency and accuracy. Using a coarser resolution level decreases processing time and storage size. Using a finer resolution level increases the level of detail preserved in the mask. Coarse resolution levels can be used for regular ROIs like polygons. For small, freehand ROIs, a fine resolution level can be more appropriate. For this example, use an intermediate resolution level.

maskLevel = 2;

Specify the image size for the new blocked image to match that of the original image, bim, at the desired resolution level.

imageSize = bim.Size(maskLevel,1:2);

Create a labeled blocked image. Maintain the spatial referencing of the original blocked image, bim, at the desired resolution level. By default, pixels that do not fall inside any ROI are assigned the numeric label 0.

bROILabels = polyToBlockedImage(roiPositions,roiLabelIDs,imageSize, ...
    BlockSize=bim.BlockSize(maskLevel,1:2), ...

Display Overlay of ROI Labels and Original Data

Display the labeled blocked image overlaid on the original image. The tumor regions are shown in red, and the normal tissue region completely enclosed in a tumor region is shown in green. The background and the normal tissue connected to the background are shown in blue, indicating that the connected normal regions are incorrectly classified as background.

hbim = bigimageshow(bim);
showlabels(hbim,bROILabels,Colormap=[0 0 1; 0 1 0; 1 0 0]);

Zoom in on the normal tissue region displayed in green. Visually verify that the ROI boundaries are represented with sufficient detail.

xlim([3940 4290])
ylim([2680 3010])

Separate Tissue and Background Using Mask

Use image segmentation to create a mask that correctly distinguishes the normal tissue from the background. Since thresholding requires reading the underlying image data into memory and the background and foreground regions are sufficiently large, the coarsest resolution level is appropriate. The mask is 1 (true) for pixels whose grayscale value is less than 130. Fill small holes in the mask by performing morphological closing using the bwmorph function.

btissueMask = apply(bim,@(bs)bwmorph(im2gray(bs.Data)<130,"close"),Level=3);

Create Single Labeled Blocked Image

Combine the tissue mask and ROI label data into a final labeled blocked image. Although the ROI image has been created at a finer resolution level than the tissue mask, they can be processed together because they are derived from the same image and have the same world extents. Use the combineLabels helper function, which is defined at the end of this example.

bLabels = apply(bROILabels,@combineLabels,ExtraImages=btissueMask);

Display the labeled blocked image overlaid on the original image. The three labels (normal, tumor, and background) appear in green, red, and blue respectively.

hbim = bigimageshow(bim);
showlabels(hbim,bLabels,Colormap=[0 0 1; 0 1 0; 1 0 0]);
title("Background (Blue), Normal Tissue (Green), Tumor (Red)")

Zoom in on a region of interest to examine in closer detail.

xlim([3940 4290])
ylim([2680 3010])

Supporting Function

The combineLabels helper function combines an ROI-based label image and mask-based label image into a single label image.

function blabel = combineLabels(bs,btissueMask)
    % The tissue mask block is smaller in size
    btissueMask = imresize(btissueMask,size(bs.Data));
    % Convert to labels, 0 is background, 1 is foreground.
    blabel = uint8(btissueMask);
    % Label tumor regions with 2
    blabel(bs.Data==2) = 2;

See Also

| |

Related Topics