%load_ext autoreload
%autoreload 2
%config InlineBackend.rc = {'figure.figsize': (10,6)}
%matplotlib inline

Select a region of interest

This example shows how to crop an image using sample data from nanomesh.data.

If you want to use your own data, any numpy array can be passed to into a `Volume <https://nanomesh.readthedocs.io/en/latest/nanomesh.volume.html#nanomesh.volume.Volume>`__ object. Data stored as .npy can be loaded using Volume.load().

from nanomesh import Volume
from nanomesh.data import nanopores3d

data = nanopores3d()

vol = Volume(data)
plane = vol.select_plane(x=161)

Use .select_roi to open an interactive widget. Select a region of interest in the figure by enclosing them within a polygon. A rectangle is fitted to the polygon.

  • Press the β€˜esc’ key to start a new polygon

  • Hold the β€˜shift’ key to move all of the vertices

  • Hold the β€˜ctrl’ key to move a single vertex

roi = plane.select_roi()

The .bbox attribute is updated when the selection above changes.

array([[0, 0],
       [0, 1],
       [1, 1],
       [1, 0]])

Use the .crop_to_roi method to extract the region of interest.

plane_roi = plane.crop_to_roi(bbox=roi.bbox)
<AxesSubplot:xlabel='x', ylabel='y'>

Manual cropping

Alternatively, if you know which points to extract from the to

Use the minimum_bounding_rectangle function to fit the smallest rectangle around the given points.

The example below demonstrates this on a slanted square. Well, almost, because the coordinates contain a small error to demonstrate the function.

from nanomesh.image import minimum_bounding_rectangle
import numpy as np

roi = np.array([
    [60, 60],
    [110, 110],
    [60, 145],  # <- should be 150
    [10, 110],

bbox = minimum_bounding_rectangle(roi)
array([[110., 110.],
       [ 60.,  60.],
       [ 10., 110.],
       [ 60., 160.]])

The bounding box (bbox) can then be used to extract this region from the image using extract_rectangle. If the bounding box is slanted, the image will be straightened using an Euclidean transform.

from nanomesh.image import extract_rectangle

cropped = plane.apply(extract_rectangle, bbox=bbox)
<AxesSubplot:xlabel='x', ylabel='y'>

Generated by nbsphinx from a Jupyter notebook.