Transformation with Missing Values

You may use an Image with NaN as missing values.

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.image import imread
from mpl_toolkits.axes_grid1 import ImageGrid

Read Image and cast type

Note that NaN values can only be assigned to float like arrays.

ar = imread("../../../data/image.jpg")[:1000:4, :1000:4]
plt.imshow(ar)
ar = ar.astype(np.float64)
../_images/b73bd63db379ce14fbfe47deb413fea52a622ea1bc4fd24705ade13fcc6c84f9.png

Create artificial hole, missing data

ar[100:-100, 100:-100] = np.nan
plt.imshow(ar / 256)
plt.title("Image with NaN Hole")
plt.show()
../_images/837c337102c73dd0b7846c80d03f0618604da04d4675e7ddffef0b0f5aa8bd3f.png

Transform & Plot Transformation

Notice how GLCM works around the hole.

  • For GLCM Windows that are totally NaNs, the result is a NaN.

  • For GLCM Windows that partially contain NaNs, the result is the GLCM of the non-NaN values

from glcm_cupy import glcm

radius = 2
step_size = 1
ar_g = glcm(ar, 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(ar_g.shape[-1]),
                           ("HOMOGENEITY", "CONTRAST", "ASM", "MEAN",
                            "VARIANCE", "CORRELATION", "DISSIMILARITY"), ):
    ax.imshow(ar_g[..., f_ix] ** (1 / 3))
    ax.set_title(title)
fig.suptitle('Padded GLCM Features (Cube Rooted for visibility)')
GLCM Progress: 100%|██████████| 714k/714k [00:02<00:00, 335k Cells/s] 
Text(0.5, 0.98, 'Padded GLCM Features (Cube Rooted for visibility)')
../_images/3f3d304285d4396f8a3970ba1406dccbb39977e3ec67bce570500631a0defb65.png

Summary

  • glcm-cupy can work around NaN values

See Also

As we can use NaNs, we can pad the image to avoid reducing the height and width!

See the padding example if you haven’t