API Documentation#

Within the tomni there are helpers for different use cases.

Bounding boxes#

A bounding box is an rectangle defined by 4 number.

x1: lowest x value

x2: highest x value

y1: lowest y value

y2: highest y value

An bounding box is never rotated. The bounding box can be outside the image or even bigger than the image.

Tomni is a collection of image analysis functions useful for CytoSmart solution.

tomni.bbox_fitting(img: numpy.ndarray, x1: int, y1: int, x2: int, y2: int, padding_value: int = 0) numpy.ndarray[source]#

Bounding box fitting: Using cropping and padding to fit an image to the bounding box. Relative to the 0,0 of the input image the image gets cropped and padded to fit with x1, y1, x2 and y2. The result is including the pixels x1, x2, y1 and y2. So a 0, 0, 9, 9 gives an 10x10 result.

WARNING: Coordinates are IMAGE-coordinates. So that is the same pillow and OpenCV uses but swapped from what Numpy and Scipy use (ARRAY-coordinates). Yes, I hate it too.

Args:

img: (np.ndarray): The image the bounding box will be extracted from

x1: (int): Lowest value of X

y1: (int): Lowest value of Y

x2: (int): Highest value of X

y2: (int): Highest value of Y

padding_value: (int, optional): Every pixel outside the image bit inside the bounding box will get this value. Defaults to 0.

Returns:

np.ndarray: Extracted patch within the bounding box from the image

tomni.bbox_fitting_center(img: numpy.ndarray, size, padding_value: int = 0) numpy.ndarray[source]#

Creates an image of size ‘size’ ([(int) x, (int) y]) in IMAGE-Coordinates). The orignal image will be centered and padded and/or cropped to fit the size.

img: numpy.ndarray size: [int, int]

output: numpy.ndarray

tomni.bbox_operations.check_overlap_bbox(bb1: Union[tuple, list], bb2: Union[tuple, list]) bool[source]#

Checks if two bounding boxes overlap with eachother. The bounding boxes are assumed the be structured as follows: (xmin, ymin, xmax, ymax). Warning: Touching bounding boxes are not seen as overlapping

Args:

bb1 (Union[tuple, list]): boundingbox 1, (x1, y1, x2 ,y2) bb2 (Union[tuple, list]): boundingbox 2, (x1, y1, x2 ,y2)

Raises:

ValueError: Length of bounding box is the wrong size

Returns:

bool: if the bounding boxes are touching

Contour operations#

OpenCV has already nice operations to apply to your contours. We have some additional ones

tomni.contour_operations.approximate_circle_by_area(contour: numpy.ndarray) Tuple[float, float, float][source]#

Returns a circle with the same area and center as the contour.

Args:

contour (np.ndarray): An opencv contour of a single object

Returns:

x: (float) Center x postion y: (float) Center y postion radius: (float) Radius of the circle

tomni.contour_operations.circularity(contour: numpy.ndarray) float[source]#

Calculates the circularity of a contour. This is based on the perimeter and area of the contour. A circle will give the smallest area for a given perimeter, so this is 1.

The smaller the area is for a perimeter the less circular the contour is. So the closer is get to 0.

The formula works from calculating the r**2 based on Area and Perimeter Area based: r**2 = A/pi

Perimeter based: r**2 = (p/2*pi)**2

Dividing these two gives circularity c = (A/pi) / (p/2*pi)**2 = (4 * pi * A) / (p**2)

Args:

contour (np.ndarray): Contour as given by opencv

Returns:

float: circularity (0 < c <= 1)

tomni.contour_operations.get_center(contour: numpy.ndarray) Tuple[int, int][source]#

Returns the center of a contour

Args:

contour (np.ndarray): An opencv contour of a single object

Returns:

x: (float) Center x postion y: (float) Center y postion

tomni.contour_operations.roundness(contour: numpy.ndarray) float[source]#

Returns roundess of the contour Roundness is determined by difference in areas of the contour and enclosing circle Roundness = \(\frac{A}{r_e^2 \pi}\) With \(r_e\) being the radius of the enclosing circle.

Args:

contour (np.ndarray): An opencv contour of a single object

Returns:

float: roundess number between 0 and 1