Skip to content
Snippets Groups Projects
Commit 421e1c6b authored by s204159's avatar s204159 :sunglasses: Committed by fima
Browse files

K3d vol resize

parent a1aaba88
No related branches found
No related tags found
1 merge request!82K3d vol resize
""" Provides a collection of internal utility functions."""
import socket
import getpass
import hashlib
import outputformat as ouf
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import os
import shutil
import requests
import getpass
from PIL import Image
import socket
from pathlib import Path
from qim3d.io.logger import log
from fastapi import FastAPI
import gradio as gr
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import outputformat as ouf
import requests
from fastapi import FastAPI
from PIL import Image
from scipy.ndimage import zoom
from uvicorn import run
from qim3d.io.logger import log
def mock_plot():
"""Creates a mock plot of a sine wave.
......@@ -306,6 +309,29 @@ def get_css():
return css_content
def downscale_img(img, max_voxels=512**3):
""" Downscale image if total number of voxels exceeds 512³.
Args:
img (np.Array): Input image.
max_voxels (int, optional): Max number of voxels. Defaults to 512³=134217728.
Returns:
np.Array: Downscaled image if total number of voxels exceeds 512³.
"""
# Calculate total number of pixels in the image
total_voxels = np.prod(img.shape)
# If total pixels is less than or equal to 512³, return original image
if total_voxels <= max_voxels:
return img
# Calculate zoom factor
zoom_factor = (max_voxels / total_voxels) ** (1/3)
# Downscale image
return zoom(img, zoom_factor)
def scale_to_float16(arr: np.ndarray):
"""
......
......@@ -9,10 +9,22 @@ Volumetric visualization using K3D
import k3d
import numpy as np
from qim3d.utils.internal_tools import scale_to_float16
def vol(img, aspectmode="data", show=True, save=False, grid_visible=False, cmap=None, samples="auto", **kwargs):
from qim3d.io.logger import log
from qim3d.utils.internal_tools import downscale_img, scale_to_float16
def vol(
img,
aspectmode="data",
show=True,
save=False,
grid_visible=False,
cmap=None,
samples="auto",
max_voxels=412**3,
**kwargs,
):
"""
Visualizes a 3D volume using volumetric rendering.
......@@ -72,10 +84,19 @@ def vol(img, aspectmode="data", show=True, save=False, grid_visible=False, cmap=
else:
samples = int(samples) # make sure it's an integer
if aspectmode.lower() not in ["data", "cube"]:
raise ValueError("aspectmode should be either 'data' or 'cube'")
# check if image should be downsampled for visualization
original_shape = img.shape
img = downscale_img(img, max_voxels=max_voxels)
new_shape = img.shape
if original_shape != new_shape:
log.warning(
f"Downsampled image for visualization. From {original_shape} to {new_shape}"
)
plt_volume = k3d.volume(
scale_to_float16(img),
bounds=(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment