Generating feature stacks#

Machine learning algorithms such as Random Forest Classifiers, e.g. as implemented in APOC use feature stacks for pixel classification. Feature stacks are lists of images of the same size (2D or 3D). APOC brings a utility function for generating feature stacks. It takes an image and a feature specification string as input to produce a feature stack.

Let’s start with loading an example image and making a convenience function for visualization:

from skimage.io import imread, imshow
import numpy as np
import apoc
import matplotlib.pyplot as plt

We start again with the blobs example image.

image = imread('../../data/blobs.tif')
imshow(image)
<matplotlib.image.AxesImage at 0x7f9ba9333580>
../_images/feature_stacks_3_1.png

We furthermore define a utility function for visualizing feature stacks.

def visualize(feature_stack):
    """
    Shows lists of images in rows of three
    """
    if len(feature_stack) == 0:
        return
    
    # show feature images
    width = 3
    height = int(len(feature_stack) / 3)
    if height * width < len(feature_stack):
        height = height + 1    
    
    fig, axes = plt.subplots(height, width, figsize=(10,10))

    for i, f in enumerate(feature_stack):
        if height > 1:
            axes[int(i / 3)][i % 3].imshow(f, cmap=plt.cm.gray)
        else:
            axes[i].imshow(f, cmap=plt.cm.gray)

    w = len(feature_stack) % width
    if w > 0:
        w = width - w
        while (w > 0):
            if height > 1:
                axes[-1][width - w].set_visible(False)
            else:
                axes[width - w].set_visible(False)
            w = w - 1

    plt.show()        

Feature stacks#

A feature stack is a list of 2D or 3D images, typically derived from an original image. If you don’t specify any feature specfication, a stack will be generated with the original image, a blurred version and an edge image:

feature_stack = apoc.generate_feature_stack(image)

visualize(feature_stack)
../_images/feature_stacks_7_0.png

There are also some pre-defined Feature sets:

for f in apoc.PredefinedFeatureSet:
    print(f)
    visualize(apoc.generate_feature_stack(image, f))
PredefinedFeatureSet.custom
PredefinedFeatureSet.small_quick
../_images/feature_stacks_9_1.png
PredefinedFeatureSet.medium_quick
../_images/feature_stacks_9_3.png
PredefinedFeatureSet.large_quick
../_images/feature_stacks_9_5.png
PredefinedFeatureSet.small_dog_log
../_images/feature_stacks_9_7.png
PredefinedFeatureSet.medium_dog_log
../_images/feature_stacks_9_9.png
PredefinedFeatureSet.large_dog_log
../_images/feature_stacks_9_11.png
PredefinedFeatureSet.object_size_1_to_2_px
../_images/feature_stacks_9_13.png
PredefinedFeatureSet.object_size_1_to_5_px
../_images/feature_stacks_9_15.png
PredefinedFeatureSet.object_size_3_to_8_px
../_images/feature_stacks_9_17.png
PredefinedFeatureSet.object_size_5_to_10_px
../_images/feature_stacks_9_19.png
PredefinedFeatureSet.object_size_10_to_15_px
../_images/feature_stacks_9_21.png
PredefinedFeatureSet.object_size_15_to_20_px
../_images/feature_stacks_9_23.png
PredefinedFeatureSet.object_size_20_to_25_px
../_images/feature_stacks_9_25.png
PredefinedFeatureSet.object_size_25_to_50_px
../_images/feature_stacks_9_27.png
PredefinedFeatureSet.object_size_50_to_100_px
../_images/feature_stacks_9_29.png

Custom feature definitions#

You can furthermore, define specific features corresponding to clesperanto filters:

feature_stack = apoc.generate_feature_stack(image, "gaussian_blur=3 mean_box=3 top_hat_box=3")

visualize(feature_stack)
../_images/feature_stacks_11_0.png
feature_stack = apoc.generate_feature_stack(image, "maximum_box=3 difference_of_gaussian=5 sobel=0")

visualize(feature_stack)
../_images/feature_stacks_12_0.png

You can also vary radii / sigmas of the fetures:

feature_stack = apoc.generate_feature_stack(image, "gaussian_blur=1 gaussian_blur=5 gaussian_blur=10 gaussian_blur=20")

visualize(feature_stack)
../_images/feature_stacks_14_0.png

Furthermore, some features can be combined, for example you can compute the sobel of a Gaussian or a Laplacian of a Gaussian

feature_stack = apoc.generate_feature_stack(image, "sobel_of_gaussian_blur=1 sobel_of_gaussian_blur=5 sobel_of_gaussian_blur=10")

visualize(feature_stack)
../_images/feature_stacks_16_0.png
feature_stack = apoc.generate_feature_stack(image, "laplace_box_of_gaussian_blur=1 laplace_box_of_gaussian_blur=5 laplace_box_of_gaussian_blur=10")

visualize(feature_stack)
../_images/feature_stacks_17_0.png