IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
Region.cs
Go to the documentation of this file.
1using System.Runtime.InteropServices;
5using Microsoft.Win32.SafeHandles;
6
8
42public class Region : IDisposable, IDrawable, IHasSafeHandle
43{
44 internal Handle _handle;
45
50 public Region()
51 {
52 var handle = new Handle();
53 _handle = handle;
54 }
55
60 public Region(Point[] points)
61 {
62 Backend.Types.peak_icv_point[] cPoints = points
63 .Select(coordinate => new Backend.Types.peak_icv_point { x = coordinate.X, y = coordinate.Y })
64 .ToArray();
65 var handle = new Handle(cPoints);
66 _handle = handle;
67 }
68
73 public Region(Rectangle rectangle)
74 {
75 var cRectangle = new Backend.Types.peak_common_rectangle
76 { x = rectangle.X, y = rectangle.Y, width = rectangle.Width, height = rectangle.Height };
77 var handle = new Handle(cRectangle);
78 _handle = handle;
79 }
80
81 internal Region(Handle handle)
82 {
83 _handle = handle;
84 }
85
100 {
101 get
102 {
103 nuint numConnectedComponents = 0;
104 DllHelper.ExecuteAndMapReturnCodes(() =>
105 CInterface.peak_icv_Region_GetConnectedComponents_GetCount(_handle, out numConnectedComponents));
106
107 if (numConnectedComponents == 0)
108 {
109 return new DisposableList<Region>();
110 }
111
112 var cConnectedComponentsHandles = new IntPtr[numConnectedComponents];
113 try
114 {
115 DllHelper.ExecuteAndMapReturnCodes(() =>
116 CInterface.peak_icv_Region_Array_Create(cConnectedComponentsHandles, numConnectedComponents));
117
118 DllHelper.ExecuteAndMapReturnCodes(() =>
119 CInterface.peak_icv_Region_GetConnectedComponents(_handle, cConnectedComponentsHandles,
120 numConnectedComponents));
121
122 return
124 cConnectedComponentsHandles.Select(handle => new Region(new Handle(handle))).ToList());
125 }
126 catch (Exception)
127 {
128 CInterface.peak_icv_Region_Array_Destroy(cConnectedComponentsHandles, numConnectedComponents);
129 throw;
130 }
131 }
132 }
133
138 public List<Point> Points
139 {
140 get
141 {
142 nuint numPoints = 0;
143 DllHelper.ExecuteAndMapReturnCodes(() =>
144 CInterface.peak_icv_Region_GetPoints_GetCount(_handle, out numPoints));
145
146 if (numPoints == 0)
147 {
148 return new List<Point>();
149 }
150
151 var cPoints = new Backend.Types.peak_icv_point[numPoints];
152
153 DllHelper.ExecuteAndMapReturnCodes(() =>
154 CInterface.peak_icv_Region_GetPoints(_handle, cPoints, numPoints));
155 return cPoints.Select(cCoordinate => new Point(cCoordinate.x, cCoordinate.y)).ToList();
156 }
157 }
158
166 public uint Area
167 {
168 get
169 {
170 nuint area = 0;
171 DllHelper.ExecuteAndMapReturnCodes(() => CInterface.peak_icv_Region_GetArea(_handle, out area));
172 return (uint) area;
173 }
174 }
175
187 {
188 get
189 {
190 var cCenterOfGravity = new Backend.Types.peak_icv_point_f();
191 DllHelper.ExecuteAndMapReturnCodes(() =>
192 CInterface.peak_icv_Region_GetCenterOfGravity(_handle, out cCenterOfGravity));
193 return new PointF(cCenterOfGravity.x, cCenterOfGravity.y);
194 }
195 }
196
201 public static IEqualityComparer<Region> Comparer { get; } = new RegionEqualityComparer();
202
207 public void Dispose()
208 {
209 _handle.Dispose();
210 GC.SuppressFinalize(this);
211 }
212
213 void IDrawable.Draw(Image image, DrawingOptions drawingOptions)
214 {
215 var cOptions = new Backend.Types.peak_icv_drawing_options
216 {
217 color = drawingOptions.Color.ColorCode,
218 opacity = drawingOptions.Opacity.Value
219 };
220 DllHelper.ExecuteAndMapReturnCodes(() =>
221 CInterface.peak_icv_Region_Draw(image._handle, _handle, cOptions));
222 }
223
224 SafeHandle IHasSafeHandle.Handle => _handle;
225
234 {
235 var outputRegion = new Region();
236 DllHelper.ExecuteAndMapReturnCodes(() =>
237 CInterface.peak_icv_Region_Difference(_handle, other._handle, ref outputRegion._handle));
238
239 return outputRegion;
240 }
241
250 public Region Union(Region other)
251 {
252 var outputRegion = new Region();
253 DllHelper.ExecuteAndMapReturnCodes(() =>
254 CInterface.peak_icv_Region_Union(_handle, other._handle, ref outputRegion._handle));
255
256 return outputRegion;
257 }
258
267 {
268 var outputRegion = new Region();
269 DllHelper.ExecuteAndMapReturnCodes(() =>
270 CInterface.peak_icv_Region_Intersection(_handle, other._handle, ref outputRegion._handle));
271
272 return outputRegion;
273 }
274
285 public Region Dilation(Region structuringElement)
286 {
287 var outputRegion = new Region();
288 DllHelper.ExecuteAndMapReturnCodes(() =>
289 CInterface.peak_icv_Region_Dilation(_handle, structuringElement._handle, ref outputRegion._handle));
290
291 return outputRegion;
292 }
293
304 public Region Erosion(Region structuringElement)
305 {
306 var outputRegion = new Region();
307 DllHelper.ExecuteAndMapReturnCodes(() =>
308 CInterface.peak_icv_Region_Erosion(_handle, structuringElement._handle, ref outputRegion._handle));
309
310 return outputRegion;
311 }
312
320 public override string ToString()
321 {
322 List<Point> points = Points;
323 return "{" + string.Join(", ", points.Select(p => $"{{X: {p.X}, Y: {p.Y}}}")) + "}";
324 }
325
339 public static Region operator -(Region regionMinuend, Region regionSubtrahend)
340 {
341 return regionMinuend.Difference(regionSubtrahend);
342 }
343
351 public static Region operator +(Region region1, Region region2)
352 {
353 return region1.Union(region2);
354 }
355
356 private sealed class RegionEqualityComparer : IEqualityComparer<Region>
357 {
358 public bool Equals(Region x, Region y)
359 {
360 if (ReferenceEquals(x, y))
361 {
362 return true;
363 }
364
365 if (ReferenceEquals(x, null))
366 {
367 return false;
368 }
369
370 if (ReferenceEquals(y, null))
371 {
372 return false;
373 }
374
375 var isEqual = false;
376 DllHelper.ExecuteAndMapReturnCodes(() =>
377 CInterface.peak_icv_Region_Compare(x._handle, y._handle, out isEqual));
378
379 return isEqual;
380 }
381
382 public int GetHashCode(Region obj)
383 {
384 unchecked
385 {
386 return obj.Points?.Aggregate(17, (current, p) => current * 31 + p.GetHashCode()) ?? 0;
387 }
388 }
389 }
390
391 internal sealed class Handle : SafeHandleZeroOrMinusOneIsInvalid
392 {
393 internal Handle() : base(true)
394 {
395 DllHelper.ExecuteAndMapReturnCodes(() => CInterface.peak_icv_Region_Create(out handle));
396 }
397
398 internal Handle(Backend.Types.peak_icv_point[] cPoints) : base(true)
399 {
400 DllHelper.ExecuteAndMapReturnCodes(() =>
401 CInterface.peak_icv_Region_CreateFromPoints(out handle, cPoints, (uint) cPoints.Length));
402 }
403
404 internal Handle(Backend.Types.peak_common_rectangle cRectangle) : base(true)
405 {
406 DllHelper.ExecuteAndMapReturnCodes(() =>
407 CInterface.peak_icv_Region_CreateFromRectangle(out handle, cRectangle));
408 }
409
410 internal Handle(IntPtr handle) : base(true)
411 {
412 SetHandle(handle);
413 }
414
415 protected override bool ReleaseHandle()
416 {
417 return CInterface.peak_icv_Region_Destroy(handle) ==
418 Backend.Types.peak_icv_status.PEAK_ICV_STATUS_SUCCESS;
419 }
420 }
421
422 private static class CInterface
423 {
424 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
425 public static extern Backend.Types.peak_icv_status peak_icv_Region_Create(out IntPtr region_handle);
426
427 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
428 public static extern Backend.Types.peak_icv_status peak_icv_Region_CreateFromPoints(
429 out IntPtr region_handle,
430 Backend.Types.peak_icv_point[] points, nuint num_points);
431
432 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
433 public static extern Backend.Types.peak_icv_status peak_icv_Region_CreateFromRectangle(
434 out IntPtr region_handle,
435 Backend.Types.peak_common_rectangle rectangle);
436
437 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
438 public static extern Backend.Types.peak_icv_status peak_icv_Region_Destroy(IntPtr region_handle);
439
440 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
441 public static extern Backend.Types.peak_icv_status peak_icv_Region_GetArea(Handle input_region,
442 out nuint area);
443
444 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
445 public static extern Backend.Types.peak_icv_status peak_icv_Region_GetPoints_GetCount(
446 Handle input_region,
447 out nuint points_count);
448
449 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
450 public static extern Backend.Types.peak_icv_status peak_icv_Region_GetPoints(Handle input_region,
451 [Out] Backend.Types.peak_icv_point[] points, nuint num_points);
452
453 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
454 public static extern Backend.Types.peak_icv_status peak_icv_Region_GetConnectedComponents_GetCount(
455 Handle input_region, out nuint num_connected_components);
456
457 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
458 public static extern Backend.Types.peak_icv_status peak_icv_Region_GetConnectedComponents(
459 Handle input_region,
460 IntPtr[] output_regions, nuint num_connected_components);
461
462 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
463 public static extern Backend.Types.peak_icv_status peak_icv_Region_Array_Create(IntPtr[] region_handles,
464 nuint num_region_handles);
465
466 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
467 public static extern Backend.Types.peak_icv_status peak_icv_Region_Array_Destroy(IntPtr[] region_handles,
468 nuint num_region_handles);
469
470 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
471 public static extern Backend.Types.peak_icv_status peak_icv_Region_Compare(Handle region_lhs,
472 Handle region_rhs,
473 [MarshalAs(UnmanagedType.I1)] out bool is_equal);
474
475 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
476 public static extern Backend.Types.peak_icv_status peak_icv_Region_GetCenterOfGravity(Handle input_region,
477 out Backend.Types.peak_icv_point_f center_of_gravity);
478
479 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
480 public static extern Backend.Types.peak_icv_status peak_icv_Region_Difference(Handle input_region_minuend,
481 Handle input_region_subtrahend, ref Handle output_region_difference);
482
483 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
484 public static extern Backend.Types.peak_icv_status peak_icv_Region_Union(Handle input_region_1,
485 Handle input_region_2, ref Handle output_region_united);
486
487 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
488 public static extern Backend.Types.peak_icv_status peak_icv_Region_Intersection(Handle input_region_1,
489 Handle input_region_2, ref Handle output_region_intersection);
490
491 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
492 public static extern Backend.Types.peak_icv_status peak_icv_Region_Dilation(Handle input_region,
493 Handle input_structuring_element, ref Handle output_region_dilation);
494
495 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
496 public static extern Backend.Types.peak_icv_status peak_icv_Region_Erosion(Handle input_region,
497 Handle input_structuring_element, ref Handle output_region_erosion);
498
499 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
500 public static extern Backend.Types.peak_icv_status peak_icv_Region_Draw(Image.Handle input_image,
501 Handle input_region, Backend.Types.peak_icv_drawing_options drawing_options);
502 }
503}
TSize Width
Gets the width of the rectangle.
Definition Rectangle.cs:58
TPosition Y
Gets the Y coordinate of the top-left corner of the rectangle.
Definition Rectangle.cs:53
TPosition X
Gets or sets the X coordinate of the top-left corner of the rectangle.
Definition Rectangle.cs:48
Represents a 2D point with floating-point coordinates.
Definition Point.cs:126
Represents a 2D point with integer coordinates.
Definition Point.cs:98
Represents a rectangle with integer coordinates and dimensions.
Definition Rectangle.cs:138
ulong ColorCode
The color code.
Definition Color.cs:105
Represents the drawing options used by the Painter for rendering.
Color Color
Color used for drawing. Defaults to ColorConstant.Red.
A list that automatically calls IDisposable.Dispose on all its elements when disposed.
Constructs an Image from an IImageView Instance.
Definition Image.cs:18
Represents a region defined by a set of points in the image plane.
Definition Region.cs:43
DisposableList< Region > ConnectedComponents
Calculate the connected components of the region.
Definition Region.cs:100
Region Dilation(Region structuringElement)
Performs morphological erosion on the input region using the specified structuring element.
Definition Region.cs:285
static IEqualityComparer< Region > Comparer
Gets a default equality comparer for comparing Region data values.
Definition Region.cs:201
Region Difference(Region other)
Computes the difference of two regions.
Definition Region.cs:233
static Region operator-(Region regionMinuend, Region regionSubtrahend)
Performs a region difference operation. It is equivalent to the Difference method.
Definition Region.cs:339
Region Union(Region other)
Computes the union of two regions.
Definition Region.cs:250
static Region operator+(Region region1, Region region2)
Performs a region union operation. It is equivalent to the Union method.
Definition Region.cs:351
uint Area
Calculates the area of a region. The area is the number of pixels in the region.
Definition Region.cs:167
List< Point > Points
The points of the region.
Definition Region.cs:139
void Dispose()
Releases the resources used by the Region class.
Definition Region.cs:207
Region(Rectangle rectangle)
Creates a region from a Rectangle.
Definition Region.cs:73
override string ToString()
Converts the region to a string.
Definition Region.cs:320
Region()
Creates an empty Region.
Definition Region.cs:50
PointF CenterOfGravity
Calculates the center of gravity of a region.
Definition Region.cs:187
Region(Point[] points)
Creates a region from a collection of points.
Definition Region.cs:60
Region Erosion(Region structuringElement)
Performs morphological erosion on the input region using the specified structuring element.
Definition Region.cs:304
Region Intersection(Region other)
Computes the intersection of two regions.
Definition Region.cs:266
Base class for all drawables.
Definition IDrawable.cs:11
void Draw(Image image, DrawingOptions drawingOptions)
Base class draw function.