P2DingoCV.Util.VisualUtil

Classes

VisualUtils([pathToVisuals])

Utility class for visualizing and saving images, histograms, and tables.

class P2DingoCV.Util.VisualUtil.VisualUtils(pathToVisuals='visuals')[source]

Bases: object

Utility class for visualizing and saving images, histograms, and tables.

Provides methods to plot image histograms, save frames, plot frequency arrays, and pretty-print tables. All outputs are saved to the specified directory.

Parameters:

pathToVisuals (str)

__init__(pathToVisuals='visuals')[source]

Initialize the VisualUtils object and create output directory.

Parameters:

pathToVisuals (str) – Path to directory where visuals will be saved. Defaults to “visuals”.

drawFrameCountours(frame, componentMask, cx, cy, lbl)[source]

Draws the contours of a mask on the given frame and places a label at the specified centroid position.

Parameters:
  • frame (numpy.ndarray) – The image frame on which to draw the contours and label.

  • componentMask (numpy.ndarray) – A binary mask image used to extract contours (non-zero pixels represent the object of interest).

  • cx (int) – The x-coordinate of the centroid where the label should be placed.

  • cy (int) – The y-coordinate of the centroid where the label should be placed.

  • lbl (float) – The label value to be displayed at the centroid position.

Returns:

The function modifies the frame in place by drawing the contours and adding the label.

Return type:

None

Notes

  • The function assumes that the mask contains a single object (only the first contour is drawn).

  • The contour is drawn in green (0, 255, 0) with a thickness of 1 pixel.

  • The label is drawn in red (0, 0, 255) using the font cv.FONT_HERSHEY_SIMPLEX.

  • The label is displayed with 2 decimal points.

static drawLines(frame, lines, colour)[source]
Parameters:
  • frame (numpy.ndarray)

  • lines (numpy.ndarray | None)

  • colour (str)

Return type:

numpy.ndarray

static drawSpacingLines(frame, rows, cols, height, width)[source]

Draws a rows x cols grid over the image with thick blue lines and a blue outer outline.

Parameters:
  • frame (numpy.ndarray)

  • rows (int)

  • cols (int)

  • height (int)

  • width (int)

Return type:

numpy.ndarray

plotFreqArray(arr, title)[source]

Plot a 1D array as a bar chart and save it as a PNG.

Parameters:
  • arr (np.ndarray) – 1D array of values to plot.

  • title (str) – Title of the plot, also used as filename.

Return type:

None

plotHistogram(frame)[source]

Plot and save the histogram of an image.

Supports grayscale and BGR images. Saves the histogram as a PNG in the output directory.

Parameters:

frame (Frame) – Image array (grayscale or BGR).

Returns:

The input frame, also displayed using OpenCV.

Return type:

Frame | None

Raises:
  • AssertionError – If frame is None.

  • ValueError – If the image format is unsupported.

static printTable(data, headers=None, precision=2)[source]

Pretty-print a list of lists (or tuples) as a table with borders.

Numeric values are formatted to the specified precision. Nested tuples/lists are also formatted.

Parameters:
  • data (list[list|tuple]) – 2D list or list of tuples containing table data.

  • headers (list[str] | None) – Optional list of column headers.

  • precision (int) – Number of decimal places for numeric values. Defaults to 2.

Return type:

None

saveFrame(frame, tag, frameCount, logger, folder='frames')[source]

Save an image frame with a tag and frame count.

Parameters:
  • frame (Frame) – Image frame to save.

  • tag (str) – Descriptive tag for the frame.

  • frameCount (int) – Frame number to include in the filename.

  • folder (str) – Subfolder under the output path to save frames. Defaults to “frames”.

  • logger (logging.Logger)

Return type:

None

savePanelArray(panels, frameCount, logger, folder='cells')[source]

Save an array of panel images to disk.

Parameters:
  • panels (list[Frame]) – List of panel image frames to save.

  • frameCount (int) – Frame number to include in the filenames.

  • logger (Logger) – Logger object for logging messages.

  • folder (str) – Subfolder under the output path to save panels. Defaults to “frames”.

Return type:

None

static splitImageToGrid(image, h_cells, v_cells)[source]

Split an image into a grid of equally sized cells.

The image is divided into h_cells rows and v_cells columns. Any remainder pixels (if the image is not perfectly divisible) are included in the last row/column cells.

Parameters:
  • image (Frame) – Input image as a NumPy array of shape (H, W, C).

  • h_cells (int) – Number of horizontal grid cells (rows).

  • v_cells (int) – Number of vertical grid cells (columns).

Returns:

A list of image cells in row-major order

(top-left to bottom-right), each as a Frame.

Return type:

list[Frame]

static stretchToAspectRatio(image, targetHeight, targetRatio)[source]

Resize (stretch) an image to a target aspect ratio and height.

This function changes the pixel dimensions of the image to match the desired aspect ratio by stretching (not cropping). New pixels are interpolated.

Parameters:
  • image (Frame) – Input image as a NumPy array of shape (H, W, C).

  • targetHeight (int) – Desired output height in pixels.

  • targetRatio (float) – Desired width/height ratio.

Returns:

Resized image with shape (targetHeight, targetWidth, C).

Return type:

Frame