roi_demo.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """
  2. Example of working with RegionOfInterest objects
  3. """
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from neo.core import CircularRegionOfInterest, RectangularRegionOfInterest, PolygonRegionOfInterest
  7. from numpy.random import rand
  8. def plot_roi(roi, shape):
  9. img = rand(120, 100)
  10. pir = np.array(roi.pixels_in_region()).T
  11. img[pir[1], pir[0]] = 5
  12. plt.imshow(img, cmap='gray_r')
  13. plt.clim(0, 5)
  14. ax = plt.gca()
  15. ax.add_artist(shape)
  16. roi = CircularRegionOfInterest(x=50.3, y=50.8, radius=30.2)
  17. shape = plt.Circle(roi.centre, roi.radius, color='r', fill=False)
  18. plt.subplot(1, 3, 1)
  19. plot_roi(roi, shape)
  20. roi = RectangularRegionOfInterest(x=50.3, y=40.2, width=40.1, height=50.3)
  21. shape = plt.Rectangle((roi.x - roi.width/2.0, roi.y - roi.height/2.0),
  22. roi.width, roi.height, color='r', fill=False)
  23. plt.subplot(1, 3, 2)
  24. plot_roi(roi, shape)
  25. roi = PolygonRegionOfInterest(
  26. (20.3, 30.2), (80.7, 30.1), (55.2, 59.4)
  27. )
  28. shape = plt.Polygon(np.array(roi.vertices), closed=True, color='r', fill=False)
  29. plt.subplot(1, 3, 3)
  30. plot_roi(roi, shape)
  31. plt.show()