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.
View Source
"""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.

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

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.set_midpoint(other.get_midpoint())

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])

Return the radius value (float or None).

View Source
"""Return the radius value (float or None)."""

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

The midpoint value must be set first.

Raises

AnnDataTypeError, AnnDataNegValueError

View Source
"""Fix the radius value, ie. the vagueness of the point.

The midpoint value must be set first.

:raise: AnnDataTypeError, AnnDataNegValueError

"""
try:
except ValueError:

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 = 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])

__format__

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

__repr__

View Source
def __repr__(self):
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):
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:
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()