IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
RegionSelector.cs
Go to the documentation of this file.
1using System.Runtime.InteropServices;
6
8
19public class RegionSelector
20{
26
35 {
36 Regions = regions;
37 }
38
54 {
55 return _SelectBy(CInterface.peak_icv_Region_SelectByArea_GetCount,
56 CInterface.peak_icv_Region_SelectByArea,
57 new Backend.Types.peak_icv_interval_u { minimum = interval.Minimum, maximum = interval.Maximum });
58 }
59
75 {
76 return _SelectBy(CInterface.peak_icv_Region_SelectByCenterOfGravityX_GetCount,
77 CInterface.peak_icv_Region_SelectByCenterOfGravityX,
78 new Backend.Types.peak_icv_interval_f { minimum = interval.Minimum, maximum = interval.Maximum });
79 }
80
96 {
97 return _SelectBy(CInterface.peak_icv_Region_SelectByCenterOfGravityY_GetCount,
98 CInterface.peak_icv_Region_SelectByCenterOfGravityY,
99 new Backend.Types.peak_icv_interval_f { minimum = interval.Minimum, maximum = interval.Maximum });
100 }
101
125 {
126 return _SelectBy(CInterface.peak_icv_Region_SelectByCenterOfGravityRect_GetCount,
127 CInterface.peak_icv_Region_SelectByCenterOfGravityRect,
128 new Backend.Types.peak_common_rectangle_f
129 {
130 x = rect.X,
131 y = rect.Y,
132 width = rect.Width,
133 height = rect.Height
134 });
135 }
136
156 private RegionSelector _SelectBy<T>(GetCountDelegate<T> getCountFunc, SelectByDelegate<T> selectByFunc,
157 T criteria)
158 {
159 if (Regions.Count == 0)
160 {
161 return new RegionSelector(new DisposableList<Region>());
162 }
163
164 IntPtr[] handles = SafeHandles.ExtractHandles(Regions, out Action release);
165
166 try
167 {
168 nuint count = 0;
169 DllHelper.ExecuteAndMapReturnCodes(() =>
170 getCountFunc(handles, (nuint) handles.Length, criteria, out count));
171
172 if ((uint) count == 0)
173 {
174 return new RegionSelector(new DisposableList<Region>());
175 }
176
177 var selectedHandles = new IntPtr[(uint) count];
178 DllHelper.ExecuteAndMapReturnCodes(() =>
179 selectByFunc(handles, (nuint) handles.Length, criteria, selectedHandles, count));
180
181 var selectedRegions =
182 new DisposableList<Region>(selectedHandles.Select(handle => new Region(new Region.Handle(handle)))
183 .ToList());
184
185 return new RegionSelector(selectedRegions);
186 }
187 finally
188 {
189 release();
190 }
191 }
192
193 private static class CInterface
194 {
195 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
196 public static extern Backend.Types.peak_icv_status peak_icv_Region_SelectByArea_GetCount(
197 IntPtr[] inputRegions, nuint numInputRegions, Backend.Types.peak_icv_interval_u area,
198 out nuint count);
199
200 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
201 public static extern Backend.Types.peak_icv_status peak_icv_Region_SelectByArea(
202 IntPtr[] inputRegions, nuint numInputRegions, Backend.Types.peak_icv_interval_u area,
203 [Out] IntPtr[] outputRegions,
204 nuint numOutputRegions);
205
206 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
207 public static extern Backend.Types.peak_icv_status peak_icv_Region_SelectByCenterOfGravityX_GetCount(
208 IntPtr[] inputRegions, nuint numInputRegions, Backend.Types.peak_icv_interval_f x,
209 out nuint count);
210
211 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
212 public static extern Backend.Types.peak_icv_status peak_icv_Region_SelectByCenterOfGravityX(
213 IntPtr[] inputRegions, nuint numInputRegions, Backend.Types.peak_icv_interval_f x,
214 [Out] IntPtr[] outputRegions,
215 nuint numOutputRegions);
216
217 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
218 public static extern Backend.Types.peak_icv_status peak_icv_Region_SelectByCenterOfGravityY_GetCount(
219 IntPtr[] inputRegions, nuint numInputRegions, Backend.Types.peak_icv_interval_f y,
220 out nuint count);
221
222 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
223 public static extern Backend.Types.peak_icv_status peak_icv_Region_SelectByCenterOfGravityY(
224 IntPtr[] inputRegions, nuint numInputRegions, Backend.Types.peak_icv_interval_f y,
225 [Out] IntPtr[] outputRegions,
226 nuint numOutputRegions);
227
228 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
229 public static extern Backend.Types.peak_icv_status peak_icv_Region_SelectByCenterOfGravityRect_GetCount(
230 IntPtr[] inputRegions, nuint numInputRegions, Backend.Types.peak_common_rectangle_f rect,
231 out nuint count);
232
233 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
234 public static extern Backend.Types.peak_icv_status peak_icv_Region_SelectByCenterOfGravityRect(
235 IntPtr[] inputRegions, nuint numInputRegions, Backend.Types.peak_common_rectangle_f rect,
236 [Out] IntPtr[] outputRegions, nuint numOutputRegions);
237 }
238
239 private delegate Backend.Types.peak_icv_status GetCountDelegate<in T>(IntPtr[] inputRegions,
240 nuint numInputRegions,
241 T area,
242 out nuint count);
243
244 private delegate Backend.Types.peak_icv_status SelectByDelegate<in T>(IntPtr[] inputRegions,
245 nuint numInputRegions,
246 T area,
247 [Out] IntPtr[] outputRegions, nuint numOutputRegions);
248}
Represents a rectangle with floating-point coordinates and dimensions.
Definition Rectangle.cs:175
Represents an interval with floating-point boundaries.
Definition Interval.cs:157
Represents an interval with unsigned integer boundaries.
Definition Interval.cs:137
Selects regions based on feature criteria.
RegionSelector SelectByCenterOfGravityX(IntervalF interval)
Selects regions based on the x-coordinate of the center of gravity.
RegionSelector SelectByArea(IntervalU interval)
Selects regions based on their area.
RegionSelector SelectByCenterOfGravityY(IntervalF interval)
Selects regions based on the y-coordinate of the center of gravity.
readonly DisposableList< Region > Regions
The currently selected regions.
RegionSelector SelectByCenterOfGravityRectangle(RectangleF rect)
Selects regions based on whether their center of gravity lies within the given rectangle.
RegionSelector(DisposableList< Region > regions)
Creates a RegionSelector with a collection of Region objects.
A list that automatically calls IDisposable.Dispose on all its elements when disposed.
Represents a region defined by a set of points in the image plane.
Definition Region.cs:43