diff options
author | Tiago Conceição <Tiago_caza@hotmail.com> | 2021-10-02 07:05:45 +0300 |
---|---|---|
committer | Tiago Conceição <Tiago_caza@hotmail.com> | 2021-10-02 07:05:45 +0300 |
commit | 9794fe1f6dda4c5616339ee70b5814dfbdb7b92c (patch) | |
tree | 82f3b18b850cbc62d5ab949b9676693e9f460d54 /UVtools.Core/Extensions/EmguExtensions.cs | |
parent | 6b594c365ee3ada262aacc9459b826b4ed8a2ad0 (diff) |
(Add) Ignore areas smaller than an threshold
Diffstat (limited to 'UVtools.Core/Extensions/EmguExtensions.cs')
-rw-r--r-- | UVtools.Core/Extensions/EmguExtensions.cs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/UVtools.Core/Extensions/EmguExtensions.cs b/UVtools.Core/Extensions/EmguExtensions.cs index 1e85659..bb3674d 100644 --- a/UVtools.Core/Extensions/EmguExtensions.cs +++ b/UVtools.Core/Extensions/EmguExtensions.cs @@ -14,6 +14,7 @@ using Emgu.CV.Cuda; using Emgu.CV.CvEnum; using Emgu.CV.Structure; using Emgu.CV.Util; +using UVtools.Core.EmguCV; using UVtools.Core.Objects; namespace UVtools.Core.Extensions @@ -441,6 +442,17 @@ namespace UVtools.Core.Extensions } #endregion + #region Create methods + + public static Mat CreateMask(this Mat src, VectorOfVectorOfPoint contours) + { + var mask = src.NewBlank(); + CvInvoke.DrawContours(mask, contours, -1, WhiteColor, -1); + return mask; + } + + #endregion + #region Copy methods /// <summary> /// Copy a <see cref="Mat"/> to center of other <see cref="Mat"/> @@ -464,6 +476,58 @@ namespace UVtools.Core.Extensions m.CopyTo(dst); } } + + public static void CopyAreasSmallerThan(this Mat src, double threshold, Mat dst) + { + if (threshold <= 1) return; + using var contours = src.FindContours(out var hierarchy, RetrType.Tree); + var contourGroups = EmguContours.GetContoursInGroups(contours, hierarchy); + + var mask = src.NewBlank(); + uint drawContours = 0; + foreach (var contourGroup in contourGroups) + { + using var selectedContours = new VectorOfVectorOfPoint(); + foreach (var group in contourGroup) + { + var area = EmguContours.GetContourArea(group); + if (area >= threshold) continue; + drawContours++; + selectedContours.Push(group); + } + + if (selectedContours.Size == 0) continue; + CvInvoke.DrawContours(mask, selectedContours, -1, WhiteColor, -1); + + } + if (drawContours > 0) src.CopyTo(dst, mask); + } + + public static void CopyAreasLargerThan(this Mat src, double threshold, Mat dst) + { + if (threshold <= 0) return; + using var contours = src.FindContours(out var hierarchy, RetrType.Tree); + var contourGroups = EmguContours.GetContoursInGroups(contours, hierarchy); + + var mask = src.NewBlank(); + uint drawContours = 0; + foreach (var contourGroup in contourGroups) + { + using var selectedContours = new VectorOfVectorOfPoint(); + foreach (var group in contourGroup) + { + var area = EmguContours.GetContourArea(group); + if (area <= threshold) continue; + drawContours++; + selectedContours.Push(group); + } + + if (selectedContours.Size == 0) continue; + CvInvoke.DrawContours(mask, selectedContours, -1, WhiteColor, -1); + + } + if(drawContours > 0) src.CopyTo(dst, mask); + } #endregion #region Roi methods |