thermography.utils package

This package contains utility functions which are used by thermography.

Submodules

thermography.utils.ID module

This module implements a simple unique-ID generator. The generated IDs follow the natural numbers order.

Example:
first_id = next_id() # first_id = 0
second_id = next_id() # second_id = 1

reset_id()
third_id = next_id() # third_id = 0

reset_id(10)
fourth_id = next_id() # fourth_id = 10
next_id()[source]

Generates the next unique id.

Returns:The generated ID
reset_id(value: int = 0)[source]

Resets the id of the ID generator.

Parameters:value – New start value of the ID generator.

thermography.utils.display module

This module contains multiple utility functions which can be used to display intermediate representations computed by the ThermoApp class.

draw_intersections(intersections: list, base_image: numpy.ndarray, windows_name: str)[source]

Draws the intersections contained in the first parameter onto the base image passed as second parameter and displays the image using the third parameter as title.

Parameters:
  • intersections – List of intersection coordinates.
  • base_image – Base image over which to render the intersections.
  • windows_name – Title to give to the rendered image.
draw_motion(flow: numpy.ndarray, base_image: numpy.ndarray, windows_name: str, draw_mean_motion: bool = True, nums=10)[source]

Draws the motion flow contained in the first parameter onto the base image passed as second argument and displays the image using the third argument as title.

Parameters:
  • flow – Numpy array of flow computed by the motion estimator.
  • base_image – Base image over which to render the intersection. Note that this image must have the dimensions used for the flow computation.
  • windows_name – Title to give to the rendered image.
  • draw_mean_motion – Boolean flag indicating whether to render also the mean motion estimate.
  • nums – Number of samples in each dimension to be rendered.
draw_rectangles(rectangles: list, base_image: numpy.ndarray, windows_name: str)[source]

Draws the rectangles contained in the first parameter onto the base image passed as second parameter.

This function displays the image using the third parameter as title.

Parameters:
  • rectangles – List of rectangles.
  • base_image – Base image over which to render the rectangles.
  • windows_name – Title to give to the rendered image.
draw_segments(segments: list, base_image: numpy.ndarray, windows_name: str, render_indices: bool = True, colors: list = None)[source]

Draws the segments contained in the first parameter onto the base image passed as second parameter.

This function displays the image using the third parameter as title. The indices associated to the segments are rendered on the image depending on ‘render_indices’. A list of colors can be passed as argument to specify the colors to be used for different segment clusters.

Parameters:
  • segments – List of segment clusters.
  • base_image – Base image over which to render the segments.
  • windows_name – Title to give to the rendered image.
  • render_indices – Boolean flag indicating whether to render the segment indices or not.
  • colors – Color list to be used for segment rendering, such that segments belonging to the same cluster are of the same color.
random_color() → tuple[source]

Generates a random RGB color in [0, 255]^3

Returns:A randomly generated color defined as a triplet of RGB values.
color_from_probabilities(prob: numpy.ndarray) → tuple[source]

Constructs a color tuple given the probability distribution prob.

Parameters:prob – A three dimensional numpy array containing class probabilities.
Returns:The color associated to the probability distribution.

thermography.utils.geometry module

The functions implemented in this module are used bz thermography for the computation of geometric properties.

angle(pt1: numpy.ndarray, pt2: numpy.ndarray) → float[source]

Computes the angle (in radiants) between a segment specified by the two points and the x-axis.

Note

The computed angle lies in [0, pi].

Parameters:
  • pt1 – First point of the segment.
  • pt2 – Second point of the segment.
Returns:

Angle in radiants between the segment and the x-axis.

angle_diff(angle1: float, angle2: float) → float[source]

Computes the angle difference between the input arguments.

Note

The resulting angle difference is in [0, pi * 0.5]

Parameters:
  • angle1 – First angle expressed in radiants.
  • angle2 – Second angle expressed in radiants.
Returns:

Angle difference between the input parameters. This angle represents the smallest positive angle between the input parameters.

aspect_ratio(rectangle: numpy.ndarray) → float[source]

Computes the aspect ratio of a rectangle.

The aspect ratio is computed based on the following rectangle.

   3      s2     2
   *-------------*
   |             |
s4 |             | s3
   |             |
   *-------------*
   0     s1      1
Parameters:rectangle – Rectangle is a numpy array of coordinates ordered as shown in the diagram.
Returns:Aspect ratio of the rectangle.
area(points: numpy.ndarray) → float[source]

Computes the surface of the polygon defined by the coordinates passed as argument.

Parameters:points – List of coordinates defining the polygon’s vertices.
Returns:The surface contained by the polygon.
area_between_rectangles(rect1: numpy.ndarray, rect2: numpy.ndarray) → float[source]

Computes the cumulative surface between the corresponding edges of the two rectangles passed as argument.

*--------------------*
|####################|
|###*-------------*##|
|###|             |##|
|###|             |##|
|###|             |##|
|###*-------------*##|
|####################|
*--------------------*
Parameters:
  • rect1 – First rectangle’s coordinates [[x0,y0],[x1,y1],[x2,y2],[x3,y3]]
  • rect2 – Second rectangle’s coordinates [[x‘0,y‘0],[x‘1,y‘1],[x‘2,y‘2],[x‘3,y‘3]]
