diff options
author | Tiago Conceição <Tiago_caza@hotmail.com> | 2021-09-23 03:21:02 +0300 |
---|---|---|
committer | Tiago Conceição <Tiago_caza@hotmail.com> | 2021-09-23 03:21:02 +0300 |
commit | b8a705ce214c98b05b4f4ec490019e91f4ec3454 (patch) | |
tree | 696b2ab7a4741cd330ed83c9d05fd97ba6f2f8b5 /UVtools.Core/Extensions/EmguExtensions.cs | |
parent | bfb45a4f0003498a0832ba18f3739811ebd262b0 (diff) |
RAM and performance improvements
Diffstat (limited to 'UVtools.Core/Extensions/EmguExtensions.cs')
-rw-r--r-- | UVtools.Core/Extensions/EmguExtensions.cs | 110 |
1 files changed, 109 insertions, 1 deletions
diff --git a/UVtools.Core/Extensions/EmguExtensions.cs b/UVtools.Core/Extensions/EmguExtensions.cs index 7b4577e..90473c4 100644 --- a/UVtools.Core/Extensions/EmguExtensions.cs +++ b/UVtools.Core/Extensions/EmguExtensions.cs @@ -527,6 +527,114 @@ namespace UVtools.Core.Extensions } #endregion + #region Find methods + + /// <summary> + /// Finds the first negative (Black) pixel + /// </summary> + /// <param name="mat"></param> + /// <returns>Pixel position in the span, or -1 if not found</returns> + public static int FindFirstNegativePixel(this Mat mat) + { + return mat.FindFirstPixelEqualTo(0); + } + + /// <summary> + /// Finds the first positive pixel + /// </summary> + /// <param name="mat"></param> + /// <returns>Pixel position in the span, or -1 if not found</returns> + public static int FindFirstPositivePixel(this Mat mat) + { + return mat.FindFirstPixelEqualOrGreaterThan(1); + } + + /// <summary> + /// Finds the first pixel that is <see cref="value"/> + /// </summary> + /// <param name="mat"></param> + /// <param name="value"></param> + /// <returns>Pixel position in the span, or -1 if not found</returns> + public static int FindFirstPixelEqualTo(this Mat mat, byte value) + { + var span = mat.GetDataByteSpan(); + for (var i = 0; i < span.Length; i++) + { + if (span[i] == value) return i; + } + + return -1; + } + + /// <summary> + /// Finds the first pixel that is at less than <see cref="value"/> + /// </summary> + /// <param name="mat"></param> + /// <param name="value"></param> + /// <returns>Pixel position in the span, or -1 if not found</returns> + public static int FindFirstPixelLessThan(this Mat mat, byte value) + { + var span = mat.GetDataByteSpan(); + for (var i = 0; i < span.Length; i++) + { + if (span[i] < value) return i; + } + + return -1; + } + + /// <summary> + /// Finds the first pixel that is at less or equal than <see cref="value"/> + /// </summary> + /// <param name="mat"></param> + /// <param name="value"></param> + /// <returns>Pixel position in the span, or -1 if not found</returns> + public static int FindFirstPixelEqualOrLessThan(this Mat mat, byte value) + { + var span = mat.GetDataByteSpan(); + for (var i = 0; i < span.Length; i++) + { + if (span[i] <= value) return i; + } + + return -1; + } + + /// <summary> + /// Finds the first pixel that is at greater than <see cref="value"/> + /// </summary> + /// <param name="mat"></param> + /// <param name="value"></param> + /// <returns>Pixel position in the span, or -1 if not found</returns> + public static int FindFirstPixelGreaterThan(this Mat mat, byte value) + { + var span = mat.GetDataByteSpan(); + for (var i = 0; i < span.Length; i++) + { + if (span[i] > value) return i; + } + + return -1; + } + + /// <summary> + /// Finds the first pixel that is at equal or greater than <see cref="value"/> + /// </summary> + /// <param name="mat"></param> + /// <param name="value"></param> + /// <returns>Pixel position in the span, or -1 if not found</returns> + public static int FindFirstPixelEqualOrGreaterThan(this Mat mat, byte value) + { + var span = mat.GetDataByteSpan(); + for (var i = 0; i < span.Length; i++) + { + if (span[i] >= value) return i; + } + + return -1; + } + #endregion + #region Transform methods public static void Transform(this Mat src, double xScale, double yScale, double xTrans = 0, double yTrans = 0, Size dstSize = default, Inter interpolation = Inter.Linear) { @@ -1009,7 +1117,7 @@ namespace UVtools.Core.Extensions // if there are no more 'white' pixels in the image, then // break from the loop - if (CvInvoke.CountNonZero(image) == 0) break; + if (image.FindFirstPixelEqualOrGreaterThan(1) == -1) break; } return skeleton; |