SPPAS 4.22

https://sppas.org/

Module sppas.src.anndata

Class sppasFuzzyRect

Description

Data structure to represent an area (x,y,w,h) with a radius (r).

Mainly used to represent a rectangle in an image with a vagueness around the midpoint, which is a rectangle. The radius is half of the vagueness.

Constructor

Create a sppasFuzzyRect instance.

The given coordinates of the midpoint can be a tuple of int values or a string representing it.

Parameters
  • coord: (int,int,int,int) x,y,w,h coords of the midpoint value.
  • radius: (int) the radius around the midpoint.
View Source
def __init__(self, coord, radius=None):
    """Create a sppasFuzzyRect instance.

    The given coordinates of the midpoint can be a tuple of int values
    or a string representing it.

    :param coord: (int,int,int,int) x,y,w,h coords of the midpoint value.
    :param radius: (int) the radius around the midpoint.

    """
    super(sppasFuzzyRect, self).__init__()
    self.__x = 0
    self.__y = 0
    self.__w = 0
    self.__h = 0
    self.__radius = None
    self.set_midpoint(coord)
    self.set_radius(radius)

Public functions

set

Set self members from another sppasFuzzyRect instance.

Parameters
  • other: (sppasFuzzyRect)
View Source
def set(self, other):
    """Set self members from another sppasFuzzyRect instance.

        :param other: (sppasFuzzyRect)

        """
    if isinstance(other, sppasFuzzyRect) is False:
        raise AnnDataTypeError(other, 'sppasFuzzyRect')
    self.__radius = None
    self.set_midpoint(other.get_midpoint())
    self.set_radius(other.get_radius())

copy

Return a deep copy of self.

View Source
def copy(self):
    """Return a deep copy of self."""
    return sppasFuzzyRect((self.__x, self.__y, self.__w, self.__h), self.__radius)

get_midpoint

Return the midpoint coords (x,y,w,h).

View Source
def get_midpoint(self):
    """Return the midpoint coords (x,y,w,h)."""
    return (self.__x, self.__y, self.__w, self.__h)

parse

Return a tuple (x,y,w,h) or (x,y,w,h,r).

Parameters
  • str_rect: (str) A string representing a fuzzy rect.
View Source
@staticmethod
def parse(str_rect):
    """Return a tuple (x,y,w,h) or (x,y,w,h,r).

        :param str_rect: (str) A string representing a fuzzy rect.

        """
    tc = str_rect.lower()[1:-1]
    tab = tc.split(',')
    x = int(tab[0])
    y = int(tab[1])
    w = int(tab[2])
    h = int(tab[3])
    if len(tab) == 5:
        return (x, y, w, h, int(tab[4]))
    return (x, y, w, h, None)

set_midpoint

Set the midpoint value.

Parameters
  • midpoint: (tuple(int,int,int,int), str) the new midpoint coords.
Raises

AnnDataTypeError

View Source
def set_midpoint(self, midpoint):
    """Set the midpoint value.

        :param midpoint: (tuple(int,int,int,int), str) the new midpoint coords.
        :raise: AnnDataTypeError

        """
    if isinstance(midpoint, str) is True:
        x, y, w, h, r = sppasFuzzyRect.parse(midpoint)
        midpoint = (x, y, w, h)
    self.__check_coords(midpoint)
    self.__x = int(midpoint[0])
    self.__y = int(midpoint[1])
    self.__w = int(midpoint[2])
    self.__h = int(midpoint[3])

get_radius

Return the radius value (float or None).

View Source
def get_radius(self):
    """Return the radius value (float or None)."""
    return self.__radius

set_radius

Fix the radius value, ie. the vagueness of the point.

The midpoint value must be set first.

Parameters
  • radius: (int, str, None) the radius value
Raises

AnnDataTypeError, AnnDataNegValueError

View Source
def set_radius(self, radius=None):
    """Fix the radius value, ie. the vagueness of the point.

        The midpoint value must be set first.

        :param radius: (int, str, None) the radius value
        :raise: AnnDataTypeError, AnnDataNegValueError

        """
    if radius is not None:
        if isinstance(radius, str) is True:
            try:
                radius = int(radius)
            except ValueError:
                raise AnnDataTypeError(radius, 'int')
        if isinstance(radius, int) is False:
            raise AnnDataTypeError(radius, 'int')
    self.__radius = radius

contains

Check if the given point is inside the vagueness of self.

Parameters
  • coord: (tuple) An (x, y) coordinates
View Source
def contains(self, coord):
    """Check if the given point is inside the vagueness of self.

        :param coord: (tuple) An (x, y) coordinates

        """
    p = sppasFuzzyPoint(coord)
    cx, cy = p.get_midpoint()
    r = self.__radius
    if self.__radius is None:
        r = 0
    if cx < self.__x - r:
        return False
    if cx > self.__x + self.__w + r:
        return False
    if cy < self.__y - r:
        return False
    if cy > self.__y + self.__h + r:
        return False
    return True

Protected functions

__check_coords

View Source
def __check_coords(self, midpoint):
    if isinstance(midpoint, tuple) is False:
        raise AnnDataTypeError(midpoint, 'tuple')
    if len(midpoint) != 4:
        raise AnnDataTypeError(midpoint, 'tuple(int, int, int, int)')
    for i in range(4):
        if isinstance(midpoint[i], (int, float)) is False:
            raise AnnDataTypeError(midpoint, 'tuple(int, int, int, int)')
        if midpoint[i] < 0:
            raise AnnDataNegValueError(midpoint[i])

Overloads

__format__

View Source
def __format__(self, fmt):
    return str(self).__format__(fmt)

__repr__

View Source
def __repr__(self):
    if self.__radius is None:
        return 'sppasFuzzyRect: ({:d},{:d},{:d},{:d})'.format(self.__x, self.__y, self.__w, self.__h)
    return 'sppasFuzzyRect: ({:d},{:d},{:d},{:d},{:d})) '.format(self.__x, self.__y, self.__w, self.__h, self.__radius)

__str__

View Source
def __str__(self):
    if self.__radius is None:
        return '({:d},{:d},{:d},{:d})'.format(self.__x, self.__y, self.__w, self.__h)
    return '({:d},{:d},{:d},{:d},{:d})'.format(self.__x, self.__y, self.__w, self.__h, self.__radius)

__eq__

Test equality of self with other.

Two fuzzy points are equals if the midpoint of one of them is in the area of the other.

Parameters
  • other
View Source
def __eq__(self, other):
    """Test equality of self with other.

        Two fuzzy points are equals if the midpoint of one of them is in the
        area of the other.

        """
    rect = None
    if isinstance(other, str) is True:
        rect = sppasFuzzyRect.parse(other)
    elif isinstance(other, tuple) is True:
        if len(other) in (4, 5):
            rect = sppasFuzzyRect((other[0], other[1], other[2], other[3]))
            if len(other) == 5:
                rect.set_radius(other[4])
    elif isinstance(other, sppasFuzzyRect) is True:
        rect = other
    if rect is None:
        raise AnnDataTypeError('sppasFuzzyRect', 'tuple(int,int,int,int)')
    other_x, other_y, other_w, other_h = rect.get_midpoint()
    other_r = rect.get_radius()
    if other_r is None:
        other_r = 0
    if self.contains((other_x - other_r, other_y - other_r)) is True:
        return True
    if self.contains((other_x - other_r, other_y + other_h + other_r)) is True:
        return True
    if self.contains((other_x + other_w + other_r, other_y - other_r)) is True:
        return True
    if self.contains((other_x + other_w + other_r, other_y + other_h + other_r)) is True:
        return True
    return False