# plot.py
from typing import Optional, Tuple, Union
import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.axes import Axes
import matplotlib.patches as patches
import xarray as xr
from .analysis import (
min_enclosing_circle,
min_enclosing_rotated_rectangle,
min_enclosing_ellipse,
fit_ellipse
)
[docs]
def quick_look_single_particle_image(
particle_image: xr.DataArray,
ax: Optional[Axes] = None,
show: bool = True
) -> None:
"""
Displays a quick look visualization of a single particle image.
Parameters:
-----------
- particle_image (xarray.DataArray): An xarray DataArray representing the image of a single particle.
The DataArray should have two dimensions corresponding to the image's pixel dimensions.
- ax (matplotlib.axes.Axes, optional): The axes to plot on. If None, the current axes are used.
- show (bool, optional): Whether to call plt.show() after plotting. Defaults to True.
Returns:
--------
None. The function displays the image plot or adds it to the provided axes.
History:
--------
- 2024-02-24: Created by Yongjie Huang (huangynj@gmail.com)
"""
if ax is None:
ax = plt.gca()
# Transpose and plot using xarray's plotting method, passing the axes
particle_image.transpose().plot(ax=ax)
# Adjusting the aspect ratio of the plot to 'equal'
ax.set_aspect('equal', adjustable='box')
if show:
plt.show()
[docs]
def plot_single_particle_image_with_enclosing_shape(
particle_image: xr.DataArray,
plot_circle: bool = True,
plot_rectangle: bool = True,
plot_MinEllipse: bool = True,
plot_FitEllipse: bool = True,
ax: Optional[Axes] = None,
show: bool = True
) -> None:
"""
Plots a single particle image along with various enclosing geometric shapes.
Parameters:
-----------
- particle_image (xarray.DataArray): An xarray DataArray representing the image of a single particle.
- plot_circle (bool, optional): Whether to plot the minimum enclosing circle. Defaults to True.
- plot_rectangle (bool, optional): Whether to plot the minimum enclosing rotated rectangle. Defaults to True.
- plot_MinEllipse (bool, optional): Whether to plot the minimum enclosing ellipse. Defaults to True.
- plot_FitEllipse (bool, optional): Whether to plot the fitted ellipse. Defaults to True.
- ax (matplotlib.axes.Axes, optional): The axes to plot on. If None, the current axes are used.
- show (bool, optional): Whether to call plt.show() after plotting. Defaults to True.
Returns:
--------
None. The function displays the plot or adds elements to the provided axes.
History:
--------
- 2024-02-24: Created by Yongjie Huang (huangynj@gmail.com)
"""
if ax is None:
ax = plt.gca()
# Transpose and plot using xarray's plotting method
particle_image.transpose().plot(ax=ax)
# Adjusting the aspect ratio of the plot to 'equal'
ax.set_aspect('equal', adjustable='box')
if plot_circle:
circle_center_x, circle_center_y, circle_radius = min_enclosing_circle(particle_image)
circle = plt.Circle((circle_center_x, circle_center_y), circle_radius, color='red', fill=False, linewidth=1)
ax.add_artist(circle)
if plot_rectangle:
rect = min_enclosing_rotated_rectangle(particle_image)
box = np.intp(cv2.boxPoints(rect))
box_closed = np.vstack([box, box[0]]) # To ensure the box is closed, append the first point at the end
ax.plot(box_closed[:, 0], box_closed[:, 1], 'b-', linewidth=1)
if plot_MinEllipse:
(ellipse_center_x, ellipse_center_y), (ellipse_width, ellipse_height), ellipse_angle = min_enclosing_ellipse(particle_image)
ellipse = patches.Ellipse((ellipse_center_x, ellipse_center_y), ellipse_width, ellipse_height, angle=ellipse_angle, linewidth=1, fill=False, edgecolor='m')
ax.add_patch(ellipse)
if plot_FitEllipse:
(ellipse_center_x, ellipse_center_y), (ellipse_width, ellipse_height), ellipse_angle = fit_ellipse(particle_image)
ellipse = patches.Ellipse((ellipse_center_x, ellipse_center_y), ellipse_width, ellipse_height, angle=ellipse_angle, linewidth=1, fill=False, edgecolor='w')
ax.add_patch(ellipse)
if show:
plt.show()