Coretex
coretex.coretex.annotation.image.coretex_format.CoretexSegmentationInstance Class Reference
Inheritance diagram for coretex.coretex.annotation.image.coretex_format.CoretexSegmentationInstance:
coretex.codable.codable.Codable

Public Member Functions

Self create (cls, UUID classId, BBox bbox, List[SegmentationType] segmentations)
 
np.ndarray extractSegmentationMask (self, int width, int height)
 
np.ndarray extractBinaryMask (self, int width, int height)
 
Tuple[float, float] centroid (self)
 
None centerSegmentations (self, Tuple[float, float] newCentroid)
 
None rotateSegmentations (self, int degrees)
 
- Public Member Functions inherited from coretex.codable.codable.Codable
Dict[str, Any] encode (self)
 
None onDecode (self)
 
Self decode (cls, Dict[str, Any] encodedObject)
 

Detailed Description

    Segmentation Instance class

    Properties
    ----------
    classID : UUID
        uuid of class
    bbox : BBox
        Bounding Box as a python class
    segmentations : List[SegmentationType]
        list of segmentations that define the precise boundaries of object

Definition at line 44 of file coretex_format.py.

Member Function Documentation

◆ centerSegmentations()

None coretex.coretex.annotation.image.coretex_format.CoretexSegmentationInstance.centerSegmentations (   self,
Tuple[float, float]  newCentroid 
)
    Centers segmentations to the specified center point

    Parameters
    ----------
    newCentroid : Tuple[float, float]
x, y coordinates of centroid

Definition at line 167 of file coretex_format.py.

167  def centerSegmentations(self, newCentroid: Tuple[float, float]) -> None:
168  """
169  Centers segmentations to the specified center point
170 
171  Parameters
172  ----------
173  newCentroid : Tuple[float, float]
174  x, y coordinates of centroid
175  """
176 
177  newCenterX, newCenterY = newCentroid
178  oldCenterX, oldCenterY = self.centroid()
179 
180  modifiedSegmentations: List[List[float]] = []
181 
182  for segmentation in self.segmentations:
183  modifiedSegmentation: List[float] = []
184 
185  for i in range(0, len(segmentation), 2):
186  x = segmentation[i] + (newCenterX - oldCenterX)
187  y = segmentation[i+1] + (newCenterY - oldCenterY)
188 
189  modifiedSegmentation.append(x)
190  modifiedSegmentation.append(y)
191 
192  modifiedSegmentations.append(modifiedSegmentation)
193 
194  self.segmentations = modifiedSegmentations
195 

◆ centroid()

Tuple[float, float] coretex.coretex.annotation.image.coretex_format.CoretexSegmentationInstance.centroid (   self)
    Calculates centroid of segmentations

    Returns
    -------
    Tuple[float, float] -> x, y coordinates of centroid

Definition at line 148 of file coretex_format.py.

148  def centroid(self) -> Tuple[float, float]:
149  """
150  Calculates centroid of segmentations
151 
152  Returns
153  -------
154  Tuple[float, float] -> x, y coordinates of centroid
155  """
156 
157  flattenedSegmentations = [element for sublist in self.segmentations for element in sublist]
158 
159  listCX = [value for index, value in enumerate(flattenedSegmentations) if index % 2 == 0]
160  centerX = fsum(listCX) / len(listCX)
161 
162  listCY = [value for index, value in enumerate(flattenedSegmentations) if index % 2 != 0]
163  centerY = fsum(listCY) / len(listCY)
164 
165  return centerX, centerY
166 

◆ create()

Self coretex.coretex.annotation.image.coretex_format.CoretexSegmentationInstance.create (   cls,
UUID  classId,
BBox  bbox,
List[SegmentationType]  segmentations 
)
    Creates CoretexSegmentationInstance object with provided parameters

    Parameters
    ----------
    classID : UUID
uuid of class
    bbox : BBox
Bounding Box as a python class
    segmentations : List[SegmentationType]
list of segmentations that define the precise boundaries of object

    Returns
    -------
    The created CoretexSegmentationInstance object

Definition at line 74 of file coretex_format.py.

