1from __future__
import annotations
4from typing
import Union, TypeAlias, Any, cast, TYPE_CHECKING
7import numpy.typing
as npt
12from ids_peak_icv
import lib_loader
14 peak_icv_drawing_options, peak_common_rectangle)
25PointArray: TypeAlias = npt.NDArray[np.object_]
26RegionArray: TypeAlias = npt.NDArray[np.object_]
32 Represents a region consisting of a set of points in the image plane.
34 @see @ref concept_type_region
36 @since ids_peak_icv 1.0
37 @ingroup ids_peak_icv_python_types
44 Creates a region from points.
46 @param points: NumPy Array of Point objects representing the region's points OR NumPy Array of Points [x,y]
47 @return A region instance.
49 @exception OutOfRangeException: If a point at a given index has an invalid length.
50 @exception NotSupportedException: If the parameter `points` is not a NumPy array of Point objects or
51 a two-dimensional NumPy array.
53 @since ids_peak_icv 1.0
56 c_points = create_array(peak_icv_point, size)
58 for i, p
in enumerate(points):
59 if isinstance(p, Point)
and isinstance(points, np.ndarray):
61 elif isinstance(p, np.ndarray):
66 "Point {index} has invalid length {length}".format(index=i, length=len(p)))
69 "Parameter points has to be a numpy array of Point objects or a two-dimensional numpy array.")
72 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_CreateFromPoints,
73 ctypes.pointer(handle), c_points, size)
75 return Region(_handle=handle)
81 Creates a Region from a Rectangle.
82 @param rect: Rectangle to create the region from
84 @return A region instance.
86 @exception NotSupportedException: If the contents of rectangle are not integers.
88 @since ids_peak_icv 1.0
98 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_CreateFromRectangle,
99 ctypes.pointer(handle), c_rect)
101 return Region(_handle=handle)
103 def __init__(self, *args: Any, **kwargs: Any) ->
None:
106 In order to create an _empty Region_ create it with no arguments, in all other cases please refer to the
109 @since ids_peak_icv 1.0
115 if len(args)
or len(kwargs):
116 def _check_init_for_classes_with_classmethods_only(**kwargs: Any) -> peak_icv_region_handle:
118 return cast(peak_icv_region_handle, kwargs[
"_handle"])
121 "Cannot initialize Region with arguments. Please use a classmethod or create an empty Region "
124 self.
_handle = _check_init_for_classes_with_classmethods_only(**kwargs)
127 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_Create,
128 ctypes.pointer(handle))
137 Check if two regions are equal.
139 @param other: Another Region object
141 @return True if regions are equal, False otherwise
143 @since ids_peak_icv 1.0
145 if not isinstance(other, Region):
148 if self.
_handle == other._handle:
151 is_equal = ctypes.c_bool()
152 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_Compare, self.
_handle, other._handle,
153 ctypes.pointer(is_equal))
155 return is_equal.value
160 Check if two regions are not equal.
162 @param other: Another Region object
164 @return True if regions are not equal, False otherwise
166 @since ids_peak_icv 1.0
168 return not self.
__eq__(other)
174 Get the points of the region.
176 @return List of Point objects representing the region's points.
178 @since ids_peak_icv 1.0
183 c_num_points = ctypes.c_size_t()
184 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_GetPoints_GetCount,
185 self.
_handle, ctypes.pointer(c_num_points))
187 if c_num_points.value == 0:
189 return cast(PointArray, self.
_points)
191 c_points = create_array(peak_icv_point, c_num_points.value)
192 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_GetPoints,
193 self.
_handle, c_points, c_num_points)
195 iterable = (
Point(p.x, p.y)
for p
in c_points)
198 self.
_points = np.empty(len(c_points), dtype=Point)
199 for i, it
in enumerate(iterable):
202 return cast(PointArray, self.
_points)
208 Calculate the connected components of the region.
210 The 8-neighborhood is used to determine the components.
212 @see @ref concept_region_connected-components
214 @return List of connected components as Region objects.
216 @since ids_peak_icv 1.0
221 c_num_regions = ctypes.c_size_t()
222 execute_and_map_return_codes(
223 lib_loader.dll().peak_icv_Region_GetConnectedComponents_GetCount,
224 self.
_handle, ctypes.pointer(c_num_regions))
226 if c_num_regions.value == 0:
230 c_regions = create_array(peak_icv_region_handle, c_num_regions.value)
232 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_Array_Create, c_regions,
235 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_GetConnectedComponents,
236 self.
_handle, c_regions, c_num_regions)
237 iterable = (
Region(_handle=h)
for h
in c_regions)
238 except ICVException
as e:
239 lib_loader.dll().peak_icv_Region_Array_Destroy(c_regions, c_num_regions)
244 for i, it
in enumerate(iterable):
253 Calculate the area of the region. The area is the number of pixels in the region.
255 @return Area of the region.
257 @since ids_peak_icv 1.0
259 c_area = ctypes.c_size_t()
260 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_GetArea,
261 self.
_handle, ctypes.pointer(c_area))
269 Calculate the center of gravity of the region.
271 @return Center of gravity as a PointF object.
273 @exception MathErrorException: If the center of gravity could not be computed.
275 @since ids_peak_icv 1.0
278 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_GetCenterOfGravity,
279 self.
_handle, ctypes.pointer(c_center_of_gravity))
281 return Point(c_center_of_gravity.x, c_center_of_gravity.y)
286 Computes the intersection of two regions.
288 @return The intersection of two regions.
290 @since ids_peak_icv 1.0
293 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_Intersection,
294 self.
_handle, other_region._handle, ctypes.pointer(output_region._handle))
301 Computes the difference of two regions.
303 @return The difference of two regions.
305 @since ids_peak_icv 1.0
308 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_Difference,
309 self.
_handle, other_region._handle, ctypes.pointer(output_region._handle))
313 def union(self, other_region: Region) -> Region:
316 Computes the union of two regions.
318 @return The united region.
320 @since ids_peak_icv 1.0
323 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_Union,
324 self.
_handle, other_region._handle, ctypes.pointer(output_region._handle))
328 def dilation(self, structuring_element: Region) -> Region:
331 Computes the dilation of the input region with the given structuring element.
333 The reference point of the structuring element is at (0, 0).
335 @param structuring_element:
337 @return The dilated region.
339 @since ids_peak_icv 1.0
342 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_Dilation, self.
_handle,
343 structuring_element._handle, ctypes.pointer(output_region._handle))
346 def erosion(self, structuring_element: Region) -> Region:
349 Computes the erosion of the input region with the given structuring element.
351 The reference point of the structuring element is at (0, 0).
353 @param structuring_element:
355 @return The eroded region.
357 @since ids_peak_icv 1.0
360 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_Erosion, self.
_handle,
361 structuring_element._handle, ctypes.pointer(output_region._handle))
367 Destructor for Region class.
370 lib_loader.dll().peak_icv_Region_Destroy(self.
_handle)
372 def _draw(self, image: Image, drawing_options: DrawingOptions) ->
None:
375 Only region points inside the image are painted.
376 Region points outside the image are ignored. In case of an empty region, the image is not modified.
378 @since ids_peak_icv 1.0
383 drawing_options.opacity.value)
385 _image = _check_and_get_icv_image(image)
387 execute_and_map_return_codes(lib_loader.dll().peak_icv_Region_Draw, _image._handle,
388 self.
_handle, drawing_options_c)
Represents a point in 2D space with x and y coordinates.
Represents a region consisting of a set of points in the image plane.
np.ndarray|None _connected_components
int area(self)
Calculate the area of the region.
None __init__(self, *Any args, **Any kwargs)
In order to create an empty Region create it with no arguments, in all other cases please refer to th...
bool __eq__(self, object other)
Check if two regions are equal.
Region union(self, Region other_region)
Computes the union of two regions.
Point center_of_gravity(self)
Calculate the center of gravity of the region.
Region dilation(self, Region structuring_element)
Computes the dilation of the input region with the given structuring element.
RegionArray connected_components(self)
Calculate the connected components of the region.
Region intersection(self, Region other_region)
Computes the intersection of two regions.
None __del__(self)
Destructor for Region class.
Region create_from_points(cls, Union[PointArray, npt.NDArray[np.int_]] points)
Creates a region from points.
Region create_from_rectangle(cls, Rectangle rect)
Creates a Region from a Rectangle.
Region difference(self, Region other_region)
Computes the difference of two regions.
PointArray points(self)
Get the points of the region.
Region erosion(self, Region structuring_element)
Computes the erosion of the input region with the given structuring element.
None _draw(self, Image image, DrawingOptions drawing_options)
Only region points inside the image are painted.
bool __ne__(self, object other)
Check if two regions are not equal.
Thrown when an operation cannot be performed due to logical constraints.
Thrown when a requested operation is not implemented or supported.
Thrown when a value is outside the allowed range.
Interface for drawable objects.