Overlay in slicer (Feature request)

Is it possible to have an overlay feature in the qim3d.viz.slicer function, such that I provide it with 2 volumes, one the normal as it takes now and another used as an overlay with a alpha/transparency slider perhaps. I imagine it similar to the functionality of qim3d.viz.threshold, but the overlay is not gained directly from taking a threshold.

Something like this:

The code for reference:

%matplotlib widget
plt.close("all")
alphas = np.zeros_like(vol_copy)
alphas[vol_copy != 0] = 1
# Base and overlay volumes
volume = vol
overlay_volume = vol_copy
alpha_volume = alphas

# Initial axis and slice index
axis = 'x'
slice_index = 50
overlay_visible = [True]  # Mutable container to track visibility

# Function to get slice based on axis and index
def get_slice(volume, axis, index):
    if axis == 'x':
        return volume[index, :, :]
    elif axis == 'y':
        return volume[:, index, :]
    elif axis == 'z':
        return volume[:, :, index]

# Initial slices
base_slice = get_slice(volume, axis, slice_index)
overlay_slice = get_slice(overlay_volume, axis, slice_index)
alpha_slice = get_slice(alpha_volume, axis, slice_index)

# Create figure and axes with larger size
fig, ax = plt.subplots(figsize=(10, 10))
plt.subplots_adjust(bottom=0.35, left=0.25)
img_base = ax.imshow(base_slice, cmap='gray',vmin=0.01,vmax=0.03, aspect='auto')
img_overlay = ax.imshow(overlay_slice, cmap='spring', alpha=alpha_slice, aspect='auto')

# Slider axis (moved below image)
ax_slider = plt.axes([0.25, 0.2, 0.65, 0.03])
slider = Slider(ax_slider, 'Slice', 0, volume.shape[0]-1, valinit=slice_index, valstep=1)

# Radio buttons for axis selection
ax_radio = plt.axes([0.025, 0.5, 0.1, 0.1])
radio = RadioButtons(ax_radio, ('x', 'y', 'z'))

# Add another slider for alpha intensity
ax_alpha_slider = plt.axes([0.25, 0.05, 0.65, 0.03])
alpha_slider = Slider(ax_alpha_slider, 'Alpha Intensity', 0.0, 1.0, valinit=1.0)


# Update function for slider
def update(val):
    idx = int(slider.val)
    current_axis = radio.value_selected
    base_slice = get_slice(volume, current_axis, idx)
    overlay_slice = get_slice(overlay_volume, current_axis, idx)
    alpha_slice = get_slice(alpha_volume, current_axis, idx)
    scaled_alpha = alpha_slice * alpha_slider.val
    img_base.set_data(base_slice)
    img_overlay.set_data(overlay_slice)
    img_overlay.set_alpha(scaled_alpha)
    fig.canvas.draw_idle()


# Update function for radio buttons
def change_axis(label):
    max_index = volume.shape['xyz'.index(label)] - 1
    slider.valmax = max_index
    slider.ax.set_xlim(slider.valmin, max_index)
    slider.set_val(min(slider.val, max_index))
    update(slider.val)


slider.on_changed(update)
alpha_slider.on_changed(update)
radio.on_clicked(change_axis)

plt.show()

Hi,

I am not sure if you have been informed or noticed the feature, but it is there since Sep 18 with pull request 55 ( Deer mandible by GrunyD · Pull Request #55 · qim-center/qim3d · GitHub ).
You can achieve this with
qim3d.viz.slicer(vol, mask = mask, mask_alpha = alpha, mask_color_map = colormap)

Both alpha and colormap are set to default values (0.4 and ‘gray’) so you don’t need to pass them unless you want to. However there is no slider for setting alpha, nor a dropdown menu for colormap. Do you think that is something worth adding?

Hi,

Thanks, yes Felipe told me :slight_smile:

But now other people who see this also knows