Making blobs#

import numpy as np
import scipy as sp
import porespy as ps
import matplotlib.pyplot as plt
ps.visualization.set_mpl_style()
np.random.seed(10)

The blobs function works by generating an image of random noise then applying a Gaussian blur. The creates a correleated field with a Gaussian distribution. The function will then normalize the values back to a uniform distribution, which allow direct thresholding of the image to obtain a binary images (i.e. solid and void).

Let’s start by illustrating the correlated noise field returned by the function. It’s possible to create directional correlation by specifying the blobiness argument as an array with a different value in each direction, as follows:

im1 = ps.generators.blobs(shape=[300, 300], porosity=None, blobiness=[1, 1])
im2 = ps.generators.blobs(shape=[300, 300], porosity=None, blobiness=[1, 2])
fig, ax = plt.subplots(1, 2, figsize=[8, 4])
ax[0].imshow(im1)
ax[1].imshow(im2);
../../../_images/605f9bbe398e471402ba9e91b0ada582f3d279d2e9a434ce9229b58a3e67d7fb.png

The distribution of the values in these images has been renormalized to be a uniform distribution, which can be confirmed by plotting a histogram. Because the image is not very large, the histogram is not perfectly flat:

fig, ax = plt.subplots(figsize=(5, 4))
ax.hist(im1.flatten(), bins=25, edgecolor="k");
../../../_images/1fc3977e37abc66eb7241cb38a2b58fbce3d59e0e3202b938dd61ea9c086404f.png

Next is easy to threshold the noise field to produce a binary image of ‘solid’ and ‘void’:

im = im2 < 0.5
fig, ax = plt.subplots()
ax.imshow(im);
../../../_images/8f9ba8a614573572eb980582c19a0c7a1d716dadcd14ce4fc6e361cc6167e095.png

And we can check the porosity of the thresholded image:

p = im.sum() / im.size
print(f"The porosity is: {p:.3}")
The porosity is: 0.494

Which is reasonably close to the 50% value the threshold was set at.

It’s also possible to receive an already thresholded image from the function directly by specifying the porosity argument:

im = ps.generators.blobs(shape=[300, 300], porosity=0.7)
fig, ax = plt.subplots()
ax.imshow(im);
../../../_images/c9aea3d3514a9b871cb3c3c0e3b1b8be565ec0189bf41603243ccb97eedec764.png
print(f"The porosity is: {im.sum()/im.size:.3}")
The porosity is: 0.707

Finally, it’s possible to change the size of the structure by adjusting the blobiness where larger values means more blobs in the image:

im1 = ps.generators.blobs(shape=[300, 300], porosity=0.5, blobiness=1)
im2 = ps.generators.blobs(shape=[300, 300], porosity=0.5, blobiness=2)
im3 = ps.generators.blobs(shape=[300, 300], porosity=0.5, blobiness=3)
fig, ax = plt.subplots(1, 3, figsize=[10, 4])
ax[0].imshow(im1)
ax[1].imshow(im2)
ax[2].imshow(im3)
ax[0].set_title("Blobiness = 1")
ax[1].set_title("Blobiness = 2")
ax[2].set_title("Blobiness = 3");
../../../_images/161d6b73df3406391ea25aaa9b63734de337261a22c96ab5e91c18f794e46439.png