Image segmentation using GNU Octave

With PMA.start, it is very easy to load whole slide images in GNU Octave and run image analysis algorithms.

To work with images, we first need to install the ‘image’ package from forge by issuing:

pkg install -forge image

In order to interact with the web API of PMA.start, we need a JSON parser for Octave. An easy to use one can be found here https://github.com/fangq/jsonlab/releases. Extract it locally and load it in Octave by issuing:

addpath('/absolute/path/to/jsonlab');

Now we are ready to do some image analysis:

% define the path to a WSI. You can download a sample WSI here http://openslide.cs.cmu.edu/download/openslide-testdata/
imagePath='C:/WSI/SVS/CMU-1.svs'

% build the image info URL
infoUrl=['http://localhost:54001/api/json/GetImageInfo?pathOrUid=' imagePath];

% fetch image info as JSON
jsonString=urlread(infoUrl)

% parse it
imInfo=loadjson(jsonString);

% calculate a scale factor that produces an image at most 1500 pixels wide or tall, but don't scale up the image
scale=min(1500 / (max(imInfo.d.Width, imInfo.d.Height)), 1)

% download the image from PMA.start
imgUrl=['http://localhost:54001/region?pathOrUid=' imagePath '&width=' num2str(imInfo.d.Width) '&height=' num2str(imInfo.d.Height) '&x=0&y=0&scale=' num2str(scale) '&timeframe=0&layer=0';];

% read it
wsi=imread(imgUrl);
figure,imshow(wsi); title('WSI');

% convert the image to grayscale
gray=rgb2gray(wsi);
figure,imshow(gray); title('Grayscale');

% blur it - the actual blur factor depends on the type of the image
blur=imsmooth(gray, 'Gaussian', 5);
figure,imshow(blur); title('Blur');

% run Otsu threshold
th=graythresh(blur);

% convert the image to black and white
bw=im2bw(blur, th);
figure,imshow(bw); title('Black - white');

% run edge detection - multiple methods are available
edges=edge(mat2gray(bw), 'Canny');
figure,imshow(edges); title('Edges');

Input image

Result:

 

Click here to download here the complete Octave example.