GithubHelp home page GithubHelp logo

mxlutz / roipoly Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jdoepfert/roipoly.py

0.0 0.0 0.0 108 KB

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

License: Apache License 2.0

Python 91.71% Makefile 8.29%

roipoly's Introduction

https://travis-ci.org/jdoepfert/roipoly.py.svg?branch=master

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

Installation

Either from PyPi: pip install roipoly

Or get the latest version from github: pip install git+https://github.com/jdoepfert/roipoly.py

Running the examples

Basic usage:

python examples/basic_example.py

Drawing multiple ROIs:

python examples/multi_roi_example.py

Usage

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:

from matplotlib import pyplot as plt
plt.imshow(image)

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

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,

my_roi.display_roi()

shows the created ROI on top of this image.

Display multiple ROIs like so:

for r in [my_roi1, my_roi2, my_roi3]
    r.display_roi()

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

my_roi.display_mean(image)

Note that you can only pass 2D images to display_mean()! If you have e.g. an RGB-image with dimension 3, you need to make the call like so:

mask = my_roi.display_mean(rgb_image[:, :, 0])

Extracting a binary mask image

The function get_mask() creates a binary mask for a certain ROI instance, that is, a 2D numpy array of the size of the (2D) image array, whose elements are True if they lie inside the ROI polygon, and False otherwise.

mask = my_roi.get_mask(image)
plt.imshow(mask) # show the binary signal mask

Note that you can only pass 2D images to get_mask(), If you have e.g. an RGB-image with dimension 3, you need to make the call like so:

mask = my_roi.get_mask(rgb_image[:, :, 0])

The resulting mask image can then be used to e.g. calculate the mean pixel intensity in an image over that ROI:

mean = plt.mean(image[mask])

To get the ROI coordinates [(x1, y1), (x2, y2), …]:

roi_coordinates = my_roi.get_roi_coordinates()

Drawing multiple ROIs

See examples/multi_roi_example.py

Credits

Based on a code snippet originally posted here by Daniel Kornhauser.

roipoly's People

Contributors

jdoepfert avatar schmudo avatar georgedeath avatar xksteven avatar venissacarolquadros avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.