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 import imread
import stackview
image = imread("../../data/blobs.tif")

# in case you run this notebook on Google colab, use this line instead:
# image = imread('')

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)

shape(254, 256)
size254.0 kB


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.