roi_rectangle
A Python module for handling rectangular regions of interest (ROI) in images.
Features
- Define a rectangular ROI with coordinates.
- Get the center of the ROI.
- Move the ROI to a new center.
- Resize the ROI.
- Get the coordinates and area of the ROI.
- Slice the ROI region from an image.
Installation
To install the roi-rectangle
module, use:
pip install roi-rectangle
Usage
Here's an example of how to use the RoiRectangle
module:
from roi_rect import RoiRectangle
import numpy as np
import matplotlib.pyplot as plt
test_image = np.random.random((100, 100))
roi = RoiRectangle(x1=20, y1=30, x2=70, y2=80)
print("Initial ROI:")
print(roi)
print("Center Coordinate:", roi.center)
print("Width:", roi.width)
print("Height:", roi.height)
new_center = (50, 50)
roi.move_to_center(new_center)
print("\nAfter Moving to Center:")
print(roi)
new_width, new_height = 30, 40
roi.resize(new_width, new_height)
print("\nAfter Resizing:")
print(roi)
roi_slice = roi.slice(test_image)
print("\nROI Slice:")
print(roi_slice)
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(test_image, cmap='gray')
plt.subplot(1, 2, 2)
plt.title('ROI Slice')
plt.imshow(roi_slice, cmap='gray')
plt.gca().add_patch(plt.Rectangle((roi.x1, roi.y1), roi.width, roi.height, linewidth=2, edgecolor='r', facecolor='none'))
plt.show()
License
This project is licensed under the MIT License.