Returns:

The surface between the rectangles’ corresponding edges.

line_estimate(seg1: numpy.ndarray, seg2: numpy.ndarray) → tuple[source]

Computes the line estimation (regression) using the endpoints of the segments passed as argument.

Note

Depending on the points’ distribution, the line estimate is computed in the x-y plane as usual, or on the y-x plane (i.e. with inverted coordinates).

Parameters:
  • seg1 – First segment.
  • seg2 – Second segment.
Returns:

The slope and intercept of the estimated line, a boolean indicating whether the slope and intercept refer to a vertical or horizontal polyfit.

mean_segment_angle(segment_list: list) → float[source]

Computes the mean angle of a list of segments.

Note

The computed mean angle lies in [0, pi]

Parameters:segment_list – A list of segments of the form [np.array([x0, y0, x1, y1]), np.array([…]), …. ]
Returns:The mean angle of the segments passed as argument.
merge_segments(segment_list: list) → numpy.ndarray[source]

Computes a unique segments as a representation of the almost collinear segments passed as argument.

Parameters:segment_list – List of almost collinear segments to be merged into a single segment.
Returns:A new segment defined on the line estimation over the segments passed as argument.
point_line_distance(point: numpy.ndarray, slope: float, intercept: float, vertical: bool) → float[source]

Computes the shortest distance between a point and a line defined by its slope and intercept.

Parameters:
  • point – Point given by a 2D coordinate in the form of [x, y]
  • slope – Slope of the line
  • intercept – Intercept of the line
  • vertical – Boolean indicating if the slope and intercept refer to a vertical or horizontal polyfit.
Returns:

Positive minimal distance between the point passed as argument and the line defined by the slope and intercept passed as arguments.

rectangle_contains(rectangle: numpy.ndarray, point: numpy.ndarray) → bool[source]

Computes whether a point is inside a rectangle or not.

Parameters:
  • rectangle – Rectangle to be tested against the query point.
  • point – Point to be tested against the rectangle.
Returns:

True if the point is inside or on the rectangle contours, False otherwise.

segments_collinear(seg1: numpy.ndarray, seg2: numpy.ndarray, max_angle: float = 0.08726646259971647, max_endpoint_distance: float = 50.0) → bool[source]

Tests whether two segments are collinear given some thresholds for collinearity.

Parameters:
  • seg1 – First segment to be tested.
  • seg2 – Second segment to be tested.
  • max_angle – Maximal angle between segments to be accepted as collinear.
  • max_endpoint_distance – Max sum of euclidean distance between the endpoints of the passed segments and the line estimate computed between the segments. This parameter discards almost parallel segments with different intercept.
Returns:

True if the segments are almost collinear, False otherwise.

segment_line_intersection(seg: numpy.ndarray, slope: float, intercept: float) → numpy.ndarray[source]

Computes the intersection point between a segment and a line.

Note

If no intersection is found, a boolean flag set to False is returned instead.

Parameters:
  • seg – Segment to be intersected with the line.
  • slope – Slope of the intersecting line.
  • intercept – Intercept of the intersecting line.
Returns:

Coordinates of the intersection point, or False if no intersection point is found.

segment_min_distance(seg1: numpy.ndarray, seg2: numpy.ndarray) → float[source]

Computes the minimal distance between two segments.

Implementation taken form here.

Parameters:
  • seg1 – First segment defined as [x1, y1, x2, y2]
  • seg2 – Second segment defined as [x2, y3, x4, y4]
Returns:

The minimal distance between the two segments

segment_segment_intersection(seg1: numpy.ndarray, seg2: numpy.ndarray) → numpy.ndarray[source]

Computes the intersection point between two segments.

Note

If no intersection is found, a boolean flag set to False is returned.

Parameters:
  • seg1 – First segment of intersection.
  • seg2 – Second segment of intersection.
Returns:

Coordinates of intersection point, or False if no intersection is found.

sort_rectangle(rectangle: numpy.ndarray) → numpy.ndarray[source]

Sorts the coordinates in the rectangle such that the final indexing corresponds to the following structure:

+-----------> x
|  3             2
|  *-------------*
|  |             |
v  |             |
y  |             |
   *-------------*
   0             1
Parameters:rectangle – numpy array of coordinates with form: [[x0, y0], [x1, y1], [x2, y2], [x3, y3]]
Returns:A rectangle whose vertices are sorted.
sort_segments(segment_list: list) → numpy.ndarray[source]

Sorts the segments passed as argument based on the normal associated to the mean angle.

Parameters:segment_list – A list of segments of the form [[x0, y0, x1, y1], […], …. ]
Returns:A list of indices in the segment list passed as argument which sorts the segments.

thermography.utils.images module

rotate_image(image: numpy.ndarray, a: float)[source]

Rotates the input image by a radiants in counter-clock-wise direction.

Parameters:
  • image – Image to be rotated.
  • a – Rotation angle expressed in radiants.
Returns:

Rotated version of the input image.

scale_image(input_image: numpy.ndarray, s: float)[source]

Scales an input image by the value passed as parameter.

Parameters:
  • input_image – Image to be scaled.
  • s – Scalar value to be applied to the scaling procedure.
Returns:

A copy of the input image scaled by the passed parameter.