gbvision.utils.shapes package¶
Submodules¶
gbvision.utils.shapes.base_circle module¶
-
class
gbvision.utils.shapes.base_circle.BaseCircle[source]¶ Bases:
gbvision.utils.shapes.base_shape.BaseShape-
classmethod
area(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Union[int, float]]) → Union[int, float][source]¶ calculates the area of the shape
Parameters: shape – the shape Returns: the area of the shape
-
static
center(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Union[int, float]]) → Tuple[Union[int, float], Union[int, float]][source]¶ calculates the center-of-mass of the shape
Parameters: shape – the shape Returns: the center of the shape
-
static
collision(shape1: Tuple[Tuple[Union[int, float], Union[int, float]], Union[int, float]], shape2: Tuple[Tuple[Union[int, float], Union[int, float]], Union[int, float]]) → bool[source]¶ checks if the two shapes are colliding
Parameters: - shape1 – the first shape
- shape2 – the second shape
Returns: True if the shapes are colliding, False otherwise
-
static
from_bounding_rect(bounding_rect: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → Tuple[Tuple[Union[int, float], Union[int, float]], Union[int, float]][source]¶ Converts a rect to the closest possible object of this shape that is contained in it
Parameters: bounding_rect – The rect to convert Returns: A shape of this type that is as close as possible to this rect
-
from_contour()¶ minEnclosingCircle(points) -> center, radius . @brief Finds a circle of the minimum area enclosing a 2D point set. . . The function finds the minimal enclosing circle of a 2D point set using an iterative algorithm. . . @param points Input vector of 2D points, stored in std::vector<> or Mat . @param center Output center of the circle. . @param radius Output radius of the circle.
-
classmethod
root_area(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Union[int, float]]) → Union[int, float][source]¶ calculates the square root of the area of the shape default is the square root of cls.shape_area, but it can be overridden in case there is a simpler way (for example for circles)
Parameters: shape – the shape Returns: the square root of the area of the shape
-
static
set_center(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Union[int, float]], new_center: Tuple[Union[int, float], Union[int, float]]) → Tuple[Tuple[Union[int, float], Union[int, float]], Union[int, float]][source]¶ Returns an identical copy that has been moved to a new location Only the center should be different
Parameters: - shape – The shape
- new_center – The new center, calling BaseShape.center on the return value should result in this
Returns: A copy of this shape, with the new center
-
classmethod
sort(shapes: List[Tuple[Tuple[Union[int, float], Union[int, float]], Union[int, float]]])[source]¶ sorts the list of shapes by area, should be overridden to use area root in case it’s better doesn’t modify the given list, returns a new list
Parameters: shapes – the list of shapes to sort Returns: a sorted copy of the list of shapes
-
static
to_bounding_rect(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Union[int, float]]) → Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]][source]¶ Finds the bounding rect of this shape
Parameters: shape – The shape Returns: The bounding rect of this shape, as a gbvision.Rect
-
classmethod
gbvision.utils.shapes.base_contour module¶
-
class
gbvision.utils.shapes.base_contour.BaseContour[source]¶ Bases:
gbvision.utils.shapes.base_shape.BaseShape-
static
area(shape: numpy.ndarray) → Union[int, float][source]¶ calculates the area of the shape
Parameters: shape – the shape Returns: the area of the shape
-
static
center(shape: numpy.ndarray) → Tuple[Union[int, float], Union[int, float]][source]¶ calculates the center-of-mass of the shape
Parameters: shape – the shape Returns: the center of the shape
-
static
collision(shape1: numpy.ndarray, shape2: numpy.ndarray) → bool[source]¶ checks if the two shapes are colliding
Parameters: - shape1 – the first shape
- shape2 – the second shape
Returns: True if the shapes are colliding, False otherwise
-
static
from_bounding_rect(bounding_rect: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → numpy.ndarray[source]¶ Converts a rect to the closest possible object of this shape that is contained in it
Parameters: bounding_rect – The rect to convert Returns: A shape of this type that is as close as possible to this rect
-
static
from_contour(cnt: numpy.ndarray) → numpy.ndarray[source]¶ Converts a single contour to this shape
Parameters: cnt – The contour Returns: A shape representing this contour
-
classmethod
set_center(shape: numpy.ndarray, new_center: Tuple[Union[int, float], Union[int, float]]) → numpy.ndarray[source]¶ Returns an identical copy that has been moved to a new location Only the center should be different
Parameters: - shape – The shape
- new_center – The new center, calling BaseShape.center on the return value should result in this
Returns: A copy of this shape, with the new center
-
static
gbvision.utils.shapes.base_convex_polygon module¶
-
class
gbvision.utils.shapes.base_convex_polygon.BaseConvexPolygon[source]¶ Bases:
gbvision.utils.shapes.base_polygon.BasePolygon-
static
collision(shape1: Union[List[Tuple[Union[int, float], Union[int, float]]], numpy.ndarray], shape2: Union[List[Tuple[Union[int, float], Union[int, float]]], numpy.ndarray]) → bool[source]¶ checks if the two shapes are colliding
Parameters: - shape1 – the first shape
- shape2 – the second shape
Returns: True if the shapes are colliding, False otherwise
-
static
gbvision.utils.shapes.base_ellipse module¶
-
class
gbvision.utils.shapes.base_ellipse.BaseEllipse[source]¶ Bases:
gbvision.utils.shapes.base_rotated_rect.BaseRotatedRect-
static
collision(shape1: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]], float], shape2: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]], float]) → bool[source]¶ checks if the two shapes are colliding
Parameters: - shape1 – the first shape
- shape2 – the second shape
Returns: True if the shapes are colliding, False otherwise
-
from_contour()¶ fitEllipse(points) -> retval . @brief Fits an ellipse around a set of 2D points. . . The function calculates the ellipse that fits (in a least-squares sense) a set of 2D points best of . all. It returns the rotated rectangle in which the ellipse is inscribed. The first algorithm described by @cite Fitzgibbon95 . is used. Developer should keep in mind that it is possible that the returned . ellipse/rotatedRect data contains negative indices, due to the data points being close to the . border of the containing Mat element. . . @param points Input 2D point set, stored in std::vector<> or Mat
-
static
gbvision.utils.shapes.base_line module¶
-
class
gbvision.utils.shapes.base_line.BaseLine[source]¶ Bases:
gbvision.utils.shapes.base_shape.BaseShape-
static
angle(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]]) → Union[int, float][source]¶ Calculates the angle at (x0 + 1, y0), (x0, y0), (x1, y1)
Parameters: shape – The line who’s angle is needed Returns: The angle of the line (between -pi and pi)
-
classmethod
angle_positive(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]]) → Union[int, float][source]¶ Calculates the angle at (x0 + 1, y0), (x0, y0), (x1, y1) Will always return a positive value
Parameters: shape – The line who’s angle is needed Returns: The angle of the line (between 0 and 2*pi)
-
classmethod
area(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]]) → Union[int, float][source]¶ calculates the area of the shape
Parameters: shape – the shape Returns: the area of the shape
-
static
center(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]]) → Tuple[Union[int, float], Union[int, float]][source]¶ calculates the center-of-mass of the shape
Parameters: shape – the shape Returns: the center of the shape
-
classmethod
collision(shape1: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]], shape2: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]]) → bool[source]¶ checks if the two shapes are colliding
Parameters: - shape1 – the first shape
- shape2 – the second shape
Returns: True if the shapes are colliding, False otherwise
-
static
from_bounding_rect(bounding_rect: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]][source]¶ Converts a rect to the closest possible object of this shape that is contained in it
Parameters: bounding_rect – The rect to convert Returns: A shape of this type that is as close as possible to this rect
-
static
from_contour(cnt: numpy.ndarray) → Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]][source]¶ Converts a single contour to this shape
Parameters: cnt – The contour Returns: A shape representing this contour
-
static
infinite_line_params(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]]) → Tuple[Union[int, float], Union[int, float]][source]¶ Calculates the parameters (m, b) such that the function y = m * x + b contains the entire given line
Parameters: shape – The line to find Returns: A tuple of (m, b)
-
classmethod
set_center(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]], new_center: Tuple[Union[int, float], Union[int, float]]) → Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]][source]¶ Returns an identical copy that has been moved to a new location Only the center should be different
Parameters: - shape – The shape
- new_center – The new center, calling BaseShape.center on the return value should result in this
Returns: A copy of this shape, with the new center
-
static
to_bounding_rect(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]]]) → Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]][source]¶ Finds the bounding rect of this shape
Parameters: shape – The shape Returns: The bounding rect of this shape, as a gbvision.Rect
-
static
gbvision.utils.shapes.base_point module¶
-
class
gbvision.utils.shapes.base_point.BasePoint[source]¶ Bases:
gbvision.utils.shapes.base_shape.BaseShape-
classmethod
area(shape: Tuple[Union[int, float], Union[int, float]]) → Union[int, float][source]¶ calculates the area of the shape
Parameters: shape – the shape Returns: the area of the shape
-
static
center(shape: Tuple[Union[int, float], Union[int, float]]) → Tuple[Union[int, float], Union[int, float]][source]¶ calculates the center-of-mass of the shape
Parameters: shape – the shape Returns: the center of the shape
-
static
collision(shape1: Tuple[Union[int, float], Union[int, float]], shape2: Tuple[Union[int, float], Union[int, float]]) → bool[source]¶ checks if the two shapes are colliding
Parameters: - shape1 – the first shape
- shape2 – the second shape
Returns: True if the shapes are colliding, False otherwise
-
static
from_bounding_rect(bounding_rect: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → Tuple[Union[int, float], Union[int, float]][source]¶ Converts a rect to the closest possible object of this shape that is contained in it
Parameters: bounding_rect – The rect to convert Returns: A shape of this type that is as close as possible to this rect
-
static
from_contour(cnt: numpy.ndarray) → Tuple[Union[int, float], Union[int, float]][source]¶ Converts a single contour to this shape
Parameters: cnt – The contour Returns: A shape representing this contour
-
static
rotate(shape: Tuple[Union[int, float], Union[int, float]], angle: Union[int, float]) → Tuple[Union[int, float], Union[int, float]][source]¶ Rotates the point around the (0, 0) point by the given angle
Parameters: - shape – The point to rotate
- angle – The angle by which to rotate
Returns: The point after rotation
-
static
set_center(shape: Tuple[Union[int, float], Union[int, float]], new_center: Tuple[Union[int, float], Union[int, float]]) → Tuple[Union[int, float], Union[int, float]][source]¶ Returns an identical copy that has been moved to a new location Only the center should be different
Parameters: - shape – The shape
- new_center – The new center, calling BaseShape.center on the return value should result in this
Returns: A copy of this shape, with the new center
-
classmethod
gbvision.utils.shapes.base_polygon module¶
gbvision.utils.shapes.base_rect module¶
-
class
gbvision.utils.shapes.base_rect.BaseRect[source]¶ Bases:
gbvision.utils.shapes.base_shape.BaseShape-
classmethod
area(shape: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → Union[int, float][source]¶ calculates the area of the shape
Parameters: shape – the shape Returns: the area of the shape
-
static
center(shape: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → Tuple[Union[int, float], Union[int, float]][source]¶ calculates the center-of-mass of the shape
Parameters: shape – the shape Returns: the center of the shape
-
static
collision(shape1: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]], shape2: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → bool[source]¶ checks if the two shapes are colliding
Parameters: - shape1 – the first shape
- shape2 – the second shape
Returns: True if the shapes are colliding, False otherwise
-
static
from_bounding_rect(bounding_rect: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]][source]¶ Converts a rect to the closest possible object of this shape that is contained in it
Parameters: bounding_rect – The rect to convert Returns: A shape of this type that is as close as possible to this rect
-
from_contour()¶ boundingRect(array) -> retval . @brief Calculates the up-right bounding rectangle of a point set or non-zero pixels of gray-scale image. . . The function calculates and returns the minimal up-right bounding rectangle for the specified point set or . non-zero pixels of gray-scale image. . . @param array Input gray-scale image or 2D point set, stored in std::vector or Mat.
-
classmethod
set_center(shape: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]], new_center: Tuple[Union[int, float], Union[int, float]]) → Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]][source]¶ Returns an identical copy that has been moved to a new location Only the center should be different
Parameters: - shape – The shape
- new_center – The new center, calling BaseShape.center on the return value should result in this
Returns: A copy of this shape, with the new center
-
static
to_bounding_rect(shape: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]][source]¶ Finds the bounding rect of this shape
Parameters: shape – The shape Returns: The bounding rect of this shape, as a gbvision.Rect
-
classmethod
gbvision.utils.shapes.base_rotated_rect module¶
-
class
gbvision.utils.shapes.base_rotated_rect.BaseRotatedRect[source]¶ Bases:
gbvision.utils.shapes.base_shape.BaseShape-
classmethod
area(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]], float]) → Union[int, float][source]¶ calculates the area of the shape
Parameters: shape – the shape Returns: the area of the shape
-
static
center(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]], float]) → Tuple[Union[int, float], Union[int, float]][source]¶ calculates the center-of-mass of the shape
Parameters: shape – the shape Returns: the center of the shape
-
static
collision(shape1: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]], float], shape2: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]], float]) → bool[source]¶ checks if the two shapes are colliding
Parameters: - shape1 – the first shape
- shape2 – the second shape
Returns: True if the shapes are colliding, False otherwise
-
static
from_bounding_rect(bounding_rect: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]], float][source]¶ Converts a rect to the closest possible object of this shape that is contained in it
Parameters: bounding_rect – The rect to convert Returns: A shape of this type that is as close as possible to this rect
-
from_contour()¶ minAreaRect(points) -> retval . @brief Finds a rotated rectangle of the minimum area enclosing the input 2D point set. . . The function calculates and returns the minimum-area bounding rectangle (possibly rotated) for a . specified point set. Developer should keep in mind that the returned RotatedRect can contain negative . indices when data is close to the containing Mat element boundary. . . @param points Input vector of 2D points, stored in std::vector<> or Mat
-
static
set_center(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]], float], new_center: Tuple[Union[int, float], Union[int, float]]) → Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]], float][source]¶ Returns an identical copy that has been moved to a new location Only the center should be different
Parameters: - shape – The shape
- new_center – The new center, calling BaseShape.center on the return value should result in this
Returns: A copy of this shape, with the new center
-
static
to_bounding_rect(shape: Tuple[Tuple[Union[int, float], Union[int, float]], Tuple[Union[int, float], Union[int, float]], float]) → Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]][source]¶ Finds the bounding rect of this shape
Parameters: shape – The shape Returns: The bounding rect of this shape, as a gbvision.Rect
-
classmethod
gbvision.utils.shapes.base_shape module¶
-
class
gbvision.utils.shapes.base_shape.BaseShape[source]¶ Bases:
abc.ABC-
static
area(shape: Shape) → Union[int, float][source]¶ calculates the area of the shape
Parameters: shape – the shape Returns: the area of the shape
-
static
center(shape: Shape) → Tuple[Union[int, float], Union[int, float]][source]¶ calculates the center-of-mass of the shape
Parameters: shape – the shape Returns: the center of the shape
-
static
collision(shape1: Shape, shape2: Shape) → bool[source]¶ checks if the two shapes are colliding
Parameters: - shape1 – the first shape
- shape2 – the second shape
Returns: True if the shapes are colliding, False otherwise
-
classmethod
distance(shape1: Shape, shape2: Shape) → Union[int, float][source]¶ Finds the square of the distance between the centers of the shapes
Parameters: - shape1 – The first shape
- shape2 – The second shape
Returns: The square of distance between the two centers
-
classmethod
distance_squared(shape1: Shape, shape2: Shape) → Union[int, float][source]¶ Finds the square of the distance between the centers of the shapes
Parameters: - shape1 – The first shape
- shape2 – The second shape
Returns: The square of distance between the two centers
-
classmethod
draw(frame: Optional[numpy.ndarray], shape: Shape, color: Tuple[int, int, int], *args, **kwargs) → Optional[numpy.ndarray][source]¶ Draws the given shape on the frame This method does not change to original frame, and instead creates a copy of it and returns the copy
Parameters: - frame – The frame to draw on
- shape – The shape to draw
- color – The color to use
- args – Optional arguments to pass to the drawing function
- kwargs – Optional keyword arguments to pass to the drawing function
Returns: A copy of the frame with the shape drawn on it
-
classmethod
draw_multiple(frame: Optional[numpy.ndarray], shapes: List[Shape], color: Tuple[int, int, int], *args, **kwargs) → Optional[numpy.ndarray][source]¶ Draws the given shapes on the frame This method does not change to original frame, and instead creates a copy of it and returns the copy
Parameters: - frame – The frame to draw on
- shapes – The list of shapes to draw
- color – The color to use
- args – Optional arguments to pass to the drawing function
- kwargs – Optional keyword arguments to pass to the drawing function
Returns: A copy of the frame with the shape drawn on it
-
classmethod
filter_inners(shapes: List[Shape]) → List[Shape][source]¶ filters out all shapes that are colliding with a shape with a smaller index in the given list returns a list of all shapes that aren’t colliding
Parameters: shapes – the list of shapes Returns: the filtered list of shapes
-
static
from_bounding_rect(bounding_rect: Tuple[Union[int, float], Union[int, float], Union[int, float], Union[int, float]]) → Shape[source]¶ Converts a rect to the closest possible object of this shape that is contained in it
Parameters: bounding_rect – The rect to convert Returns: A shape of this type that is as close as possible to this rect
-
static
from_contour(cnt: numpy.ndarray) → Shape[source]¶ Converts a single contour to this shape
Parameters: cnt – The contour Returns: A shape representing this contour
-
classmethod
from_contours(cnts: List[numpy.ndarray]) → List[Shape][source]¶ Converts the given list of contours to a list of this shape
Parameters: cnts – The contours to convert Returns: A list of this shape, the same length as the contours list
-
classmethod
from_contours_sorted(cnts: List[numpy.ndarray]) → List[Shape][source]¶ Converts the given list of contours to a list of this shape, and sorts them by size
Parameters: cnts – The contours to convert Returns: A list of this shape, the same length as the contours list
-
classmethod
root_area(shape: Shape) → Union[int, float][source]¶ calculates the square root of the area of the shape default is the square root of cls.shape_area, but it can be overridden in case there is a simpler way (for example for circles)
Parameters: shape – the shape Returns: the square root of the area of the shape
-
static
set_center(shape: Shape, new_center: Tuple[Union[int, float], Union[int, float]]) → Shape[source]¶ Returns an identical copy that has been moved to a new location Only the center should be different
Parameters: - shape – The shape
- new_center – The new center, calling BaseShape.center on the return value should result in this
Returns: A copy of this shape, with the new center
-
classmethod
sort(shapes: List[Shape]) → List[Shape][source]¶ sorts the list of shapes by area, should be overridden to use area root in case it’s better doesn’t modify the given list, returns a new list
Parameters: shapes – the list of shapes to sort Returns: a sorted copy of the list of shapes
-
static