Skip to main content

Tool to draw regions of interest (ROIs)

Project description



Small python module to select a polygonal region of interest (ROI) in
an image that is stored as a numpy array. The usage is similar to the
function present in the image processing toolbox from MATLAB.


** Installation
Either from PyPi: ~pip install roipoly~

Or get the latest version from github: ~pip install git+

** Running the examples

Basic usage:
#+begin_SRC shell
python examples/

Drawing multiple ROIs:
#+begin_SRC shell
python examples/

** Usage
*** Creating a ROI
In your python code, import the roipoly module using
#+begin_SRC python
from roipoly import RoiPoly
To draw a ROI within an image present as a numpy array, show it first
using e.g. pylabs's =imshow=:
#+begin_SRC python
from matplotlib import pyplot as plt
Then let the user draw a polygonal ROI within that image:
#+begin_SRC python
my_roi = RoiPoly(color='r') # draw new ROI in red color
This lets the user interactively draw a polygon within the image by clicking
with the left mouse button to select the vertices of the polygon. To
close the polygon, click with the right mouse button. After finishing
the ROI, the current figure is closed so that the execution of the code
can continue.

*** Displaying a ROI
To display a created ROI within an image, first display the image as
described above using e.g. =imshow=. Then,
#+begin_SRC python
shows the created ROI on top of this image.

Display multiple ROIs like so:
#+begin_SRC python
for r in [my_roi1, my_roi2, my_roi3]

To additionally show the mean pixel grey value inside a ROI in the
image, type
#+begin_SRC python

*** Extracting a binary mask image
The function =get_mask(image)= creates a binary mask for a certain ROI
instance, that is, a 2D numpy array of the size of the image array,
whose elements are =True= if they lie inside the ROI polygon,
and =False= otherwise.
#+begin_SRC python
mask = my_roi.get_mask(image)
plt.imshow(mask) # show the binary signal mask

This mask image can be used to e.g. calculate the mean pixel intensity
in an image over that ROI:
#+begin_SRC python
mean = plt.mean(image[mask])

*** Drawing multiple ROIs
See =examples/

** Credits
Based on a code snippet originally posted [[][here]] by Daniel Kornhauser.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for roipoly, version 0.5.3
Filename, size File type Python version Upload date Hashes
Filename, size roipoly-0.5.3.tar.gz (9.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page