74  def create(cls, classId: UUID, bbox: BBox, segmentations: List[SegmentationType]) -> Self:
75  """
76  Creates CoretexSegmentationInstance object with provided parameters
77 
78  Parameters
79  ----------
80  classID : UUID
81  uuid of class
82  bbox : BBox
83  Bounding Box as a python class
84  segmentations : List[SegmentationType]
85  list of segmentations that define the precise boundaries of object
86 
87  Returns
88  -------
89  The created CoretexSegmentationInstance object
90  """
91 
92  obj = cls()
93 
94  obj.classId = classId
95  obj.bbox = bbox
96  obj.segmentations = segmentations
97 
98  return obj
99 

◆ extractBinaryMask()

np.ndarray coretex.coretex.annotation.image.coretex_format.CoretexSegmentationInstance.extractBinaryMask (   self,
int  width,
int  height 
)
    Works the same way as extractSegmentationMask function
    Values that are > 0 are capped to 1

    Parameters
    ----------
    width : int
width of image in pixels
    height : int
height of image in pixels

    Returns
    -------
    np.ndarray -> binary segmentation mask represented as np.ndarray

Definition at line 126 of file coretex_format.py.

126  def extractBinaryMask(self, width: int, height: int) -> np.ndarray:
127  """
128  Works the same way as extractSegmentationMask function
129  Values that are > 0 are capped to 1
130 
131  Parameters
132  ----------
133  width : int
134  width of image in pixels
135  height : int
136  height of image in pixels
137 
138  Returns
139  -------
140  np.ndarray -> binary segmentation mask represented as np.ndarray
141  """
142 
143  binaryMask = self.extractSegmentationMask(width, height)
144  binaryMask[binaryMask > 0] = 1
145 
146  return binaryMask
147 

◆ extractSegmentationMask()

np.ndarray coretex.coretex.annotation.image.coretex_format.CoretexSegmentationInstance.extractSegmentationMask (   self,
int  width,
int  height 
)
    Generates segmentation mask based on provided
    width and height of image\n
    Pixel values are equal to class IDs

    Parameters
    ----------
    width : int
width of image in pixels
    height : int
height of image in pixels

    Returns
    -------
    np.ndarray -> segmentation mask represented as np.ndarray

Definition at line 100 of file coretex_format.py.

100  def extractSegmentationMask(self, width: int, height: int) -> np.ndarray:
101  """
102  Generates segmentation mask based on provided
103  width and height of image\n
104  Pixel values are equal to class IDs
105 
106  Parameters
107  ----------
108  width : int
109  width of image in pixels
110  height : int
111  height of image in pixels
112 
113  Returns
114  -------
115  np.ndarray -> segmentation mask represented as np.ndarray
116  """
117 
118  image = Image.new("L", (width, height))
119 
120  for segmentation in self.segmentations:
121  draw = ImageDraw.Draw(image)
122  draw.polygon(toPoly(segmentation), fill = 1)
123 
124  return np.asarray(image)
125 

◆ rotateSegmentations()

None coretex.coretex.annotation.image.coretex_format.CoretexSegmentationInstance.rotateSegmentations (   self,
int  degrees 
)
    Rotates segmentations of CoretexSegmentationInstance object

    Parameters
    ----------
    degrees : int
degree of rotation

Definition at line 196 of file coretex_format.py.

196  def rotateSegmentations(self, degrees: int) -> None:
197  """
198  Rotates segmentations of CoretexSegmentationInstance object
199 
200  Parameters
201  ----------
202  degrees : int
203  degree of rotation
204  """
205 
206  rotatedSegmentations: List[List[float]] = []
207  centerX, centerY = self.centroid()
208 
209  # because rotations with image and segmentations doesn't go in same direction
210  # one of the rotations has to be inverted so they go in same direction
211  theta = radians(-degrees)
212  cosang, sinang = cos(theta), sin(theta)
213 
214  for segmentation in self.segmentations:
215  rotatedSegmentation: List[float] = []
216 
217  for i in range(0, len(segmentation), 2):
218  x = segmentation[i] - centerX
219  y = segmentation[i + 1] - centerY
220 
221  newX = (x * cosang - y * sinang) + centerX
222  newY = (x * sinang + y * cosang) + centerY
223 
224  rotatedSegmentation.append(newX)
225  rotatedSegmentation.append(newY)
226 
227  rotatedSegmentations.append(rotatedSegmentation)
228 
229  self.segmentations = rotatedSegmentations
230 
231 

The documentation for this class was generated from the following file: