Skip to content

Select a polygonal region of interest (ROI) with python and matplotlib, similar to the roipoly.m function from MATLAB.

License

Notifications You must be signed in to change notification settings

okuspokus/roipoly.py

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

roipoly.py

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 roipoly function present in the image processing toolbox from MATLAB.

/img/ROIs.PNG

Requirements

The module requires the python modules numpy and matplotlib to work.

Running the example

  • Copy the files roipoly.py and example.py to a directory.
  • Switch to that directory and run the example from the shell using
python example.py

Usage

Installation

Copy the file roipoly.py to your working directory or add it to PYTHONPATH.

Creating a ROI

In your python code, import the roipoly module using

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:

import pylab as pl
pl.imshow(image) 

Then let the user draw a polygonal ROI within that image:

MyROI = roipoly(roicolor='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,

MyROI.displayROI() 

shows the created ROI on top of this image.

To display multiple ROIs, use e.g. list comprehensions:

[x.displayROI() for x in [MyROI1, MyROI2, MyROI3]]

To additionally show the mean pixel grey value inside a ROI in the image, type

MyROI.displayMean(image)

Extracting a binary mask image

The function getMask(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.

mask = MyROI.getMask(image)
pl.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:

Mean = pl.mean(image[mask])

About

Select a polygonal region of interest (ROI) with python and matplotlib, similar to the roipoly.m function from MATLAB.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%