1from __future__
import annotations
4from typing
import List, Tuple, Union, TypeAlias, Callable, cast
7import numpy.typing
as npt
9from ids_peak_icv
import lib_loader
12 peak_common_rectangle_f, peak_icv_region_handle)
20RegionArray: TypeAlias = npt.NDArray[np.object_]
26 Represents a selector for regions based on various criteria.
28 @ingroup ids_peak_icv_python_selectors
29 @since ids_peak_icv 1.1
32 def __init__(self, regions: RegionArray) ->
None:
35 Creates a region selector.
37 It is possible to provide an empty numpy array (no regions),
38 but it does not make sense, because in this case no regions can be selected.
40 @param regions: List of Region objects to select from.
42 @exception NotSupportedException: If regions is not a numpy array.
44 @since ids_peak_icv 1.1
47 if not isinstance(regions, np.ndarray):
56 Get the selected regions.
58 @return List[Region]: List of selected Region objects.
60 @since ids_peak_icv 1.1
67 Select regions based on their area.
69 @param interval: Interval to select by. Only positive values are allowed.
71 @return RegionSelector: New RegionSelector object with selected regions.
73 @exception NotSupportedException: If interval has a wrong type or the interval is not valid for this function.
74 @exception NotPossibleException: If interval has atleast one negative component.
76 @since ids_peak_icv 1.1
79 if not isinstance(interval, Interval):
81 "The given interval is invalid. It has to be an Interval, a Tuple(int, int) or a List "
84 if interval._is_negative():
85 raise NotPossibleException(
"The given interval is negative. You can not select a negative area.")
89 return self.
_select_by(lib_loader.dll().peak_icv_Region_SelectByArea_GetCount,
90 lib_loader.dll().peak_icv_Region_SelectByArea,
94 Interval, Tuple[float, float], List[float], Tuple[int, int], List[int]]) -> RegionSelector:
97 Select regions based on the center of gravity's x coordinate.
99 @param interval: Interval to select by.
101 @return RegionSelector: New RegionSelector object with selected regions.
103 @exception NotSupportedException: If interval has a wrong type or the interval is not valid for this function.
105 @since ids_peak_icv 1.1
108 if (isinstance(interval, tuple)
or isinstance(interval, list))
and len(interval) == 2:
111 if not isinstance(interval, Interval):
113 "The given interval is invalid. It has to be an Interval,"
114 "a Tuple(float | int, float | int) or a List with two float | int")
118 return self.
_select_by(lib_loader.dll().peak_icv_Region_SelectByCenterOfGravityX_GetCount,
119 lib_loader.dll().peak_icv_Region_SelectByCenterOfGravityX,
123 Interval, Tuple[float, float], List[float], Tuple[int, int], List[int]]) -> RegionSelector:
126 Select regions based in the center of gravity's y coordinate.
128 @param interval: Interval to select by.
130 @return RegionSelector: New RegionSelector object with selected regions.
132 @exception NotSupportedException: If interval has a wrong type or the interval is not valid for this function.
134 @since ids_peak_icv 1.1
136 if (isinstance(interval, tuple)
or isinstance(interval, list))
and len(interval) == 2:
139 if not isinstance(interval, Interval):
141 "The given interval is invalid. It has to be an Interval,"
142 "a Tuple(float | int, float | int) or a List with two float | int")
146 return self.
_select_by(lib_loader.dll().peak_icv_Region_SelectByCenterOfGravityY_GetCount,
147 lib_loader.dll().peak_icv_Region_SelectByCenterOfGravityY,
151 Rectangle, Tuple[float, float, float, float], List[float], Tuple[int, int, int, int], List[
152 int]]) -> RegionSelector:
155 Select regions based in the center of gravity's position within a rectangle.
157 @param rect: Rectangle to select by.
159 @return RegionSelector: New RegionSelector object with selected regions.
161 @exception NotSupportedException: If interval has a wrong type or the interval is not valid for this function.
163 @since ids_peak_icv 1.1
166 if (isinstance(rect, tuple)
or isinstance(rect, list))
and len(rect) == 4:
168 Rectangle.create_from_coordinates_and_dimensions(
169 rect[0], rect[1], rect[2], rect[3]))
171 if not isinstance(rect, Rectangle):
173 "The given interval is invalid. It has to be an Interval,"
174 "a Tuple(float | int, float | int, float | int, float | int) or a List with four float | int")
178 return self.
_select_by(lib_loader.dll().peak_icv_Region_SelectByCenterOfGravityRect_GetCount,
179 lib_loader.dll().peak_icv_Region_SelectByCenterOfGravityRect,
182 def _select_by(self, select_by_get_count_function: Callable, select_by_function: Callable,
183 c_interval: ctypes.Structure) -> RegionSelector:
187 handles = create_array(peak_icv_region_handle, len(self.
regionsregions))
190 c_count = ctypes.c_size_t()
191 execute_and_map_return_codes(select_by_get_count_function, handles, len(handles), c_interval,
192 ctypes.pointer(c_count))
194 if c_count.value == 0:
197 selected_handles = create_array(peak_icv_region_handle, c_count.value)
198 execute_and_map_return_codes(select_by_function, handles, len(handles), c_interval,
200 len(selected_handles))
202 iterable = (
Region(_handle=regionHandle)
for regionHandle
in selected_handles)
204 selected_regions = np.empty(len(selected_handles), dtype=Region)
205 for i, it
in enumerate(iterable):
206 selected_regions[i] = it
Represents an interval with lower and upper boundaries.
Represents a region consisting of a set of points in the image plane.
Thrown when an operation cannot be performed due to logical constraints.
Thrown when a requested operation is not implemented or supported.
Represents a selector for regions based on various criteria.
RegionSelector select_by_center_of_gravity_x(self, Union[Interval, Tuple[float, float], List[float], Tuple[int, int], List[int]] interval)
Select regions based on the center of gravity's x coordinate.
RegionSelector select_by_center_of_gravity_y(self, Union[Interval, Tuple[float, float], List[float], Tuple[int, int], List[int]] interval)
Select regions based in the center of gravity's y coordinate.
None __init__(self, RegionArray regions)
Creates a region selector.
RegionSelector _select_by(self, Callable select_by_get_count_function, Callable select_by_function, ctypes.Structure c_interval)
RegionSelector select_by_area(self, Interval interval)
Select regions based on their area.
RegionArray regions(self)
Get the selected regions.
RegionSelector select_by_center_of_gravity_rect(self, Union[Rectangle, Tuple[float, float, float, float], List[float], Tuple[int, int, int, int], List[int]] rect)
Select regions based in the center of gravity's position within a rectangle.