Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
qim3d
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
QIM
Tools
qim3d
Commits
421e1c6b
Commit
421e1c6b
authored
1 year ago
by
s204159
Committed by
fima
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
K3d vol resize
parent
a1aaba88
No related branches found
No related tags found
1 merge request
!82
K3d vol resize
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
qim3d/utils/internal_tools.py
+37
-11
37 additions, 11 deletions
qim3d/utils/internal_tools.py
qim3d/viz/k3d.py
+35
-14
35 additions, 14 deletions
qim3d/viz/k3d.py
with
72 additions
and
25 deletions
qim3d/utils/internal_tools.py
+
37
−
11
View file @
421e1c6b
"""
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
):
"""
...
...
This diff is collapsed.
Click to expand it.
qim3d/viz/k3d.py
+
35
−
14
View file @
421e1c6b
...
...
@@ -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
=
(
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment