# Examples from pyvista.ImageDataFilters.select_values
# ====================================================

# Load a CT image. Here we load
# `download_whole_body_ct_male()`.
import pyvista as pv
from pyvista import examples
dataset = examples.download_whole_body_ct_male()
ct_image = dataset['ct']

# Show the initial data range.
ct_image.get_data_range()

# Select intensity values above `150` to select the bones.
bone_range = [150, float('inf')]
fill_value = -1000  # fill with intensity values corresponding to air
bone_image = ct_image.select_values(
    ranges=bone_range, fill_value=fill_value
)

# Show the new data range.
bone_image.get_data_range()

# Plot the selected values. Use `'foreground'` opacity to make the fill value
# transparent and the selected values opaque.
pl = pv.Plotter()
_ = pl.add_volume(
    bone_image,
    opacity='foreground',
    cmap='bone',
)
pl.view_zx()
pl.camera.up = (0, 0, 1)
pl.show()

# Use `'replacement_value'` to binarize the selected values instead. The fill
# value, or background, is `0` by default.
bone_mask = ct_image.select_values(ranges=bone_range, replacement_value=1)
bone_mask.get_data_range()

# Generate a surface contour of the mask and plot it.
surf = bone_mask.contour_labels()

pl = pv.Plotter()
_ = pl.add_mesh(surf, color=True)
pl.view_zx()
pl.camera.up = (0, 0, 1)
pl.show()

# Load a color image. Here we load `download_beach()`.
image = examples.download_beach()
plot_kwargs = dict(
    cpos='xy', rgb=True, lighting=False, zoom='tight', show_axes=False
)
image.plot(**plot_kwargs)

# Select components from the image which have a strong red component.
# Use `replacement_value` to replace these pixels with a pure red color
# and `fill_value` to fill the rest of the image with white pixels.
white = [255, 255, 255]
red = [255, 0, 0]
red_range = [200, 255]
red_component = 0
selected = image.select_values(
    ranges=red_range,
    component_mode=red_component,
    replacement_value=red,
    fill_value=white,
)

selected.plot(**plot_kwargs)

# ----------------------------------------------------------------------
# Generated by sphinx-examples-as-code https://github.com/pyvista/sphinx-examples-as-code
