Batch Transformation

With multiple images of the same size, you can batch transform them

from matplotlib import pyplot as plt
from matplotlib.image import imread
from mpl_toolkits.axes_grid1 import ImageGrid
from skimage.util import view_as_blocks

Read Image and Split them

We split the image into 4 125 x 125 x 3 images

ar = imread("../../../data/image.jpg")[:250, :250]
ars = view_as_blocks(ar, (125, 125, 3)).reshape(-1, 125, 125, 3)
fig = plt.figure()
fig.add_subplot(221)
plt.imshow(ars[0])
fig.add_subplot(222)
plt.imshow(ars[1])
fig.add_subplot(223)
plt.imshow(ars[2])
fig.add_subplot(224)
plt.imshow(ars[3])
<matplotlib.image.AxesImage at 0x225b5154220>
../_images/e5c46ac0b8a74383ccbaefcfc949c27c34ac7fce7da89661449f0c9c56252392.png

Inspect split array shape

Note that the shape expected is B x H x W x C.

  • B: Batch

  • H: Height

  • W: Width

  • C: Channel

ars.shape
(4, 125, 125, 3)

Transform & Plot Results

Each image is separately transformed.

There is slight speedup by doing batch processing, however, it’s minimal and mainly for convenience.

from glcm_cupy import glcm

radius = 2
step_size = 1
gs = glcm(ars, bin_from=256, bin_to=16, radius=radius, step_size=step_size)
# Plot in a grid
fig = plt.figure(figsize=(12, 8))
grid = ImageGrid(fig, 111,
                 nrows_ncols=(3, 3),
                 axes_pad=0.4)

for ax, f_ix, title in zip(grid, range(gs.shape[-1]),
                           ("HOMOGENEITY", "CONTRAST", "ASM", "MEAN",
                            "VARIANCE", "CORRELATION", "DISSIMILARITY"), ):
    ax.imshow(gs[0][..., f_ix] ** (1 / 3))
    ax.set_title(title)
fig.suptitle('Padded GLCM Features for 1st Image (Cube Rooted for visibility)')
GLCM Progress: 100%|██████████| 680k/680k [00:02<00:00, 336k Cells/s] 
Text(0.5, 0.98, 'Padded GLCM Features for 1st Image (Cube Rooted for visibility)')
../_images/d8f40eb49e2f2dde6b520547e532f3aa12d945386690b0c82e39aff6264d0870.png
# Plot in a grid
fig = plt.figure(figsize=(12, 8))
grid = ImageGrid(fig, 111,
                 nrows_ncols=(3, 3),
                 axes_pad=0.4)

for ax, f_ix, title in zip(grid, range(gs.shape[-1]),
                           ("HOMOGENEITY", "CONTRAST", "ASM", "MEAN",
                            "VARIANCE", "CORRELATION", "DISSIMILARITY"), ):
    ax.imshow(gs[1][..., f_ix] ** (1 / 3))
    ax.set_title(title)
fig.suptitle('Padded GLCM Features for 2nd Image (Cube Rooted for visibility)')
Text(0.5, 0.98, 'Padded GLCM Features for 2nd Image (Cube Rooted for visibility)')
../_images/3f094195920c21e92526c5a4a3f054fab6bd6f0260381994639bc8ce42824b07.png

Summary

  • glcm-cupy works with batched images, for convenience and a small speed up!