Skip to content
Snippets Groups Projects
Commit 3b196d5e authored by fima's avatar fima :beers:
Browse files

extra parameter for image generation

parent 4bca5dfd
No related branches found
No related tags found
1 merge request!89Synthetic image gen
This commit is part of merge request !89. Comments created here will be created in the context of that merge request.
......@@ -2,7 +2,7 @@
from . import doi, internal_tools
from .augmentations import Augmentation
from .data import Dataset, prepare_dataloaders, prepare_datasets
from .img import generate_synthetic_volume, overlay_rgb_images
from .img import generate_volume, overlay_rgb_images
from .models import inference, model_summary, train_model
from .preview import image_preview
from .system import Memory
......@@ -49,7 +49,7 @@ def overlay_rgb_images(background, foreground, alpha=0.5):
return composite.astype("uint8")
def generate_synthetic_volume(shape, dtype=np.float32, order=1):
def generate_volume(final_shape=(128,128,128), scale=0.05, order=1, max_value=255, threshold=0.5, gamma=1.0, dtype="uint8", base_shape=(128,128,128)):
"""Generate a synthetic volume with custom structures.
Args:
......@@ -70,36 +70,49 @@ def generate_synthetic_volume(shape, dtype=np.float32, order=1):
- The shape parameter should be a tuple in the format (x, y, z).
- The dtype parameter should be a valid numpy data type.
"""
if not isinstance(shape, tuple) or len(shape) != 3:
if not isinstance(final_shape, tuple) or len(final_shape) != 3:
raise ValueError("Size must be a tuple")
if not np.issubdtype(dtype, np.number):
raise ValueError("Invalid data type")
# Define the dimensions of the shape for generating Perlin noise
shape_dim = 128
# Initialize the 3D array for the shape
organic_shape = np.empty((shape_dim, shape_dim, shape_dim),dtype=np.float32)
volume = np.empty((base_shape[0], base_shape[1], base_shape[2]),dtype=np.float32)
# Define the scale of the noise
scale = 0.05
# Fill the 3D array with values from the Perlin noise function
for i in range(shape_dim):
for j in range(shape_dim):
for k in range(shape_dim):
for i in range(base_shape[0]):
for j in range(base_shape[1]):
for k in range(base_shape[2]):
# Calculate the distance from the center of the shape
dist = np.sqrt((i-shape_dim/2)**2+(j-shape_dim/2)**2+(k-shape_dim/2)**2) / np.sqrt(3*((shape_dim/2)**2))
dist = np.sqrt((i-base_shape[0]/2)**2+(j-base_shape[1]/2)**2+(k-base_shape[2]/2)**2) / np.sqrt(3*((base_shape[0]/2)**2))
# Generate Perlin noise and adjust the values based on the distance from the center
# This creates a spherical shape with noise
organic_shape[i][j][k] = (1+pnoise3(i * scale,
volume[i][j][k] = (1+pnoise3(i * scale,
j * scale,
k * scale) ) * (1-dist)
# Scale up the volume of organic_shape to size
volume = scipy.ndimage.zoom(organic_shape, [s/shape_dim for s in shape], order=order)
# Normalize
volume = (volume - np.min(volume)) / (np.max(volume) - np.min(volume))
# Gamma correction
volume = np.power(volume, gamma)
# Scale the volume to the maximum value
volume = volume * max_value
# clip the low values of the volume to create a coherent volume
volume[volume < 0.5] = 0
volume[volume < threshold*max_value] = 0
# Clip high values
volume[volume > max_value] = max_value
# Scale up the volume of volume to size
volume = scipy.ndimage.zoom(volume, np.array(final_shape)/np.array(base_shape), order=order)
return volume.astype(dtype)
\ No newline at end of file
......@@ -24,3 +24,4 @@ k3d>=2.16.1
olefile>=0.46
psutil>=5.9.0
structure-tensor>=0.2.1
noise>=1.2.2
\ No newline at end of file
......@@ -62,6 +62,7 @@ setup(
"k3d>=2.16.1",
"olefile>=0.46",
"psutil>=5.9.0",
"structure-tensor>=0.2.1"
"structure-tensor>=0.2.1",
"noise>=1.2.2"
],
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment