Basics of Cupy#

Cupy is a library for processing data on CUDA-compatible NVidia graphics cards.

See also

In case you run this notebook from Google Coolab, switch to a GPU-runtime and uncomment this line:

# !pip install stackview ipycanvas==0.11
import numpy as np
import cupy as cp
import cupyx.scipy.ndimage as cdi

from skimage.io import imread
import stackview
image = imread("../../data/blobs.tif")

# in case you run this notebook on Google colab, use this line instead:
# image = imread('https://github.com/BiAPoL/PoL-BioImage-Analysis-TS-GPU-Accelerated-Image-Analysis/raw/main/data/blobs.tif')

When working with cupy it is important to convert numpy arrays to cupy arrays, e.g. using the cupy.asarray() function.

cp_image = cp.asarray(image)

Cupy arrays work pretty much the same as numpy arrays. For example, we can apply a threshold using the > operator.

cp_binary = cp_image > 128

We can also apply connected component labeling to separate objects.

cp_labels, _ = cdi.label(cp_binary)

type(cp_labels)
cupy.ndarray
stackview.insight(cp_labels)
shape(254, 256)
dtypeint32
size254.0 kB
min0
max63

Exercise#

Determine the maximum intensity in the label image. This should correspond to the number of labels in the image.

Subsample the cp_image using the [::] syntax.