polyroi.py
Select and manipulate Region of interest.
📝 Table of Contents
🧐 About
A small python module to select a polygonal region of interest (ROI) in a given image that is stored as a Shape object. You can use this Shape object later to manipulate the polygon selected. You can also extract the inner content from an image, calculate the histogram of the created shape, calculate the center of the shape, rotate the shape around its center, or translate the shape.
🏁 Getting Started
import cv2 as cv
from polyroi import Shape
img = cv.imread('image.jpg')
shape = Shape.get_roi(img)
shape.draw_shape(img, color=(0, 255, 255), thickness=1)
while(1):
cv.imshow("Getting Started", img)
k = cv.waitKey(1) & 0xFF
if k == 27:
break
cv.destroyAllWindows()
Prerequisites
pip install cv2
pip install numpy
Installing
pip install polyroi
🎈 Usage
Some time ago, I looked for an efficient tool to draw and manipulate polygons in a python environment. But I didn't find anything useful for my case. I did find some tools that can draw and extract a NumPy array, but as for the manipulation of shapes, I had to develop the logic myself. So I decided to create one.
I was trying to implement the particle filter from Part-Based Lumbar Vertebrae Tracking in Videofluoroscopy Using Particle Filter. You can check the repository of how I did manage to work with this package.
img = cv.imread('image.jpg')
shape = Shape.get_roi(img)
shape2 = Shape.copy(shape)
shape2.rotate_around_center(np.pi/4)
shape2.translate_x(5)
shape2.translate_y(5)
shape2.centroid()
shape2.translate_to(10, 15)
shape2.update(5, 3, np.pi / 12)
shape.draw_shape(img, color=(0, 255, 255), thickness=1)
shape2.draw_shape(img, color=(0, 255, 0), thickness=1)
p1, p2 = shape2.to_rectangle()
while(1):
cv.imshow("Getting Started", img)
k = cv.waitKey(1) & 0xFF
if k == 27:
break
cv.destroyAllWindows()
⛏️ Built Using
✍️ Authors
See also the list of contributors who participated in this project.
🎉 Acknowledgements